@@ -58,6 +58,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
5858 (globalLike) => errorMessage === `${globalLike} is not defined`,
5959 );
6060
61+
62+ /**
63+ *
64+ * @param {Error} e
65+ * @param {string} url
66+ * @returns {void}
67+ */
68+ const explainCommonJSGlobalLikeNotDefinedError = (e, url) => {
69+ if (e?.name === 'ReferenceError' &&
70+ isCommonJSGlobalLikeNotDefinedError(e.message)) {
71+ e.message += ' in ES module scope';
72+
73+ if (StringPrototypeStartsWith(e.message, 'require ')) {
74+ e.message += ', you can use import instead';
75+ }
76+
77+ const packageConfig =
78+ StringPrototypeStartsWith(url, 'file://') &&
79+ RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
80+ require('internal/modules/package_json_reader')
81+ .getPackageScopeConfig(url);
82+ if (packageConfig.type === 'module') {
83+ e.message +=
84+ '\nThis file is being treated as an ES module because it has a ' +
85+ `'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
86+ '"type": "module". To treat it as a CommonJS script, rename it ' +
87+ 'to use the \'.cjs\' file extension.';
88+ }
89+ }
90+ };
91+
6192class ModuleJobBase {
6293 constructor(url, importAttributes, isMain, inspectBrk) {
6394 this.importAttributes = importAttributes;
@@ -273,27 +304,7 @@ class ModuleJob extends ModuleJobBase {
273304 try {
274305 await this.module.evaluate(timeout, breakOnSigint);
275306 } catch (e) {
276- if (e?.name === 'ReferenceError' &&
277- isCommonJSGlobalLikeNotDefinedError(e.message)) {
278- e.message += ' in ES module scope';
279-
280- if (StringPrototypeStartsWith(e.message, 'require ')) {
281- e.message += ', you can use import instead';
282- }
283-
284- const packageConfig =
285- StringPrototypeStartsWith(this.module.url, 'file://') &&
286- RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
287- require('internal/modules/package_json_reader')
288- .getPackageScopeConfig(this.module.url);
289- if (packageConfig.type === 'module') {
290- e.message +=
291- '\nThis file is being treated as an ES module because it has a ' +
292- `'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
293- '"type": "module". To treat it as a CommonJS script, rename it ' +
294- 'to use the \'.cjs\' file extension.';
295- }
296- }
307+ explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
297308 throw e;
298309 }
299310 return { __proto__: null, module: this.module };
@@ -397,8 +408,13 @@ class ModuleJobSync extends ModuleJobBase {
397408 throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);
398409 }
399410 setHasStartedUserESMExecution();
400- const namespace = this.module.evaluateSync(filename, parentFilename);
401- return { __proto__: null, module: this.module, namespace };
411+ try {
412+ const namespace = this.module.evaluateSync(filename, parentFilename);
413+ return { __proto__: null, module: this.module, namespace };
414+ } catch (e) {
415+ explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
416+ throw e;
417+ }
402418 }
403419}
404420
0 commit comments