summaryrefslogtreecommitdiff
path: root/lib/internal/modules
diff options
context:
space:
mode:
authorJuan José Arboleda <soyjuanarbol@gmail.com>2019-11-27 20:26:36 -0500
committerGuy Bedford <guybedford@gmail.com>2019-12-04 23:25:10 -0500
commit81ac3023b3ab082804deda96d65530321d8af448 (patch)
tree28b6f42ac9701d79b036667f983b92238709271c /lib/internal/modules
parentbcd5491219bfd34630d331bdca3c92538a7b0e5e (diff)
downloadandroid-node-v8-81ac3023b3ab082804deda96d65530321d8af448.tar.gz
android-node-v8-81ac3023b3ab082804deda96d65530321d8af448.tar.bz2
android-node-v8-81ac3023b3ab082804deda96d65530321d8af448.zip
lib,test: improves ERR_REQUIRE_ESM message
PR-URL: https://github.com/nodejs/node/pull/30694 Fixes: https://github.com/nodejs/node/issues/30599 Reviewed-By: Guy Bedford <guybedford@gmail.com>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r--lib/internal/modules/cjs/loader.js27
1 files changed, 4 insertions, 23 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 11c04e3e0c..84b2b4772b 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -1196,33 +1196,14 @@ Module.prototype._compile = function(content, filename) {
};
// Native extension for .js
-let warnRequireESM = true;
Module._extensions['.js'] = function(module, filename) {
if (filename.endsWith('.js')) {
const pkg = readPackageScope(filename);
+ // Function require shouldn't be used in ES modules.
if (pkg && pkg.data && pkg.data.type === 'module') {
- if (warnRequireESM) {
- const parentPath = module.parent && module.parent.filename;
- const basename = parentPath &&
- path.basename(filename) === path.basename(parentPath) ?
- filename : path.basename(filename);
- process.emitWarning(
- 'require() of ES modules is not supported.\nrequire() of ' +
- `${filename} ${parentPath ? `from ${module.parent.filename} ` : ''}` +
- 'is an ES module file as it is a .js file whose nearest parent ' +
- 'package.json contains "type": "module" which defines all .js ' +
- 'files in that package scope as ES modules.\nInstead rename ' +
- `${basename} to end in .cjs, change the requiring code to use ` +
- 'import(), or remove "type": "module" from ' +
- `${path.resolve(pkg.path, 'package.json')}.`,
- undefined,
- undefined,
- undefined,
- true
- );
- warnRequireESM = false;
- }
- throw new ERR_REQUIRE_ESM(filename);
+ const parentPath = module.parent && module.parent.filename;
+ const packageJsonPath = path.resolve(pkg.path, 'package.json');
+ throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
}
}
const content = fs.readFileSync(filename, 'utf8');