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

Error after upgrading to Node 16.6.0 when using ES modules #11708

Closed
adalinesimonian opened this issue Jul 30, 2021 · 10 comments
Closed

Error after upgrading to Node 16.6.0 when using ES modules #11708

adalinesimonian opened this issue Jul 30, 2021 · 10 comments

Comments

@adalinesimonian
Copy link

🐛 Bug Report

Calling jest gives the error:

TypeError: String.prototype.startsWith called on null or undefined
    at startsWith (<anonymous>)
    at node:internal/errors:811:19
    at Array.filter (<anonymous>)
    at node:internal/errors:809:16
    at prepareStackTrace (node:internal/errors:96:12)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:38)
    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)

To Reproduce

Steps to reproduce the behavior:

  1. Install Node.js 16.6.0 (versions 16.5.0– are not affected)
  2. Create a project using ES modules ("type": "module" in package.json) and install jest ^27.0.6
  3. Create a jest.config.js config file with a default export.
  4. Run jest (npx jest, yarn jest, etc.)

Expected behavior

Jest runs without errors.

Link to repl or repo (highly encouraged)

https://github.com/adalinesimonian/node-jest-16.6-error-repro

envinfo

  System:
    OS: Linux 5.4 Fedora Remix for WSL 34
    CPU: (32) x64 AMD Ryzen 9 3950X 16-Core Processor
  Binaries:
    Node: 16.6.0 - ~/.volta/tools/image/node/16.6.0/bin/node
    Yarn: 3.0.0 - ~/.volta/tools/image/yarn/1.22.11/bin/yarn
    npm: 7.20.3 - ~/.volta/tools/image/npm/7.20.3/bin/npm
  npmPackages:
    jest: ^27.0.6 => 27.0.6 
@sigveio
Copy link
Contributor

sigveio commented Jul 31, 2021

Thanks for posting clear steps to reproduce and a repo link, @adalinesimonian

I just tested and can confirm what you are seeing.

The "ESM mode" of Jest (activated when you for example set "type": "module" in package.json) is still very much incomplete/experimental and has a lot of known and unknown issues. You can read more about it on this page, and in the tracking issue here.

If you'd have the time to dig deeper and try to find out what might be causing this particular error, I'm sure the help would be appreciated =)

@adalinesimonian
Copy link
Author

adalinesimonian commented Aug 1, 2021

If you'd have the time to dig deeper and try to find out what might be causing this particular error, I'm sure the help would be appreciated =)

The error happens in this part of Node's internal code (internal/errors):

image

Going up the stack, this is the line in jest-util/build/requireOrImportModule.js:

image

frm.getFileName() is returning null which causes the TypeError when startsWith is called. Since this error is not an ERR_REQUIRE_ESM error, Jest throws it back. It's not entirely clear that this is Jest's fault since Jest's logic makes sense. Additionally, the path passed to require() is valid — in this case, it is /home/adaline/code/node-jest-16.6-error-repro/jest.config.js, which is a valid path to an existing file without any permissions issues. I think this might be a regression in Node introduced in 16.6.0.

@adalinesimonian
Copy link
Author

I verified the change in behaviour with an even simpler test case.

  1. Create a package.json file with "type": "module"
  2. Create a JS file — it doesn't even need to have any contents.
  3. Try to require() the file with node REPL or in a script.

In Node.js 16.5.0 and prior, the error given is:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/adaline/code/node-jest-16.6-error-repro/test-mod.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'
}

However, in 16.6.0 it is:

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'
}

There is most certainly a regression in 16.6.0. I'll report this on the Node repository and link this issue.

@adalinesimonian
Copy link
Author

Reported in nodejs/node#39618

@adalinesimonian
Copy link
Author

Fix landed in nodejs/node#39593 — however, it is unclear to me when the release containing this fix will come out. Hopefully soon.

@sigveio
Copy link
Contributor

sigveio commented Aug 1, 2021

Good job tracing it down nonetheless, @adalinesimonian 🙌

@adalinesimonian
Copy link
Author

No problem, happy to help. Should I keep this issue open until the fix is released or close it now?

@sigveio
Copy link
Contributor

sigveio commented Aug 2, 2021

Looks like they are preparing to release tomorrow as it apparently had quite widespread impact. So I'd just leave it open until then

@adalinesimonian
Copy link
Author

Verified issue is fixed with Node.js 16.6.1. Closing issue!

@github-actions
Copy link

github-actions bot commented Sep 4, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants