Home > database >  Getting typescript errors when building Nestjs Dockerfile
Getting typescript errors when building Nestjs Dockerfile

Time:10-11

I have a Dockerfile for a Nestjs project, I start the container with docker-compose. But I am getting some typescript errors during the build stage.

Have been hours and I still not able to find a solution. Anyone has idea how to tackle this? Thanks a lot! Let me know if I have to provide more information

enter image description here

Here is the Dockerfile

FROM node:14.17.0-alpine as base

LABEL maintainer="[email protected]"

WORKDIR /usr/src/app

COPY package.json package-lock.json ./ 


FROM base as dev

RUN npm install

RUN npm link webpack

COPY .eslintrc.js nest-cli.json tsconfig.json tsconfig.build.json ./

COPY .env /usr/src/app/.env

CMD [ "npm", "run", "start:dev", "--preserveWatchOutput" ]

FROM base as prod

RUN npm install --only=production

COPY . .

RUN npm run build

CMD ["node", "dist/main"]


And the compose file

version: "3.7"

services:
  backend:
    # container_name: edudb_backend
    restart: always
    build:
      context: .
      dockerfile: ./Dockerfile
      target: prod
    stdin_open: true
    tty: true
    environment:
      - APP_ENV=production
      - APP_PORT=8000
    volumes:
      - ./src:/usr/src/app/src
      - ./test:/usr/src/app/test
    working_dir: /usr/src/app
    ports:
      - "8000:8000"
    links:
      - mysql
    depends_on:
      - mysql
    networks:
      - default
  mysql:
    # container_name: mysql
    restart: always
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=root
      # - MYSQL_USER=root
      - MYSQL_PASSWORD=root
      - MYSQL_DATABASE=edu_db
    ports:
      - "3306:3306"
    volumes:
      - edudb_mysql_data:/var/lib/mysql
    networks:
      - default

volumes:
  edudb_mysql_data:

networks:
  default:
    name: edudb

CodePudding user response:

RUN npm install --only=production

COPY . .

RUN npm run build

CMD ["node", "dist/main"]

You can't only install production dependencies and expect building (a dev process that requires depDependencies) to work. Most of the time, @types/ packages are saved as devDependencies and are used by Typescript to ensure the types are correct.

Generally speaking, for running the application you should only need production dependencies, and building the application should happen in an earlier stage of a multi-stage dockerfile, like in your dev stage. Then you have prod copy the dist from dev and install only the production dependencies so you can node dist/main without any typescript dependencies

  • Related