skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Are you sure that you don’t have a conflicting version of @types/node specified in your package.json? Suppose, for example, that you had this specified in package.json:

    "@types/node": "^16.0.0"
    

    Install from Dockerfile

    Something like this will enable you to install from the Dockerfile without first uninstalling.

    🗎 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
    

    🗎 package.json (Note that this doesn’t contain @types/node, @nestjs/cli or typescript, all of which are being installed from the Dockerfile!)

      "name": "nestjs-web-server",
      "version": "1.0.0",
      "description": "A simple NestJS web server",
      "main": "src/main.ts",
      "scripts": {
        "start:dev": "nest start --watch"
      },
      "dependencies": {
        "@nestjs/common": "^9.0.0",
        "@nestjs/core": "^9.0.0",
        "@nestjs/platform-express": "^9.0.0"
      },
      "devDependencies": {
        "@nestjs/testing": "^9.0.0",
        "prettier": "^2.7.1",
        "eslint": "^8.15.0",
        "jest": "^29.0.0"
      }
    }
    

    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 single npm install command.

    🗎 Dockerfile

    FROM node:22-alpine
    
    ENV NODE_ENV production
    
    WORKDIR /usr/web-server
    
    COPY ./package*.json ./
    RUN npm install
    
    COPY ./ ./
    
    EXPOSE 4000
    
    CMD npm run start:dev
    

    🗎 package.json

    {
      "name": "nestjs-web-server",
      "version": "1.0.0",
      "description": "A simple NestJS web server",
      "main": "src/main.ts",
      "scripts": {
        "start:dev": "npx nest start --watch"
      },
      "dependencies": {
        "@nestjs/common": "^9.0.0",
        "@nestjs/core": "^9.0.0",
        "@nestjs/cli": "^9.0.0",
        "@nestjs/platform-express": "^9.0.0"
      },
      "devDependencies": {
        "@nestjs/testing": "^9.0.0",
        "@types/node": "^20.0.0",
        "typescript": "^4.6.3",
        "prettier": "^2.7.1",
        "eslint": "^8.15.0",
        "jest": "^29.0.0"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search