Skip to content

Commit c05d02a

Browse files
committed
fixup! module: clarify cjs global-like error on ModuleJobSync
1 parent e009ed2 commit c05d02a

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
@@ -57,6 +57,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
5757
(globalLike) => errorMessage === `${globalLike} is not defined`,
5858
);
5959

60+
61+
/**
62+
*
63+
* @param {Error} e
64+
* @param {string} url
65+
* @returns {void}
66+
*/
67+
const explainCommonJSGlobalLikeNotDefinedError = (e, url) => {
68+
if (e?.name === 'ReferenceError' &&
69+
isCommonJSGlobalLikeNotDefinedError(e.message)) {
70+
e.message += ' in ES module scope';
71+
72+
if (StringPrototypeStartsWith(e.message, 'require ')) {
73+
e.message += ', you can use import instead';
74+
}
75+
76+
const packageConfig =
77+
StringPrototypeStartsWith(url, 'file://') &&
78+
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
79+
require('internal/modules/package_json_reader')
80+
.getPackageScopeConfig(url);
81+
if (packageConfig.type === 'module') {
82+
e.message +=
83+
'\nThis file is being treated as an ES module because it has a ' +
84+
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
85+
'"type": "module". To treat it as a CommonJS script, rename it ' +
86+
'to use the \'.cjs\' file extension.';
87+
}
88+
}
89+
};
90+
6091
class ModuleJobBase {
6192
constructor(url, importAttributes, isMain, inspectBrk) {
6293
this.importAttributes = importAttributes;
@@ -271,27 +302,7 @@ class ModuleJob extends ModuleJobBase {
271302
try {
272303
await this.module.evaluate(timeout, breakOnSigint);
273304
} catch (e) {
274-
if (e?.name === 'ReferenceError' &&
275-
isCommonJSGlobalLikeNotDefinedError(e.message)) {
276-
e.message += ' in ES module scope';
277-
278-
if (StringPrototypeStartsWith(e.message, 'require ')) {
279-
e.message += ', you can use import instead';
280-
}
281-
282-
const packageConfig =
283-
StringPrototypeStartsWith(this.module.url, 'file://') &&
284-
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
285-
require('internal/modules/package_json_reader')
286-
.getPackageScopeConfig(this.module.url);
287-
if (packageConfig.type === 'module') {
288-
e.message +=
289-
'\nThis file is being treated as an ES module because it has a ' +
290-
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
291-
'"type": "module". To treat it as a CommonJS script, rename it ' +
292-
'to use the \'.cjs\' file extension.';
293-
}
294-
}
305+
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
295306
throw e;
296307
}
297308
return { __proto__: null, module: this.module };
@@ -397,27 +408,7 @@ class ModuleJobSync extends ModuleJobBase {
397408
const namespace = this.module.evaluateSync();
398409
return { __proto__: null, module: this.module, namespace };
399410
} catch (e) {
400-
if (e?.name === 'ReferenceError' &&
401-
isCommonJSGlobalLikeNotDefinedError(e.message)) {
402-
e.message += ' in ES module scope';
403-
404-
if (StringPrototypeStartsWith(e.message, 'require ')) {
405-
e.message += ', you can use import instead';
406-
}
407-
408-
const packageConfig =
409-
StringPrototypeStartsWith(this.module.url, 'file://') &&
410-
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
411-
require('internal/modules/package_json_reader')
412-
.getPackageScopeConfig(this.module.url);
413-
if (packageConfig.type === 'module') {
414-
e.message +=
415-
'\nThis file is being treated as an ES module because it has a ' +
416-
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
417-
'"type": "module". To treat it as a CommonJS script, rename it ' +
418-
'to use the \'.cjs\' file extension.';
419-
}
420-
}
411+
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
421412
throw e;
422413
}
423414
}

0 commit comments

Comments
 (0)