Next standalone build does not work with Yarn 3 workspaces
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Next.js v12.1.6-canary.5 (also tried against 12.1.5)
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
I am using yarn 3.2.0 and workspaces, where a next.js project is one package, with dependencies on others in the same monorepo. For development and a regular next build, this works as expected, but I am unable to use standalone.
I have attempted a few different combinations of settings. When using nodeLinker: node-modules
I am getting build errors as next is unable to find files in the local node_modules
(which does not exist, only the one at the top of the monorepo does):
Error occurred prerendering page "[...]". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Cannot find module '[...]/mypackage/node_modules/react/jsx-runtime.js'
at createEsmNotFoundErr (node:internal/modules/cjs/loader:960:15)
at finalizeEsmResolution (node:internal/modules/cjs/loader:953:15)
at resolveExports (node:internal/modules/cjs/loader:482:14)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.997 ([...]/mypackage/dist/server/pages/_app.js:32:18)
at __webpack_require__ ([...]/mypackage/dist/server/webpack-runtime.js:25:43)
I have attempted to use the pnpm linker instead, which does put a node_modules directory in each package (but with symlinks rather than a fully copy of all dependencies).
In this setup, I can get it to build without errors. However, if I run the build twice, I receive an error:
[Error: EEXIST: file already exists, symlink '../../../../react-npm-18.0.0-fdbcb4c477/node_modules/react' -> '[...]/mypackage/node_modules/.store/react-dom-virtual-d71fc3884c/node_modules/react-dom/node_modules/react'] {
errno: -17,
code: 'EEXIST',
syscall: 'symlink',
path: '../../../../react-npm-18.0.0-fdbcb4c477/node_modules/react',
dest: '[...]/mypackage/node_modules/.store/react-dom-virtual-d71fc3884c/node_modules/react-dom/node_modules/react'
I’m not sure why symlinks are getting created inside my node_modules, but this can be resolved by deleting node_modules/.store
When starting the server from the standalone directory as built with pnpm compatible node_modules, I do get a “Listening on port …” but later get errors as the server cannot find dependencies
Error: Cannot find module '[...]/mypackage/dist/standalone/node_modules/react/jsx-runtime.js'
The node_modules directory in my standalone output folder contains a link to react:
react -> ../../../node_modules/.store/react-npm-18.0.0-fdbcb4c477/node_modules/react
This does in fact exist, but contains no jsx-runtime.js.
Other dependencies in my output node_modules contain symlinks to node_modules/.store that do not exist at all.
Expected Behavior
next build with standalone mode configured correctly produces the artifacts required to run the server with yarn 3 workspaces
To Reproduce
- Create a yarn 3 style monorepo with a workspace
foo
containing a next js project, using pnpm as node linker - Configure the next project to build with standalone output
- Run next build
- Attempt to run the produced standalone server
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6
Top GitHub Comments
I have the exact same problem, with the following setup
After running yarn next build in standalone mode, I don’t have any node_modules inside my standalone output folder but instead a partial and unzipped copy of my root level .yarn/cache.
When running node path/to/server.js I’f facing the issue => Error: Cannot find module ‘next/dist/server/next-server’
When running yarn node path/to/server.js, server is running because of root level .pnp.cjs resolution. But As I want to dockerize my server with build outside docker, this is an issue.
Did you guy managed to fix the above problem and get a node_module inside next app package standalone folder with yarn3 and PnP ?
I installed Next.js v12.1.6 with Yarn 3.1.1 (
nodeLinker: node-modules
) and I reproduced your first issue “react/jsx-runtime.js not found”. I fixed it, by adding a custom configuration to frontendpackage.json
to avoid hoisting (see this issue):This worked with
outputStandalone
option activated or not.