Skip to content

Commit 162e1a5

Browse files
committed
module: proxy require('esm') to import('esm')
1 parent 38a15d8 commit 162e1a5

File tree

5 files changed

+38
-86
lines changed

5 files changed

+38
-86
lines changed

lib/internal/errors.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,26 +1396,6 @@ E('ERR_PACKAGE_PATH_NOT_EXPORTED', (pkgPath, subpath, base = undefined) => {
13961396
E('ERR_PERFORMANCE_INVALID_TIMESTAMP',
13971397
'%d is not a valid timestamp', TypeError);
13981398
E('ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS', '%s', TypeError);
1399-
E('ERR_REQUIRE_ESM',
1400-
(filename, parentPath = null, packageJsonPath = null) => {
1401-
let msg = `Must use import to load ES Module: ${filename}`;
1402-
if (parentPath && packageJsonPath) {
1403-
const path = require('path');
1404-
const basename = path.basename(filename) === path.basename(parentPath) ?
1405-
filename : path.basename(filename);
1406-
msg +=
1407-
'\nrequire() of ES modules is not supported.\nrequire() of ' +
1408-
`${filename} from ${parentPath} ` +
1409-
'is an ES module file as it is a .js file whose nearest parent ' +
1410-
'package.json contains "type": "module" which defines all .js ' +
1411-
'files in that package scope as ES modules.\nInstead rename ' +
1412-
`${basename} to end in .cjs, change the requiring code to use ` +
1413-
'import(), or remove "type": "module" from ' +
1414-
`${packageJsonPath}.\n`;
1415-
return msg;
1416-
}
1417-
return msg;
1418-
}, Error);
14191399
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
14201400
'Script execution was interrupted by `SIGINT`', Error);
14211401
E('ERR_SERVER_ALREADY_LISTEN',

lib/internal/modules/cjs/loader.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ let hasLoadedAnyUserCJSModule = false;
111111
const {
112112
ERR_INVALID_ARG_VALUE,
113113
ERR_INVALID_MODULE_SPECIFIER,
114-
ERR_REQUIRE_ESM,
115114
ERR_UNKNOWN_BUILTIN_MODULE,
116115
} = require('internal/errors').codes;
117116
const { validateString } = require('internal/validators');
@@ -983,8 +982,17 @@ Module.prototype.load = function(filename) {
983982

984983
const extension = findLongestRegisteredExtension(filename);
985984
// allow .mjs to be overridden
986-
if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs'])
987-
throw new ERR_REQUIRE_ESM(filename);
985+
if (StringPrototypeEndsWith(filename, '.mjs') &&
986+
!Module._extensions['.mjs']) {
987+
process.emitWarning('Attempting to require and ESModule.' +
988+
'Replace `require` with `import`',
989+
'Warning', 'WARN_REQUIRE_ESM');
990+
const ESMLoader = asyncESM.ESMLoader;
991+
const exports = ESMLoader.import(filename);
992+
this.exports = exports;
993+
this.loaded = true;
994+
return;
995+
}
988996

989997
Module._extensions[extension](this, filename);
990998
this.loaded = true;
@@ -1120,10 +1128,14 @@ Module._extensions['.js'] = function(module, filename) {
11201128
const pkg = readPackageScope(filename);
11211129
// Function require shouldn't be used in ES modules.
11221130
if (pkg?.data?.type === 'module') {
1123-
const parent = moduleParentCache.get(module);
1124-
const parentPath = parent?.filename;
1125-
const packageJsonPath = path.resolve(pkg.path, 'package.json');
1126-
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
1131+
process.emitWarning('Attempting to require and ESModule.' +
1132+
'Replace `require` with `import`',
1133+
'Warning', 'WARN_REQUIRE_ESM');
1134+
const ESMLoader = asyncESM.ESMLoader;
1135+
const exports = ESMLoader.import(filename);
1136+
module.exports = exports;
1137+
module.loaded = true;
1138+
return;
11271139
}
11281140
}
11291141
// If already analyzed the source, then it will be cached.

test/es-module/test-cjs-esm-warn.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/es-module/test-esm-type-flag-errors.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ expect('', packageWithoutTypeMain, 'package-without-type');
2323
expect('--input-type=module', packageTypeModuleMain,
2424
'ERR_INPUT_TYPE_NOT_ALLOWED', true);
2525

26-
try {
27-
require('../fixtures/es-modules/package-type-module/index.js');
28-
assert.fail('Expected CJS to fail loading from type: module package.');
29-
} catch (e) {
30-
assert.strictEqual(e.name, 'Error');
31-
assert.strictEqual(e.code, 'ERR_REQUIRE_ESM');
32-
assert(e.toString().match(/Must use import to load ES Module/g));
33-
assert(e.message.match(/Must use import to load ES Module/g));
34-
}
35-
3626
function expect(opt = '', inputFile, want, wantsError = false) {
3727
const argv = [inputFile];
3828
const opts = {

test/parallel/test-require-mjs.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
'use strict';
2-
require('../common');
2+
const { expectWarning } = require('../common');
33
const assert = require('assert');
44

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-
);
5+
expectWarning('Warning', [
6+
[
7+
'Attempting to require and ESModule. Replace `require` with `import`',
8+
'WARN_REQUIRE_ESM',
9+
],
10+
[
11+
'Attempting to require and ESModule. Replace `require` with `import`',
12+
'WARN_REQUIRE_ESM',
13+
],
14+
]);
15+
16+
require('../fixtures/es-modules/test-esm-ok.mjs').then((module) => {
17+
assert.ok(module.default);
18+
});
19+
20+
require('../fixtures/es-modules/package-type-module').then((module) => {
21+
assert.strictEqual(module.default, 'package-type-module');
22+
});

0 commit comments

Comments
 (0)