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 4 years ago
  • Reactions: 37
  • Comments: 21 (1 by maintainers)

Most upvoted comments

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

@0xbid Just as a reminder, Next.js is a commercial project by Vercel, which is a company that offers web hosting. I don’t think this is at the top of their priorities since it would make self-hosting much easier, reducing their profit. 😕

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.

Hi everyone,

Managing environment variables can sometimes be tricky, especially during builds. To help with this, we created the “stop-nagging” tool. It can streamline managing environment variables and reduce noise, which might be useful in cases like this. Feel free to check it out!

stop-nagging

What is not a feasible workaround: individually unplugging the missing dependencies and copying them into the standalone directory. Such as:

# After running `yarn next build` as declared above, copy in the source of `styled-jsx` 

# Add styled-jsx to the project (otherwise yarn will not unplug it)
yarn add styled-jsx 
# Create unzipped copy of source of the dependency
yarn unplug styled-jsx
# Copy the dependency into the standalone application
cp -r .yarn/unplugged/styled-jsx*/node_modules .next/standalone

Repeat for: react, @swr/helpers, @next/env, react-dom

The server will run, but opening the page with the browser looks like this:

Listening on port 3000 url: http://localhost:3000
Error: Cannot find module 'next/dist/server/get-page-files.js'
Require stack:
- /home/workdir/next-docker-problem/.next/standalone/.next/server/pages/_document.js
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/require.js
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/next-server.js
- /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/next.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 /home/workdir/next-docker-problem/.next/standalone/node_modules/next/dist/server/require-hook.js:180:36
    at Module._load (node:internal/modules/cjs/loader:885:27)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at 140 (/home/workdir/next-docker-problem/.next/standalone/.next/server/pages/_document.js:916:18)
    at __webpack_require__ (/home/workdir/next-docker-problem/.next/standalone/.next/server/webpack-runtime.js:25:42)
    at 632 (/home/workdir/next-docker-problem/.next/standalone/.next/server/pages/_document.js:44:23)
    at __webpack_require__ (/home/workdir/next-docker-problem/.next/standalone/.next/server/webpack-runtime.js:25:42)
    at 35 (/home/workdir/next-docker-problem/.next/standalone/.next/server/pages/_document.js:907:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [

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

This is the solution when attempting to use the standalone directory for AWS Lambda deployments. Nice find!

I am having the same issue while trying to use @newrelic/next dependency.

(next 13.5.1)

I build with standalone and copy the relevant assets to docker build, occurring the same error: Error: Cannot find module 'styled-jsx/package.json

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

Error: Cannot find module 'styled-jsx/package.json'
Require stack:
- /app/node_modules/next/dist/server/require-hook.js
- /app/node_modules/next/dist/server/next.js
- /app/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at resolve (node:internal/modules/helpers:136:19)
    at Object.<anonymous> (/app/node_modules/next/dist/server/require-hook.js:38:32)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/app/node_modules/next/dist/server/next.js:25:1) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/node_modules/next/dist/server/require-hook.js',
    '/app/node_modules/next/dist/server/next.js',
    '/app/server.js'
  ]
}

The procedures I followed:

  1. Locally build next with standalone mode and specify distDir: 'build' in next.config.mjs
pnpm next build
  1. Copy relevant assets to docker folder
cp -r public build/standalone/* Dockerfile docker

The simple Dockerfile (just copy assets in docker folder and start server.js):

# syntax=docker/dockerfile:1

FROM node:20.6-slim

WORKDIR /app

COPY . .

# Define Nextjs custom server port
ENV PORT 8000
EXPOSE 8000

CMD ["node","server.js"]
  1. Docker build (cd above docker folder) and run:
docker buildx build -t demo --load .
docker run -it --rm -p 8000:3000 --name demo demo
  1. Access http://localhost:8000 . The above error occurs and container stop.

I took a look at the error file build/standalone/node_modules/next/dist/server/require-hook.js and found the source:

const defaultOverrides = {
    "styled-jsx": path.dirname(resolve("styled-jsx/package.json")),
    "styled-jsx/style": resolve("styled-jsx/style")
};

It might be related to pnpm module path. I don’t know.

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