module: clarify cjs global-like error on ModuleJobSync · nodejs/node@72f2a22

@@ -65,6 +65,37 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>

6565

(globalLike) => errorMessage === `${globalLike} is not defined`,

6666

);

676768+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 };

@@ -476,8 +487,13 @@ class ModuleJobSync extends ModuleJobBase {

476487

throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);

477488

}

478489

setHasStartedUserESMExecution();

479-

const namespace = this.module.evaluateSync(filename, parentFilename);

480-

return { __proto__: null, module: this.module, namespace };

490+

try {

491+

const namespace = this.module.evaluateSync(filename, parentFilename);

492+

return { __proto__: null, module: this.module, namespace };

493+

} catch (e) {

494+

explainCommonJSGlobalLikeNotDefinedError(e, this.module.url);

495+

throw e;

496+

}

481497

}

482498

}

483499