Búsqueda de sitios web

node.js - Error de tipos al ejecutar (RUN npm run build) en la construcción del dockerfile


Estoy intentando dockerizar una aplicación de Node que usa TypeScript.

Este es mi 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

Y este 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

El error que estoy recibiendo es:

=> 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.

y muchos más errores como este solamente...

Cuando intento construir localmente, es decir, (npm run build), se construye perfectamente y no da errores. Pero al hacer docker build da error.

Aquí están las dependencias de la aplicación,

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

El comando que estoy intentando es docker build -t assignment:1 . ¿Me estoy perdiendo de algo?

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

Respuesta #1

Estás utilizando la opción --production para instalar paquetes, por lo tanto, se omiten las declaraciones de tipos. Quítala y debería funcionar.

Como se mencionó, también es necesario eliminar NODE_ENV=production

Respuesta #2

puede usar la bandera --force, lo que podría eliminar el error ya que a veces, debido a preocupaciones de seguridad, estos errores aparecen. EJECUTE npm install --production --force

nota: esto puede desactivar algunas de las funciones de seguridad. Haz un checkout antes de aplicarlo