提问人:Dooomel 提问时间:1/28/2022 更新时间:9/29/2023 访问量:2210
PNPM 与独立 NextJS 构建链接
PNPM linking with standalone NextJS build
问:
我正在尝试使用 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/[email protected][email protected][email protected]/node_modules/next/dist/server/next-server.d.ts
是否可以在 BUILD 步骤中禁用下一个服务器链接,以便node_modules包括?standalone
next-server
答:
0赞
Ryabchenko Alexander
9/29/2023
#1
I gess 问题
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/apps/landing/.next/standalone ./
它会将文件/文件夹从 复制到您的 workdir。所以你得到.
试试下一步standalone
WORKDIR /dml-sdk
/dml-sdk/standalone
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/apps/landing/.next/standalone ./apps/landing/.next/standalone
或
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/apps ./apps
或
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/apps/landing ./apps/landing
评论
COPY --from=landing-builder --chown=nextjs:nodejs /dml-sdk/node_modules ./node_modules