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

Build fails with experimental ESM loader #31279

Closed
kherock opened this issue Nov 11, 2021 · 4 comments
Closed

Build fails with experimental ESM loader #31279

kherock opened this issue Nov 11, 2021 · 4 comments
Labels
bug Issue was opened via the bug report template.

Comments

@kherock
Copy link

kherock commented Nov 11, 2021

What version of Next.js are you using?

12.0.3

What version of Node.js are you using?

16.13.0

What browser are you using?

N/A

What operating system are you using?

macOS

How are you deploying your application?

N/A

Describe the Bug

When using Next.js with Yarn 3's experimental ESM loader enabled, builds will hang for a while and eventually fail. The build process's dependence on jest-worker is causing builds to fail when --experimental-loader is passed as a Node runtime argument. There isn't much that Next.js can do to fix this, but I just wanted to bring attention to this issue since PnP users will otherwise have to explicitly opt-out of ESM support starting in Yarn v3.2 and Next.js 12.

I've opened an issue on jest-worker tracked here: jestjs/jest#12060

I want to make it clear that this is not a Yarn/PnP bug. This is probably a Node.js quirk with how they've implemented their ESM loader hook.

Expected Behavior

The app should build a starter project without errors with Yarn 3 while PnP is enabled.

To Reproduce

A reproduction repo is available here: https://github.com/kherock/jest-worker-spawn-bug

Please check out the README there for more context.

$ yarn install
$ yarn build-with-loader
node:internal/validators:119
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received null
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)
    at Module.require (node:internal/modules/cjs/loader:998:3)
    at require (node:internal/modules/cjs/helpers:102:18)
    at execMethod (/Users/herockk/Workspaces/jest-worker-spawn-bug/node_modules/jest-worker/build/workers/processChild.js:107:16)
    at process.messageListener (/Users/herockk/Workspaces/jest-worker-spawn-bug/node_modules/jest-worker/build/workers/processChild.js:39:7)
    at process.emit (node:events:390:28)
    at emit (node:internal/child_process:917:12)
    at processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'ERR_INVALID_ARG_TYPE'
}
@kherock kherock added the bug Issue was opened via the bug report template. label Nov 11, 2021
@kherock
Copy link
Author

kherock commented Nov 11, 2021

I've just realized that we can use worker_threads as a workaround, it doesn't have this timing issue. Next.js already supports passing this to jest-worker with experimental: { workerThreads: true } in next.config.js. Worker threads are available in all maintained Node versions:

https://nodejs.org/docs/latest-v12.x/api/worker_threads.html

Jest also implements a fallback in case the current environment doesn't support them.

@sidhuko
Copy link

sidhuko commented Mar 18, 2022

Maybe a FAQ, I believe it should work as normal if you set pnpEnableEsmLoader: false in your .yarnrc.yml file.

@kherock
Copy link
Author

kherock commented Mar 18, 2022

Maybe a FAQ, I believe it should work as normal if you set pnpEnableEsmLoader: false in your .yarnrc.yml file.

I depend on ESM packages, this isn't an option for me. However this issue is fixed for me with Node 16.14, so I'm going to close this.

@kherock kherock closed this as completed Mar 18, 2022
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants