KEMBAR78
Dependencies missing in standalone build · Issue #50072 · vercel/next.js · GitHub
Skip to content

Dependencies missing in standalone build #50072

@Adelrisk

Description

@Adelrisk

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 #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:

  • @swc/helpers/_/_interop_require_default
  • @swc/helpers/_/_interop_require_wildcard

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

node server.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions