When I use the following Dockerfile:
FROM node:22-alpine
ENV NODE_ENV production
WORKDIR /usr/web-server
RUN npm i -g @nestjs/cli typescript
COPY ./package*.json ./
RUN npm install
RUN npm install @types/node
COPY ./ ./
EXPOSE 4000
CMD npm run start:dev
inside /usr/web-server/node_modules/@types node folder not installed.
However, if I use the Dockerfile below:
FROM node:22-alpine
ENV NODE_ENV production
WORKDIR /usr/web-server
RUN npm i -g @nestjs/cli typescript
COPY ./package*.json ./
RUN npm install
RUN npm uninstall @types/node
RUN npm install @types/node
COPY ./ ./
EXPOSE 4000
CMD npm run start:dev
everything works fine.
Why in first Dockerfile RUN npm install @types/node
doesn't install node folder inside @types?
I try to RUN npm install @types/node
after COPY ./ ./
but it doesn't work either
I this case it doesn't work in the first Dockerfile because @type/node
was installed like dev dependency in package.json
"devDependencies": {
"@types/node": "^20.12.12",
}
When i install it like RUN npm install @types/node
inside Dockerfile, it doesn't add node
folder inside @types
in node_modules
, because @types/node
not istalled if it like devDependeces package.
When jumping into the Docker container:
cd /usr/web-server/node_modules/@types
ls
// result empty folder
However, when i uninstall it and install it again as main dependency, it work fine:
cd /usr/web-server/node_modules/@types
ls
// Now result is node folder
thats why the second Docker file is worked fine
and if i add packege @type/node
like main dependence in package.json
file:
"dependencies": {
"@types/node": "^20.12.12",
}
Now I dont need to add RUN npm install/unistall @types/node
in Dockerfile.
The remaining question is why @types/node is not installed in the container as a devDependency, but that's another issue already.