NextJS独立构建在Docker中无法生成.next/standalone目录
NextJS 14.2.3 Docker构建时缺失.next/standalone目录问题解决
我用Docker构建启用standalone输出的NextJS项目(next版本^14.2.3),之前本地和容器内构建都正常,但最近Docker构建时生成的.next目录里没有standalone/server.js,导致后续COPY命令失败,本地执行相同构建命令却能正常生成该目录。以下是相关配置文件及构建错误输出,求解决办法:
相关配置文件
next.config.js
const withNextIntl = require("next-intl/plugin")(); module.exports = withNextIntl({ // TODO: remove eventually if found error with useTranslation and TS output: "standalone", typescript: { ignoreBuildErrors: true, }, });
docker-compose.yml
nextapp: build: context: ./ dockerfile: ./docker/nextjs/Dockerfile_prod args: - DATABASE_URL=mysql://root:${DB_ROOT_PASSWORD}@db:3306/${DB_DATABASE} - UID=${UID} - GID=${GID} restart: unless-stopped environment: - NODE_ENV=production - NEXTAUTH_URL=${NEXTAUTH_URL} - DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}@db:3306/${DB_DATABASE} - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID} - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET} - GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID} - GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET} - AUTH_SECRET=${SECRET} ports: - "127.0.0.1:3000:3000" # keep the IP exact, otherwise it overrides the firewall depends_on: db: condition: service_healthy goapp: condition: service_started
Dockerfile_prod
FROM node:alpine AS base ### Dependencies ### FROM base AS builder # allow prisma to use the DATABASE_URL env variable WORKDIR /app # fix `cannot find module server.js` ENV NEXT_PRIVATE_STANDALONE=true COPY ./frontend-next ./frontend-next/package.json ./frontend-next/yarn.lock ./frontend-next/localazy.json ./frontend-next/prisma ./ # fix for "Caching failed for pack" error RUN rm -rf node_modules # Install system dependencies RUN apk add --no-cache libc6-compat tzdata git openssh-client && \ yarn config set registry https://registry.npmjs.org && \ yarn --force --frozen-lockfile && \ yarn global add typescript next react react-dom prisma @localazy/cli RUN npx prisma generate && \ localazy download && \ yarn build && \ pwd && ls -la ./.next/ && ls -la ./.next/standalone
构建错误输出
ERROR [nextapp builder 5/5] RUN npx prisma generate && localazy download && yarn build && pwd && ls -la ./.next/ && ls -la ./.next/standalone 22.8s ------ > [nextapp builder 5/5] RUN npx prisma generate && localazy download && yarn build && pwd && ls -la ./.next/ && ls -la ./.next/standalone: 0.848 (node:7) ExperimentalWarning: CommonJS module /usr/local/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /usr/local/lib/node_modules/npm/node_modules/supports-color/index.js using require(). 0.848 Support for loading ES Module in require() is an experimental feature and might change at any time 0.848 (Use `node --trace-warnings ...` to show where the warning was created) # ... 3.759 $ next build 4.837 Attention: Next.js now collects completely anonymous telemetry regarding usage. 4.837 This information is used to shape Next.js' roadmap and prioritize features. 4.837 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: 4.837 https://nextjs.org/telemetry 4.837 4.923 ▲ Next.js 14.2.18 4.923 5.158 Creating an optimized production build ... 7.183 (node:97) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. 7.183 (Use `node --trace-deprecation ...` to show where the warning was created) 22.57 <w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies 22.75 Done in 19.07s. 22.77 /app 22.77 total 1076 22.77 drwxrwxr-x 1 root root 4096 Nov 15 10:50 . 22.77 drwxr-xr-x 1 root root 4096 Nov 15 10:19 .. 22.77 drwxrwxr-x 4 root root 4096 Nov 15 09:14 cache 22.77 -rw-r--r-- 1 root root 20 Nov 15 10:50 package.json 22.77 -rw-r--r-- 1 root root 3895 Nov 15 10:50 routes-manifest.json 22.77 drwxr-xr-x 5 root root 4096 Nov 15 10:50 server 22.77 drwxr-xr-x 3 root root 4096 Nov 15 10:50 static 22.77 -rw-r--r-- 1 root root 1057686 Nov 15 10:50 trace 22.77 drwxr-xr-x 3 root root 4096 Nov 15 10:50 types 22.77 ls: ./.next/standalone: No such file or directory ------ failed to solve: process "/bin/sh -c npx prisma generate && localazy download && yarn build && pwd && ls -la ./.next/ && ls -la ./.next/standalone" did not complete successfully: exit code: 1
解决方案
修复COPY命令路径问题
当前Dockerfile的COPY命令写法错误,COPY ./frontend-next ./frontend-next/package.json ... ./会把整个目录和单个文件混拷,导致项目结构混乱,Next.js无法读取正确配置。改成完整复制项目目录:COPY ./frontend-next ./或者优化为分层构建,先装依赖再拷代码:
COPY ./frontend-next/package.json ./frontend-next/yarn.lock ./ RUN yarn install --frozen-lockfile COPY ./frontend-next ./移除内部测试环境变量
NEXT_PRIVATE_STANDALONE是Next.js内部测试用变量,你已经在next.config.js中设置了output: "standalone",无需额外配置,直接删除该行:# ENV NEXT_PRIVATE_STANDALONE=true统一Next.js版本
错误输出显示构建用的是Next.js 14.2.18,但你的package.json锁定的是^14.2.3,版本差异可能导致插件(next-intl)与框架不兼容,进而影响standalone生成。可以在package.json中锁定Next.js版本为14.2.3,或者升级next-intl到兼容14.2.18的版本。清理Docker缓存
旧缓存可能导致构建状态异常,执行以下命令清理缓存后重新构建:docker-compose build --no-cache nextapp
内容的提问来源于stack exchange,提问作者sjiamnocna
相关产品推荐
相关产品推荐

