1

Currently I am running into long installation process because cypress is in my dev dependencies, and when I build the following docker image:

# 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"]

The installation process takes a while because cypress takes a long time. Cypress is not needed for the prod images.It is not needed for the testing image. It is needed for the integration tests.

Is there any way to split that up in the package.json, so that it does not need to installed in the other scenarios, or a different method where it I can avoid installing it in unnecessary circumstances?

Kalimantan
  • 702
  • 1
  • 9
  • 28
  • Does this answer your question? [How do you prevent install of "devDependencies" NPM modules for Node.js (package.json)?](https://stackoverflow.com/questions/9268259/how-do-you-prevent-install-of-devdependencies-npm-modules-for-node-js-package) – juliomalves Jan 29 '22 at 16:19

1 Answers1

1

Try using the --Prod flag or env variables in your install

npm run install --Prod

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies. To install all modules listed in both dependencies and devDependencies when NODE_ENV environment variable is set to production, you can use --production=false.

https://docs.npmjs.com/cli/v8/commands/npm-install#:~:text=With%20the%20%2D%2Dproduction,use%20%2D%2Dproduction%3Dfalse.

Ramakay
  • 2,919
  • 1
  • 5
  • 21