Jesper
Jesper

Reputation: 59

Next js production dependencies

I am trying to build docker container with nextjs app inside. Docker file as follows:

FROM node:16.16.0
RUN npm install --location=global pm2
WORKDIR /var/www/project-front
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production
COPY ./ /var/www/project-front
RUN /bin/bash -c 'yarn build'
EXPOSE 3000
USER node

# Launch app with PM2
CMD [ "pm2-runtime", "start", "npm", "--", "start" ]

My typescript and @types depencies are dev dependencies, and therefore they don't get installed on in that build, thus faling build.

I tried using ignoreBuildErrors prop in next.config.js but that makes many CI checks useless. So question is, do I really have to install my typescript dependencies not as dev deps, or may be there is another way?

I am trying to achive that all ci checks are checking types validity, but on productions it's useless so I don't want to install unnececary packages.

Upvotes: 0

Views: 896

Answers (1)

jcragun
jcragun

Reputation: 2198

Use a multi-stage Docker build. For example:

FROM node:16.16.0 as build
RUN yarn install --frozen-lockfile && yarn build

FROM node:16.16.0
RUN npm install --location=global pm2
WORKDIR /var/www/project-front
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production
COPY --from=build ./ /var/www/project-front
EXPOSE 3000
USER node

# Launch app with PM2
CMD [ "pm2-runtime", "start", "npm", "--", "start" ]

One drawback is you have to install some of the dependencies twice. One alternative is to copy files built outside the Docker build.

Upvotes: 1

Related Questions