module: throw on require('./path.mjs');
This is an extremely important part of the ESM implementation that should have been unflagged as a breaking change in v12.0.0 to allow us to unflag ESM in Node.js 12.x before LTS. Assuming we can get consensus on this behavior I would argue that this Semver-Major behavior change could be viewed as a Semver-Patch fix in v12.0.1 PR-URL: https://github.com/nodejs/node/pull/27417 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
411063c6f5
commit
d370d126c3
@ -132,6 +132,13 @@ variable. Since the module lookups using `node_modules` folders are all
|
||||
relative, and based on the real path of the files making the calls to
|
||||
`require()`, the packages themselves can be anywhere.
|
||||
|
||||
## Addenda: The .mjs extension
|
||||
|
||||
It is not possible to `require()` files that have the `.mjs` extension.
|
||||
Attempting to do so will throw [an error][]. The `.mjs` extension is
|
||||
reserved for [ECMAScript Modules][] which cannot be loaded via `require()`.
|
||||
See [ECMAScript Modules][] for more details.
|
||||
|
||||
## All Together...
|
||||
|
||||
<!-- type=misc -->
|
||||
@ -950,6 +957,8 @@ requireUtil('./some-tool');
|
||||
[`createRequire()`]: #modules_module_createrequire_filename
|
||||
[`module` object]: #modules_the_module_object
|
||||
[`path.dirname()`]: path.html#path_path_dirname_path
|
||||
[ECMAScript Modules]: esm.html
|
||||
[an error]: errors.html#errors_err_require_esm
|
||||
[exports shortcut]: #modules_exports_shortcut
|
||||
[module resolution]: #modules_all_together
|
||||
[module wrapper]: #modules_the_module_wrapper
|
||||
|
@ -800,11 +800,9 @@ Module._extensions['.node'] = function(module, filename) {
|
||||
return process.dlopen(module, path.toNamespacedPath(filename));
|
||||
};
|
||||
|
||||
if (experimentalModules) {
|
||||
Module._extensions['.mjs'] = function(module, filename) {
|
||||
throw new ERR_REQUIRE_ESM(filename);
|
||||
};
|
||||
}
|
||||
Module._extensions['.mjs'] = function(module, filename) {
|
||||
throw new ERR_REQUIRE_ESM(filename);
|
||||
};
|
||||
|
||||
// Bootstrap main module.
|
||||
Module.runMain = function() {
|
||||
|
11
test/parallel/test-require-mjs.js
Normal file
11
test/parallel/test-require-mjs.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
assert.throws(
|
||||
() => require('../fixtures/es-modules/test-esm-ok.mjs'),
|
||||
{
|
||||
message: /Must use import to load ES Module/,
|
||||
code: 'ERR_REQUIRE_ESM'
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user