There are a lot of people online asking this same question in different ways but there is no clear answer. Can anybody understand enough to explain why a docker build
fails when package-lock.json
file exists in the application, but runs successfully when it is not? Seemingly it is related to npm but it is not clear.
Everybody says delete the package-lock.json
, but it is there for a reason.
Note: npm install
works fine on my local machine, just fails in docker container.
If I have this Dockerfile:
# First Stage: Builder
FROM node:13.12.0-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
And run this:
docker build -t container-tag ./
I get this:
npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/eventsource-c2615740/example/index.html'
npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/eventsource-c2615740/example/sse-client.js'
npm WARN tar ENOENT: no such file or directory, open '/app/node_modules/.staging/react-router-a14663ae/README.md'
But this Dockerfile will run successfully:
# First Stage: Builder
FROM node:13.12.0-alpine AS build
WORKDIR /app
COPY package.json ./ #<-------- note that there is no star here
RUN npm install
COPY . .
RUN npm run build
From your question:
If you are using
npm install
, you are not sure to have the same version of dependencies.For having a reproducible environment, without unexpected issues because of different version of dependencies, you'd rather use
npm ci
(clean-install):A Fabian Gander's article gives further clarification about the
npm install
andnpm ci
tools and provides advice on when to use each one. The below table is from that source:This is why package-lock.json is there, to be available for tools like
npm ci
.After having a reproducible environment, if this doesn't fix your issue, you need to keep investigating, but IMO it should be the first step.