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:
Myles Borins 2019-04-25 17:32:04 -04:00
parent 411063c6f5
commit d370d126c3
No known key found for this signature in database
GPG Key ID: 933B01F40B5CA946
3 changed files with 23 additions and 5 deletions

View File

@ -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 relative, and based on the real path of the files making the calls to
`require()`, the packages themselves can be anywhere. `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... ## All Together...
<!-- type=misc --> <!-- type=misc -->
@ -950,6 +957,8 @@ requireUtil('./some-tool');
[`createRequire()`]: #modules_module_createrequire_filename [`createRequire()`]: #modules_module_createrequire_filename
[`module` object]: #modules_the_module_object [`module` object]: #modules_the_module_object
[`path.dirname()`]: path.html#path_path_dirname_path [`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 [exports shortcut]: #modules_exports_shortcut
[module resolution]: #modules_all_together [module resolution]: #modules_all_together
[module wrapper]: #modules_the_module_wrapper [module wrapper]: #modules_the_module_wrapper

View File

@ -800,11 +800,9 @@ Module._extensions['.node'] = function(module, filename) {
return process.dlopen(module, path.toNamespacedPath(filename)); return process.dlopen(module, path.toNamespacedPath(filename));
}; };
if (experimentalModules) { Module._extensions['.mjs'] = function(module, filename) {
Module._extensions['.mjs'] = function(module, filename) { throw new ERR_REQUIRE_ESM(filename);
throw new ERR_REQUIRE_ESM(filename); };
};
}
// Bootstrap main module. // Bootstrap main module.
Module.runMain = function() { Module.runMain = function() {

View 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'
}
);