分离测试、prod和集成测试(Nextj.js、cypeess)之间的依赖关系,以避免安装不必要的包



当前我正在运行一个漫长的安装过程,因为柏树是我的开发依赖项,当我构建以下docker映像时:

# Install dependencies only when needed
FROM node:14.8.0-alpine3.12 AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN apk add git
RUN npm cache clean -f
RUN npm cache verify
RUN npm install 
RUN mkdir /app/.next

# Rebuild the source code only when needed
FROM node:14.8.0-alpine3.12 AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm cache clean -f
RUN npm cache verify
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
# Production image, copy all the files and run next
FROM node:14.8.0-alpine3.12 AS runner
WORKDIR /app
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/.env.production ./

USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["npm", "start"]

安装过程需要一段时间,因为柏树需要很长时间。产品图像不需要Cypress。测试图像不需要它。集成测试需要它。

有没有什么方法可以在package.json中拆分它,这样它就不需要在其他场景中安装,或者有一种不同的方法可以避免在不必要的情况下安装它?

尝试在安装中使用--Prod标志或env变量

npm run install --Prod

使用--production标志(或者当NODE_ENV环境变量设置为production时(,npm将不会安装devDependencies中列出的模块。当NODE_ENV环境变量设置为production时,要安装dependencies和devDependencies中列出的所有模块,可以使用--production=false。

https://docs.npmjs.com/cli/v8/commands/npm-install#:~:text=使用%20%2D%2D制作,使用%20%2D制作%3Dfalse。

最新更新