Skip to content

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

Open
@adalinesimonian

Description

@adalinesimonian

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions