vercel / vercel/next.js

Missing dependencies when using standalone output with pnpm 8

Open
#48,017 18 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Output
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.19.1
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: 8.1.0
    Relevant packages:
      next: 13.2.5-canary.32
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)

Standalone mode (output: "standalone")

Link to the code that reproduces this issue

https://github.com/Bubbleinpit/nextjs13-standalone-with-pnpm8

To Reproduce

You can follow this README of the repo above to reproduce the issue. For convenience, I will also describe it here.

  1. execute next build and copy all necessary files to output dir
pnpm build

[ -d "build" ] && rm -r build
mkdir build

cp -r ./public ./build
cp -r ./.next/standalone/* ./build
cp -r ./.next/standalone/.next ./build
cp -r ./.next/static ./build/.next
  1. move output dir to another place and execute node server.js in it
mv ./build ~/build
cd ~/build
node ./server.js

Then you will see:

node:internal/modules/cjs/loader:1024
  throw err;
  ^

Error: Cannot find module 'styled-jsx'
Require stack:
- /Users/username/build/node_modules/next/dist/build/webpack/require-hook.js
- /Users/username/build/node_modules/next/dist/server/initialize-require-hook.js
- /Users/username/build/node_modules/next/dist/server/next-server.js
- /Users/username/build/server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1021:15)
    at Function.resolve (node:internal/modules/cjs/helpers:114:19)
    at Object.loadRequireHook (/Users/username/build/node_modules/next/dist/build/webpack/require-hook.js:38:21)
    at Object.<anonymous> (/Users/username/build/node_modules/next/dist/server/initialize-require-hook.js:3:19)
    at Module._compile (node:internal/modules/cjs/loader:1191:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
    at Module.load (node:internal/modules/cjs/loader:1069:32)
    at Function.Module._load (node:internal/modules/cjs/loader:904:12)
    at Module.require (node:internal/modules/cjs/loader:1093:19)
    at require (node:internal/modules/cjs/helpers:108:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/username/build/node_modules/next/dist/build/webpack/require-hook.js',
    '/Users/username/build/node_modules/next/dist/server/initialize-require-hook.js',
    '/Users/username/build/node_modules/next/dist/server/next-server.js',
    '/Users/username/build/server.js'
  ]
}
Describe the Bug

I'm using pnpm 8 and nextjs 13. And I turn on the appDir. I set the output mode as standalone to build the code on my dev computer and to deploy it on a prod server.

When I try to execute the server.js, it always says Error: Cannot find module 'xxx'.

node:internal/modules/cjs/loader:1024
  throw err;
  ^

Error: Cannot find module 'styled-jsx'
Require stack:
- /Users/username/build/node_modules/next/dist/build/webpack/require-hook.js
- /Users/username/build/node_modules/next/dist/server/initialize-require-hook.js
- /Users/username/build/node_modules/next/dist/server/next-server.js
- /Users/username/build/server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1021:15)
    at Function.resolve (node:internal/modules/cjs/helpers:114:19)
    at Object.loadRequireHook (/Users/username/build/node_modules/next/dist/build/webpack/require-hook.js:38:21)
    at Object.<anonymous> (/Users/username/build/node_modules/next/dist/server/initialize-require-hook.js:3:19)
    at Module._compile (node:internal/modules/cjs/loader:1191:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
    at Module.load (node:internal/modules/cjs/loader:1069:32)
    at Function.Module._load (node:internal/modules/cjs/loader:904:12)
    at Module.require (node:internal/modules/cjs/loader:1093:19)
    at require (node:internal/modules/cjs/helpers:108:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/username/build/node_modules/next/dist/build/webpack/require-hook.js',
    '/Users/username/build/node_modules/next/dist/server/initialize-require-hook.js',
    '/Users/username/build/node_modules/next/dist/server/next-server.js',
    '/Users/username/build/server.js'
  ]
}

Now I have to reinstall the dependencies in the output dir on my prod server to solve this issue. This is too much of a waste of time.

And I am sure that if I switch to use npm as the package manager, the issue will go away. I verified that.

Expected Behavior

I can start the server successfully with the steps to reproduce.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction repository and its README, run pnpm build, then launch the copied output with node ./server.js to confirm the missing styled-jsx dependency. Trace the standalone output under .next/standalone and its generated server.js; done means the copied output starts successfully without reinstalling dependencies.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.