fix(node): improve error message requiring non-npm es module (#19856)

Closes #19842
Closes #16913
This commit is contained in:
David Sherret 2023-07-17 16:19:00 -04:00 committed by GitHub
parent 7a9f7f3419
commit 4ebe3bdb06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 53 additions and 29 deletions

View file

@ -104,6 +104,14 @@ itest!(cjs_require_esm_mjs_error {
exit_code: 1,
});
itest!(require_esm_error {
args: "run --allow-read --quiet node/require_esm_error/main.ts",
output: "node/require_esm_error/main.out",
envs: env_vars_for_npm_tests(),
http_server: true,
exit_code: 1,
});
itest!(translate_cjs_to_esm {
args: "run -A --quiet npm/translate_cjs_to_esm/main.js",
output: "npm/translate_cjs_to_esm/main.out",

View file

@ -0,0 +1 @@
export class Test {}

View file

@ -0,0 +1,3 @@
error: Uncaught Error: require() of ES Module [WILDCARD]esm.js from [WILDCARD]main.ts not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.
at [WILDCARD]
at file:///[WILDCARD]/require_esm_error/main.ts:5:1

View file

@ -0,0 +1,5 @@
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
require("./esm.js");

View file

@ -1,4 +1,2 @@
error: Uncaught Error: [ERR_REQUIRE_ESM]: require() of ES Module [WILDCARD]my_esm_module.js from [WILDCARD]index.js not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.
at Object.Module._extensions..js (node:module:[WILDCARD])
error: Uncaught Error: require() of ES Module [WILDCARD]my_esm_module.js from [WILDCARD]index.js not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.
[WILDCARD]
at Module.load (node:module:[WILDCARD])

View file

@ -1,4 +1,2 @@
error: Uncaught Error: [ERR_REQUIRE_ESM]: require() of ES Module [WILDCARD]esm_mjs.mjs from [WILDCARD]require_mjs.js not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.
at Module.load (node:module:[WILDCARD])
error: Uncaught Error: require() of ES Module [WILDCARD]esm_mjs.mjs from [WILDCARD]require_mjs.js not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.
[WILDCARD]
at Function.Module._load (node:module:[WILDCARD])

View file

@ -878,9 +878,9 @@ Module.prototype.load = function (filename) {
if (
StringPrototypeEndsWith(filename, ".mjs") && !Module._extensions[".mjs"]
) {
// TODO: use proper error class
throw new Error(
requireEsmErrorText(filename, moduleParentCache.get(this)?.filename),
throw createRequireEsmError(
filename,
moduleParentCache.get(this)?.filename,
);
}
@ -923,20 +923,22 @@ Module.wrap = function (script) {
return `${Module.wrapper[0]}${script}${Module.wrapper[1]}`;
};
function isEsmSyntaxError(error) {
return error instanceof SyntaxError && (
StringPrototypeIncludes(
error.message,
"Cannot use import statement outside a module",
) ||
StringPrototypeIncludes(error.message, "Unexpected token 'export'")
);
}
function enrichCJSError(error) {
if (error instanceof SyntaxError) {
if (
StringPrototypeIncludes(
error.message,
"Cannot use import statement outside a module",
) ||
StringPrototypeIncludes(error.message, "Unexpected token 'export'")
) {
console.error(
'To load an ES module, set "type": "module" in the package.json or use ' +
"the .mjs extension.",
);
}
if (isEsmSyntaxError(error)) {
console.error(
'To load an ES module, set "type": "module" in the package.json or use ' +
"the .mjs extension.",
);
}
}
@ -951,7 +953,14 @@ function wrapSafe(
if (nodeGlobalThis.process.mainModule === cjsModuleInstance) {
enrichCJSError(err.thrown);
}
throw err.thrown;
if (isEsmSyntaxError(err.thrown)) {
throw createRequireEsmError(
filename,
moduleParentCache.get(cjsModuleInstance)?.filename,
);
} else {
throw err.thrown;
}
}
return f;
}
@ -963,7 +972,6 @@ Module.prototype._compile = function (content, filename) {
const require = makeRequireFunction(this);
const exports = this.exports;
const thisValue = exports;
const module = this;
if (requireDepth === 0) {
statCache = new SafeMap();
}
@ -994,8 +1002,9 @@ Module._extensions[".js"] = function (module, filename) {
if (StringPrototypeEndsWith(filename, ".js")) {
const pkg = ops.op_require_read_closest_package_json(filename);
if (pkg && pkg.exists && pkg.typ === "module") {
throw new Error(
requireEsmErrorText(filename, moduleParentCache.get(module)?.filename),
throw createRequireEsmError(
filename,
moduleParentCache.get(module)?.filename,
);
}
}
@ -1003,8 +1012,8 @@ Module._extensions[".js"] = function (module, filename) {
module._compile(content, filename);
};
function requireEsmErrorText(filename, parent) {
let message = `[ERR_REQUIRE_ESM]: require() of ES Module ${filename}`;
function createRequireEsmError(filename, parent) {
let message = `require() of ES Module ${filename}`;
if (parent) {
message += ` from ${parent}`;
@ -1012,7 +1021,9 @@ function requireEsmErrorText(filename, parent) {
message +=
` not supported. Instead change the require to a dynamic import() which is available in all CommonJS modules.`;
return message;
const err = new Error(message);
err.code = "ERR_REQUIRE_ESM";
return err;
}
function stripBOM(content) {