Skip to content

Commit 014a9fd

Browse files
MylesBorinstargos
authored andcommitted
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: #27417 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 5bcd770 commit 014a9fd

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

doc/api/modules.md

+9
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ variable. Since the module lookups using `node_modules` folders are all
132132
relative, and based on the real path of the files making the calls to
133133
`require()`, the packages themselves can be anywhere.
134134

135+
## Addenda: The .mjs extension
136+
137+
It is not possible to `require()` files that have the `.mjs` extension.
138+
Attempting to do so will throw [an error][]. The `.mjs` extension is
139+
reserved for [ECMAScript Modules][] which cannot be loaded via `require()`.
140+
See [ECMAScript Modules][] for more details.
141+
135142
## All Together...
136143

137144
<!-- type=misc -->
@@ -950,6 +957,8 @@ requireUtil('./some-tool');
950957
[`createRequire()`]: #modules_module_createrequire_filename
951958
[`module` object]: #modules_the_module_object
952959
[`path.dirname()`]: path.html#path_path_dirname_path
960+
[ECMAScript Modules]: esm.html
961+
[an error]: errors.html#errors_err_require_esm
953962
[exports shortcut]: #modules_exports_shortcut
954963
[module resolution]: #modules_all_together
955964
[module wrapper]: #modules_the_module_wrapper

lib/internal/modules/cjs/loader.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,9 @@ Module._extensions['.node'] = function(module, filename) {
800800
return process.dlopen(module, path.toNamespacedPath(filename));
801801
};
802802

803-
if (experimentalModules) {
804-
Module._extensions['.mjs'] = function(module, filename) {
805-
throw new ERR_REQUIRE_ESM(filename);
806-
};
807-
}
803+
Module._extensions['.mjs'] = function(module, filename) {
804+
throw new ERR_REQUIRE_ESM(filename);
805+
};
808806

809807
// Bootstrap main module.
810808
Module.runMain = function() {

test/parallel/test-require-mjs.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
assert.throws(
6+
() => require('../fixtures/es-modules/test-esm-ok.mjs'),
7+
{
8+
message: /Must use import to load ES Module/,
9+
code: 'ERR_REQUIRE_ESM'
10+
}
11+
);

0 commit comments

Comments
 (0)