node.js - Typer Fel Vid ( KÖR npm run build ) i dockerfile-build


Jag försöker dockerisera en Node-applikation som använder TypeScript.

Detta är min Dockerfile.

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

FROM node:14
WORKDIR /usr/app
COPY package*.json ./
RUN npm install -g [email 
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

Och denna 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

Felet jag får är :

=> ERROR [builder 7/7] RUN npm run build                                                                                                                                             16.3s
------
 > [builder 7/7] RUN npm run build:
#13 0.991
#13 0.991 > [email  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.

och många fler fel som detta bara...

När jag försöker bygga lokalt, dvs (npm run build), byggs det perfekt och ger inga fel. Men när jag gör docker build ger det fel.

Här är beroenden för applikationen,

{
  "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",
}

Kommandot jag försöker är docker build -t assignment:1 . Saknar jag något?

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

Svar #1

Du använder alternativet --production för att installera paket, därför hoppas typdeklarationer över. Ta bort det så borde det fungera.

Som nämnts, behöver också ta bort NODE_ENV=production

Svar #2

du kan använda --force-flaggan som kan ta bort felet eftersom dessa fel ibland dyker upp på grund av säkerhetsproblem, KÖR npm install --production --force

notera: detta kan inaktivera några av säkerhetsfunktionerna. Se till att checka ut innan du tillämpar det