-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: proxy require('esm') to import('esm') #39064
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -111,7 +111,6 @@ let hasLoadedAnyUserCJSModule = false; | |||||
const { | ||||||
ERR_INVALID_ARG_VALUE, | ||||||
ERR_INVALID_MODULE_SPECIFIER, | ||||||
ERR_REQUIRE_ESM, | ||||||
ERR_UNKNOWN_BUILTIN_MODULE, | ||||||
} = require('internal/errors').codes; | ||||||
const { validateString } = require('internal/validators'); | ||||||
|
@@ -983,8 +982,17 @@ Module.prototype.load = function(filename) { | |||||
|
||||||
const extension = findLongestRegisteredExtension(filename); | ||||||
// allow .mjs to be overridden | ||||||
if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs']) | ||||||
throw new ERR_REQUIRE_ESM(filename); | ||||||
if (StringPrototypeEndsWith(filename, '.mjs') && | ||||||
!Module._extensions['.mjs']) { | ||||||
process.emitWarning('Attempting to require and ESModule.' + | ||||||
'Replace `require` with `import`', | ||||||
'Warning', 'WARN_REQUIRE_ESM'); | ||||||
const ESMLoader = asyncESM.ESMLoader; | ||||||
const exports = ESMLoader.import(filename); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be converted to a URL for windows paths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also makes sense to expose the inners of |
||||||
this.exports = exports; | ||||||
this.loaded = true; | ||||||
return; | ||||||
} | ||||||
|
||||||
Module._extensions[extension](this, filename); | ||||||
this.loaded = true; | ||||||
|
@@ -1120,10 +1128,14 @@ Module._extensions['.js'] = function(module, filename) { | |||||
const pkg = readPackageScope(filename); | ||||||
// Function require shouldn't be used in ES modules. | ||||||
if (pkg?.data?.type === 'module') { | ||||||
const parent = moduleParentCache.get(module); | ||||||
const parentPath = parent?.filename; | ||||||
const packageJsonPath = path.resolve(pkg.path, 'package.json'); | ||||||
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); | ||||||
process.emitWarning('Attempting to require and ESModule.' + | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'Replace `require` with `import`', | ||||||
'Warning', 'WARN_REQUIRE_ESM'); | ||||||
const ESMLoader = asyncESM.ESMLoader; | ||||||
const exports = ESMLoader.import(filename); | ||||||
module.exports = exports; | ||||||
module.loaded = true; | ||||||
return; | ||||||
} | ||||||
} | ||||||
// If already analyzed the source, then it will be cached. | ||||||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
'use strict'; | ||
require('../common'); | ||
const { expectWarning } = 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' | ||
} | ||
); | ||
expectWarning('Warning', [ | ||
[ | ||
'Attempting to require and ESModule. Replace `require` with `import`', | ||
'WARN_REQUIRE_ESM', | ||
], | ||
[ | ||
'Attempting to require and ESModule. Replace `require` with `import`', | ||
'WARN_REQUIRE_ESM', | ||
], | ||
]); | ||
|
||
require('../fixtures/es-modules/test-esm-ok.mjs').then((module) => { | ||
assert.ok(module.default); | ||
}); | ||
|
||
require('../fixtures/es-modules/package-type-module').then((module) => { | ||
assert.strictEqual(module.default, 'package-type-module'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests