next.js: Dependencies missing in standalone build

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Debian 6.1.27-1 (2023-05-08)
    Binaries:
      Node: 18.13.0
      npm: 6.14.18
      Yarn: 3.5.1
      pnpm: N/A
    Relevant packages:
      next: 13.4.3
      eslint-config-next: 13.4.3
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

https://github.com/Adelrisk/reproducer_nextjs_standalone_require

To Reproduce

Prepare a project

Prepare without the reproducer
npx create-next-app@latest --ts --eslint --use-yarn next-docker-problem
# Answer all the questions with 'no', these are:
# ✔ Would you like to use Tailwind CSS with this project? … No / Yes
# ✔ Would you like to use `src/` directory with this project? … No / Yes
# ✔ Use App Router (recommended)? … No / Yes
# ✔ Would you like to customize the default import alias? … No / Yes
cd next-docker-problem/
# OPTIONAL: for first-time users of yarn > v2
corepack prepare yarn@stable --activate
# Configure yarn, currently 3.5.1
yarn set version stable
# Configure nextjs to produce a standalone-application
cat > next.config.js << EOF
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  output: 'standalone',
  poweredByHeader: false,
}
module.exports = nextConfig
EOF
# OPTIONAL: set affected version of next to canary
yarn add next@canary
Prepare with the reproducer
yarn install

Reproduce the problem

Without docker:
export NEXT_TELEMETRY_DISABLED=1
yarn next build
cd .next/standalone/
# Copy the node_modules, because node won't resolve the yarn packaging
cp -r .yarn/cache/next*/node_modules ./
cp -r ../static .next
# Start the server. (This command fails.)
node server.js
With docker:

Add the following file to the directory:

FROM node:18-alpine AS base

FROM base AS deps
RUN apk add --no-cache dumb-init libc6-compat

WORKDIR /app

RUN corepack enable
# Install dependencies based on the preferred package manager
COPY .yarn ./.yarn/
COPY ./package.json ./
COPY package.json yarn.lock .yarnrc.yml ./

RUN yarn install

# 2. Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app

ENV NEXT_TELEMETRY_DISABLED=1

COPY --from=deps /app/.yarn/ ./.yarn
COPY package.json yarn.lock .yarnrc.yml ./
COPY . ./

RUN yarn install
RUN yarn build

# 3. Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production

COPY --from=deps /usr/bin/dumb-init /usr/bin/dumb-init

# Make sure we have the latest security updates
RUN apk --no-cache -U upgrade

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# COPY --chown=nextjs:nodejs ./fake_modules/ ./node_modules/
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone/server.js ./
COPY --from=builder --chown=nextjs:nodejs /app/public* ./public/
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone/.yarn/cache/*/node_modules* ./node_modules/
/*/node_modules* ./node_modules/
COPY --from=builder --chown=nextjs:nodejs /app/.next/static* ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

# CMD ["dumb-init", "yarn", "start:custom"]
CMD ["dumb-init", "yarn", "node", "server.js"]
docker build --file Dockerfile -t temp_next_broken .
docker run -it --rm=true --name nextjs_test -p 3000:3000 temp_next_broken

Describe the Bug

One-time error with latest (not canary)

When I started to document the steps to reproduce the error, it was identical to https://github.com/vercel/next.js/issues/48173

However, after using canary, I couldn’t reproduce the error, even after downgrading.

Error with canary/latest version

The error with docker

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

Error: Cannot find module 'styled-jsx'
Require stack:
- /app/node_modules/next/dist/server/require-hook.js
- /app/node_modules/next/dist/server/lib/render-server.js
- /app/node_modules/next/dist/compiled/jest-worker/processChild.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.resolve (node:internal/modules/cjs/helpers:116:19)
    at Object.<anonymous> (/app/node_modules/next/dist/server/require-hook.js:30:17)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/app/node_modules/next/dist/server/lib/render-server.js:38:1) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/node_modules/next/dist/server/require-hook.js',
    '/app/node_modules/next/dist/server/lib/render-server.js',
    '/app/node_modules/next/dist/compiled/jest-worker/processChild.js'
  ]
}

The error without docker

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

Error: Cannot find module 'styled-jsx'
Require stack:
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/require-hook.js
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/lib/render-server.js
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/compiled/jest-worker/processChild.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
    at Function.resolve (node:internal/modules/cjs/helpers:109:19)
    at Object.<anonymous> (/home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/require-hook.js:30:17)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/lib/render-server.js:38:1) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/require-hook.js',
    '/home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/lib/render-server.js',
    '/home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/compiled/jest-worker/processChild.js'
  ]
}

Node.js v18.13.0

Expected Behavior

I would expect the MVP app to run without problems.

Additionally, I notice that the script node_modules/next/dist/server/require-hook.js attempts to require many other (not available) third party dependencies:

  • react/jsx-runtime
  • react/jsx-dev-runtime

Once I faked all these dependencies, additional require calls fail because these dependencies are missing:

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

node server.js

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 25
  • Comments: 15

Most upvoted comments

Hello, I have a similar issue with the latest version of nextjs v13.4.5: Error: Cannot find module ‘next/dist/compiled/jest-worker’

The lib is not included in .next/standalone/node-modules when builded with : npm ci for production dependencies only.

pnpm uses sym linking when creating the modules so you might run into issues like this of missing modules.

Either follow this https://pnpm.io/docker

or use node-linker=hoisted in .npmrc to return it to a flat structure no link https://pnpm.io/npmrc#node-linker

I assume yarn has a similar thing

I have a similar issue in standalone mode :

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

Error: Cannot find module 'next/dist/server/lib/start-server'
Require stack:
- /usr/src/app/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/usr/src/app/server.js:4:25)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/usr/src/app/server.js' ]
}

The file is present in my local node_modules but not in .next/standalone/node_modules