question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next standalone build does not work with Yarn 3 workspaces

See original GitHub issue

Verify 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

10reactions
CedricArtiguecommented, Jul 14, 2022

I have the exact same problem, with the following setup

  • yarn 3.2.0 monorepo with a package containing a nextjs app with next version 12.2.2.
  • pnp nodeLinker
  • standalone mode in next.config.js

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 ?

3reactions
guiyom-ecommented, May 25, 2022

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 frontend package.json to avoid hoisting (see this issue):

  "installConfig": {
    "hoistingLimits": "workspaces"
  }

This worked with outputStandalone option activated or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Workspaces | Yarn - Package Manager
An in-depth guide to Yarn's workspaces, a feature that provides an easy way to store multiple packages inside the same project.
Read more >
Nextjs deploy on vercel with yarn workspaces is not working
I'm trying to deploy my NextJs app, which is inside a yarn workspace package on my codebase, but I'm getting a build error....
Read more >
CLI Reference - Turborepo
Turborepo is a high-performance build system for JavaScript and TypeScript ... Run npm scripts across all workspaces in specified scope.
Read more >
Simple monorepos via npm workspaces and TypeScript ...
json that we need to make project references work. Alas, we can't add them to tsconfig.json itself because then it wouldn't work in...
Read more >
Upgrade to Yarn >2 with (or without) Plug'n'Play | by Julien ...
At Inato, we upgraded most of our repositories to Yarn 3 that way without incidence on ... No problem, yarn has everything you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found