skip to Main Content

I am trying to dockerize a node application which uses typescript.

This my Dockerfile.

FROM node:14 as builder
ENV NODE_ENV=production
WORKDIR /usr/app
COPY package*.json ./
RUN npm install -g [email protected]
RUN npm i
COPY . .
RUN npm run build

FROM node:14
WORKDIR /usr/app
COPY package*.json ./
RUN npm install -g [email protected]
RUN npm install --production

COPY --from=builder /usr/app/dist ./dist

COPY ./src/ormconfig.docker.ts ./src/ormconfig.ts
COPY .env .

EXPOSE 5000
CMD node dist/src/index.js

And this docker-compose.yml

version: '2'
services:
  db:
    container_name: postgres
    image: postgres:latest
    ports:
      - '5432:5432'
    volumes:
      - ./data/postgres:/data/postgres
    env_file:
      - docker.env
    networks:
      - postgres

  web:
    image: 619525/techwondoe:1
    volumes:
      - ./src:/usr/app/
    depends_on:
      - db
    ports:
      - '5000:5000'
    

networks:
  postgres:
    driver: bridge

The Error I am getting is :

=> ERROR [builder 7/7] RUN npm run build                                                                                                                                             16.3s
------
 > [builder 7/7] RUN npm run build:
#13 0.991
#13 0.991 > [email protected] build
#13 0.991 > tsc -b
#13 0.991
#13 16.23 src/interfaces/controller.interface.ts(1,24): error TS7016: Could not find a declaration file for module 'express'. '/usr/app/node_modules/express/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
#13 16.23 src/middleware/error.middleware.ts(1,49): error TS7016: Could not find a declaration file for module 'express'. '/usr/app/node_modules/express/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
#13 16.23 src/app.ts(1,29): error TS7016: Could not find a declaration file for module 'body-parser'. '/usr/app/node_modules/body-parser/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/body-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'body-parser';`
#13 16.23 src/app.ts(2,31): error TS7016: Could not find a declaration file for module 'cookie-parser'. '/usr/app/node_modules/cookie-parser/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/cookie-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'cookie-parser';`
#13 16.23 src/app.ts(3,26): error TS7016: Could not find a declaration file for module 'express'. '/usr/app/node_modules/express/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
#13 16.23 node_modules/class-validator/types/decorator/string/IsAlpha.d.ts(2,25): error TS7016: Could not find a declaration file for module 'validator'. '/usr/app/node_modules/validator/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/validator` if it exists or add a new declaration (.d.ts) file containing `declare module 'validator';`
#13 16.23 node_modules/class-validator/types/decorator/string/IsAlphanumeric.d.ts(2,25): error TS7016: Could not find a declaration file for module 'validator'. '/usr/app/node_modules/validator/index.js' implicitly has an 'any' type.
#13 16.23   Try `npm i --save-dev @types/validator` if it exists or add a new declaration (.d.ts) file containing `declare module 'validator';`
#13 16.23 node_modules/class-validator/types/decorator/string/IsDecimal.d.ts(2,25): error TS7016: Could not find a declaration file for module 'validator'. '/usr/app/node_modules/validator/index.js' implicitly has an 'any' type.

and lot more errors like this only…

When I try to build locally i.e(npm run build) it builds perfectly, and gives no errors.
But on doing docker build it gives error.

Here are the dependencies of the application,

{
  "name": "techwondoe-assignment",
  "version": "1.0.0",
  "description": "",
  "main": "src/server.ts",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "cookie-parser": "^1.4.6",
    "dotenv": "^14.3.2",
    "envalid": "^7.2.2",
    "express": "^4.17.2",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.7.1",
    "reflect-metadata": "^0.1.13",
    "typeorm": "^0.2.41"
  },
  "devDependencies": {
    "@types/bcryptjs": "^2.4.2",
    "@types/cookie-parser": "^1.4.2",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.4.0",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/pg": "^8.6.4",
    "@types/supertest": "^2.0.11",
    "@types/validator": "^13.7.1",
    "node-gyp": "^8.4.1",
    "nodemon": "^2.0.15",
    "ts-node": "^10.4.0",
    "tslint": "^6.1.3",
    "tslint-config-airbnb": "^5.11.2",
    "typescript": "^4.5.5"
  },
  "scripts": {
    "dev": "nodemon --exec ts-node -r ./src/server.ts ",
    "build": "tsc -b",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "typeorm:cli": "ts-node ./node_modules/typeorm/cli -f ./src/ormconfig.ts"
  },
  "author": "xyz",
  "license": "MIT",
}

The command I am trying is docker build -t assignment:1 .
Am I missing something ?

local node --version : v14.17.0
local npm --version : 8.4.0

2

Answers


  1. You are using --production option for installing packages therefore type declarations are skipped. Remove it and it should work.

    As mentioned, also need to remove NODE_ENV=production

    Login or Signup to reply.
  2. you may use the –force flag which might removes the error as sometimes due to security concerns, these errors pop up
    RUN npm install –production –force

    note: this may disable some of the security features. Do checkout before applying it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search