3

我正在尝试使用 NextJS 独立模式和 PNPM 创建生产 Dockerfile。

next.config.js我有输出配置:

  experimental: {
    outputStandalone: true,
    outputFileTracingRoot: path.join(__dirname, '../../'),
  },

在 Dockerfile 我使用多阶段构建:

# BUILD STEP
FROM node:16.13-alpine as landing-builder
WORKDIR /dml-sdk

COPY ./pnpm-workspace.yaml .
COPY ./pnpm-lock.yaml .
COPY ./tsconfig.base.json .
COPY ./package.json .

COPY ./apps/landing ./apps/landing

RUN apk --no-cache add curl
RUN apk --no-cache add git
RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm@6
RUN pnpm config set store-dir .pnpm-store

RUN pnpm i
RUN pnpm build

# RUNNER STEP
FROM node:16.13-alpine as landing-runner
WORKDIR /dml-sdk

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

COPY --from=landing-builder /dml-sdk/apps/landing/public ./public
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/apps/landing/.next/standalone ./
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/node_modules ./node_modules

ENV NEXT_TELEMETRY_DISABLED 1
ENV PORT 3010

EXPOSE 3010

WORKDIR /dml-sdk/apps/landing

CMD ["node", "server.js"]

由于某种原因,我遇到了错误:

landing    | Error: Cannot find module 'next/dist/server/next-server'
landing    | Require stack:
landing    | - /powerplay-sdk/apps/landing/server.js

看起来下一个服务器没有捆绑在standalone目录中,并且行:

const NextServer = require('next/dist/server/next-server').default

链接到根 node_modules,而不是/dml-sdk/standalone/apps/landing/node_modules

node_modules/.pnpm/next@12.0.8_react-dom@17.0.2+react@17.0.2/node_modules/next/dist/server/next-server.d.ts

是否可以在 BUILD 步骤中禁用下一个服务器链接,以便standalonenode_modules 包含next-server

4

0 回答 0