Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: clarify cjs global-like error on ModuleJobSync #56491

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
(globalLike) => errorMessage === `${globalLike} is not defined`,
);


/**
*
* @param {Error} e
* @param {string} url
* @returns {void}
*/
const explainCommonJSGlobalLikeNotDefinedError = (e, url) => {
if (e?.name === 'ReferenceError' &&
isCommonJSGlobalLikeNotDefinedError(e.message)) {
e.message += ' in ES module scope';

if (StringPrototypeStartsWith(e.message, 'require ')) {
e.message += ', you can use import instead';
}

const packageConfig =
StringPrototypeStartsWith(url, 'file://') &&
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
require('internal/modules/package_json_reader')
.getPackageScopeConfig(url);
if (packageConfig.type === 'module') {
e.message +=
'\nThis file is being treated as an ES module because it has a ' +
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
'"type": "module". To treat it as a CommonJS script, rename it ' +
'to use the \'.cjs\' file extension.';
}
}
};

class ModuleJobBase {
constructor(url, importAttributes, isMain, inspectBrk) {
this.importAttributes = importAttributes;
Expand Down Expand Up @@ -271,27 +302,7 @@ class ModuleJob extends ModuleJobBase {
try {
await this.module.evaluate(timeout, breakOnSigint);
} catch (e) {
if (e?.name === 'ReferenceError' &&
isCommonJSGlobalLikeNotDefinedError(e.message)) {
e.message += ' in ES module scope';

if (StringPrototypeStartsWith(e.message, 'require ')) {
e.message += ', you can use import instead';
}

const packageConfig =
StringPrototypeStartsWith(this.module.url, 'file://') &&
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
require('internal/modules/package_json_reader')
.getPackageScopeConfig(this.module.url);
if (packageConfig.type === 'module') {
e.message +=
'\nThis file is being treated as an ES module because it has a ' +
`'.js' file extension and '${packageConfig.pjsonPath}' contains ` +
'"type": "module". To treat it as a CommonJS script, rename it ' +
'to use the \'.cjs\' file extension.';
}
}
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
throw e;
}
return { __proto__: null, module: this.module };
Expand Down Expand Up @@ -381,7 +392,6 @@ class ModuleJobSync extends ModuleJobBase {
}

runSync() {
// TODO(joyeecheung): add the error decoration logic from the async instantiate.
this.module.async = this.module.instantiateSync();
// If --experimental-print-required-tla is true, proceeds to evaluation even
// if it's async because we want to search for the TLA and help users locate
Expand All @@ -393,8 +403,13 @@ class ModuleJobSync extends ModuleJobBase {
throw new ERR_REQUIRE_ASYNC_MODULE();
}
setHasStartedUserESMExecution();
const namespace = this.module.evaluateSync();
return { __proto__: null, module: this.module, namespace };
try {
const namespace = this.module.evaluateSync();
return { __proto__: null, module: this.module, namespace };
} catch (e) {
explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);
throw e;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-require-module-error-catching.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ assert.throws(() => {
require('../fixtures/es-modules/reference-error-esm.js');
}, {
name: 'ReferenceError',
message: 'exports is not defined'
message: 'exports is not defined in ES module scope'
});
Loading