尝试向现有 NextJS 项目添加对 Docker 的支持,但收到“stat app/.next/standalone:文件不存在”错误

ewa*_*ang 15 typescript docker google-cloud-platform next.js

我按照本指南将 Docker 支持添加到我现有的 NextJS + TypeScript 项目并部署到 Google Cloud Run: https: //github.com/vercel/next.js/tree/canary/examples/with-docker

但是,构建容器步骤:

docker build -t nextjs-docker .
Run Code Online (Sandbox Code Playgroud)

不断失败,因为它给我一个错误“stat app/.next/standalone:文件不存在”错误。

我查看了 .next 文件夹,没有生成独立文件,这就是我收到该错误的原因。如何创建这个 .next/standalone 文件?

我的 next.config.js 文件如下所示:

module.exports = {
  eslint: {
    ignoreDuringBuilds: true,
  },
  experimental: {
    outputStandalone: true
  }
}
Run Code Online (Sandbox Code Playgroud)

我的 Dockerfile 如下所示:

# Install dependencies only when needed
FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json package-lock.json tsconfig.json ./ 
RUN npm ci

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

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

# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

# Automatically leverage output traces to reduce image size 
# https://nextjs.org/docs/advanced-features/output-file-tracing

# Following line is giving me the error
# NOTE: I can not just comment out this line because it will give an error later that "Cannot find module '/app/server.js'"
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
Run Code Online (Sandbox Code Playgroud)

我的 .next 文件夹如下所示:

- .next
  - cache
  - server
  - static
  - traces
  - BUILD_ID
  - build-manifest.json
  - export.marker.json
  - images-manifest.json
  - prerender-manifest.json
  - react-loadasble-manifest.json
  - required-server-files.json
  - routes-manifest.json
  - trace
Run Code Online (Sandbox Code Playgroud)

我的理解是添加:

experimental: {
  outputStandalone: true
}
Run Code Online (Sandbox Code Playgroud)

到 next.config.js 文件,然后运行npm run build将创建 .next/standalone 文件,但似乎不起作用。

Sun*_*ota 18

我解决了在下一个配置文件中添加独立输出的问题

filename : next.config.js
Run Code Online (Sandbox Code Playgroud)

代码:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: false,
  swcMinify: true,
  output: "standalone",
};
Run Code Online (Sandbox Code Playgroud)

  • 这应该在 nextJs 配置中标准化。 (2认同)

ewa*_*ang 8

如果这对其他人有帮助,结果是我的“下一个”版本设置为“^11.1.0”,而独立文件夹仅适用于“下一个”版本“^12.1.0”及更高版本。更新我的 package.json 解决了问题!