You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
v16.6.0 regression — unexpected error thrown in REPL when calling require() on ES modules in a "type": "module" project #39618
Linux ultima-weapon 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
errors
What steps will reproduce the bug?
Create a package.json file with "type": "module"
Create a JS file — it doesn't even need to have any contents, though it can (e.g. export default {} or anything else).
Try to require() the file with node REPL.
How often does it reproduce? Is there a required condition?
Bug can be consistently reproduced on Node.js 16.6.0.
What is the expected behavior?
An ERR_REQUIRE_ESM error is thrown, as it is in 16.5.0 and prior, e.g.:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/module.js
at __node_internal_captureLargerStackTrace (node:internal/errors:463:5)
at new NodeError (node:internal/errors:370:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:13)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:93:18) {
code: 'ERR_REQUIRE_ESM'
}
What do you see instead?
An ERR_INVALID_ARG_TYPE error is thrown:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null
at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
at new NodeError (node:internal/errors:371:5)
at validateString (node:internal/validators:119:11)
at Object.basename (node:path:1309:5)
at Error.<anonymous> (node:internal/errors:1452:55)
at getMessage (node:internal/errors:421:12)
at new NodeError (node:internal/errors:348:21)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1128:19)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18) {
code: 'ERR_INVALID_ARG_TYPE'
}
Additional information
This regression is causing Jest to exit when running on projects with "type": "module" (see jestjs/jest#11708) since Jest is expecting the ESM error. In that particular case, it looks like Jest actually receives another error — the details are in the linked issue. Unrelated to this error; the Jest issue was fixed by #39593 pending release.
Thanks for the link! I searched for issues and PRs before creating this one but missed this fix. That definitely resolves the issue with Jest, all that remains then is the behaviour in the REPL.
As it's clear to me now that the issue Jest experienced is distinct from this behaviour in the REPL, I've updated the reproduction instructions and removed the in a script portion.
changed the title [-]v16.6.0 regression — unexpected error thrown when calling require() on ES modules in a "type": "module" project[/-][+]v16.6.0 regression — unexpected error thrown in REPL when calling require() on ES modules in a "type": "module" project[/+]on Aug 1, 2021
Activity
Mesteery commentedon Aug 1, 2021
I can reproduce in the REPL but not in a script :
node b.jsadalinesimonian commentedon Aug 1, 2021
targos commentedon Aug 1, 2021
The error you see with Jest was fixed in #39593
adalinesimonian commentedon Aug 1, 2021
Thanks for the link! I searched for issues and PRs before creating this one but missed this fix. That definitely resolves the issue with Jest, all that remains then is the behaviour in the REPL.
As it's clear to me now that the issue Jest experienced is distinct from this behaviour in the REPL, I've updated the reproduction instructions and removed the
in a scriptportion.[-]v16.6.0 regression — unexpected error thrown when calling require() on ES modules in a "type": "module" project[/-][+]v16.6.0 regression — unexpected error thrown in REPL when calling require() on ES modules in a "type": "module" project[/+]csmart29 commentedon Aug 2, 2021
git://github.com/creationix/nvm.git
BridgeAR commentedon Sep 11, 2025
This is fixed in newer Node.js versions.