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
evrething work fine.
Why in first Dockerfile
RUN npm install @types/node
dont install node folder inside @types?
I try to RUN npm install @types/node
after COPY ./ ./
it doesn’t work either
2
Answers
I this case it doesn't work in the first Dockerfile because
@type/node
was installed like dev dependency inpackage.json
When i install it like
RUN npm install @types/node
inside Dockerfile, it doesn't addnode
folder inside@types
innode_modules
, because@types/node
not istalled if it like devDependeces package.When jumping into the Docker container:
However, when i uninstall it and install it again as main dependency, it work fine:
thats why the second Docker file is worked fine
and if i add packege
@type/node
like main dependence inpackage.json
file: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.
Are you sure that you don’t have a conflicting version of
@types/node
specified in yourpackage.json
? Suppose, for example, that you had this specified inpackage.json
:Install from Dockerfile
Something like this will enable you to install from the
Dockerfile
without first uninstalling.🗎
Dockerfile
🗎
package.json
(Note that this doesn’t contain@types/node
,@nestjs/cli
ortypescript
, all of which are being installed from theDockerfile
!)Install from package.json
IMHO a better option is to move the package installation completely to
package.json
. With the setup below all packages are installed in a singlenpm install
command.🗎
Dockerfile
🗎
package.json