Skip to content

Commit 7d95b68

Browse files
committed
fixup! module: clarify cjs global-like error on ModuleJobSync
1 parent 1b9c713 commit 7d95b68

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

lib/internal/modules/esm/module_job.js

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
6565
(globalLike) => errorMessage === `${globalLike} is not defined`,
6666
);
6767

68+
69+
/**
70+
*
71+
* @param {Error} e
72+
* @param {string} url
73+
* @returns {void}
74+
*/
75+
const explainCommonJSGlobalLikeNotDefinedError = (e, url) => {
76+
if (e?.name === 'ReferenceError' &&
77+
isCommonJSGlobalLikeNotDefinedError(e.message)) {
78+
e.message += ' in ES module scope';
79+
80+
if (StringPrototypeStartsWith(e.message, 'require ')) {
81+
e.message += ', you can use import instead';
82+
}
83+
84+
const packageConfig =
85+
StringPrototypeStartsWith(url, 'file://') &&
86+
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
87+
require('internal/modules/package_json_reader')
88+
.getPackageScopeConfig(url);
89+
if (packageConfig.type === 'module') {
90+
e.message +=
91+
'\nThis file is being treated as an ES module because it has a ' +
92+
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
93+
'"type": "module". To treat it as a CommonJS script, rename it ' +
94+
'to use the \'.cjs\' file extension.';
95+
}
96+
}
97+
};
98+
6899
class ModuleJobBase {
69100
constructor(url, importAttributes, phase, isMain, inspectBrk) {
70101
assert(typeof phase === 'number');
@@ -326,27 +357,7 @@ class ModuleJob extends ModuleJobBase {
326357
try {
327358
await this.module.evaluate(timeout, breakOnSigint);
328359
} catch (e) {
329-
if (e?.name === 'ReferenceError' &&
330-
isCommonJSGlobalLikeNotDefinedError(e.message)) {
331-
e.message += ' in ES module scope';
332-
333-
if (StringPrototypeStartsWith(e.message, 'require ')) {
334-
e.message += ', you can use import instead';
335-
}
336-
337-
const packageConfig =
338-
StringPrototypeStartsWith(this.module.url, 'file://') &&
339-
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
340-
require('internal/modules/package_json_reader')
341-
.getPackageScopeConfig(this.module.url);
342-
if (packageConfig.type === 'module') {
343-
e.message +=
344-
'\nThis file is being treated as an ES module because it has a ' +
345-
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
346-
'"type": "module". To treat it as a CommonJS script, rename it ' +
347-
'to use the \'.cjs\' file extension.';
348-
}
349-
}
360+
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
350361
throw e;
351362
}
352363
return { __proto__: null, module: this.module };
@@ -480,27 +491,7 @@ class ModuleJobSync extends ModuleJobBase {
480491
const namespace = this.module.evaluateSync(filename, parentFilename);
481492
return { __proto__: null, module: this.module, namespace };
482493
} catch (e) {
483-
if (e?.name === 'ReferenceError' &&
484-
isCommonJSGlobalLikeNotDefinedError(e.message)) {
485-
e.message += ' in ES module scope';
486-
487-
if (StringPrototypeStartsWith(e.message, 'require ')) {
488-
e.message += ', you can use import instead';
489-
}
490-
491-
const packageConfig =
492-
StringPrototypeStartsWith(this.module.url, 'file://') &&
493-
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
494-
require('internal/modules/package_json_reader')
495-
.getPackageScopeConfig(this.module.url);
496-
if (packageConfig.type === 'module') {
497-
e.message +=
498-
'\nThis file is being treated as an ES module because it has a ' +
499-
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
500-
'"type": "module". To treat it as a CommonJS script, rename it ' +
501-
'to use the \'.cjs\' file extension.';
502-
}
503-
}
494+
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
504495
throw e;
505496
}
506497
}

0 commit comments

Comments
 (0)