Skip to content
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

v16.6.0 regression — unexpected error thrown in REPL when calling require() on ES modules in a "type": "module" project #39618

Open
adalinesimonian opened this issue Aug 1, 2021 · 5 comments
Labels
repl Issues and PRs related to the REPL subsystem.

Comments

@adalinesimonian
Copy link

adalinesimonian commented Aug 1, 2021

Version

v16.6.0

Platform

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?

  1. Create a package.json file with "type": "module"
  2. Create a JS file — it doesn't even need to have any contents, though it can (e.g. export default {} or anything else).
  3. 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.

@Mesteery
Copy link
Contributor

Mesteery commented Aug 1, 2021

or in a script.

I can reproduce in the REPL but not in a script : node b.js

// b.js
require('./a.js')
require('./a.js')
^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\me\workspace\$experiments\nrepo\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///C:/Users/me/workspace/$experiments/nrepo/b.js:1:1
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

@adalinesimonian

This comment has been minimized.

@targos
Copy link
Member

targos commented Aug 1, 2021

The error you see with Jest was fixed in #39593

@adalinesimonian
Copy link
Author

adalinesimonian commented Aug 1, 2021

The error you see with Jest was fixed in #39593

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.

@adalinesimonian adalinesimonian 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 Aug 1, 2021
@aduh95 aduh95 added the repl Issues and PRs related to the REPL subsystem. label Aug 1, 2021
@csmart29
Copy link

csmart29 commented Aug 2, 2021

git://github.com/creationix/nvm.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants