【问题标题】:Types Error On ( RUN npm run build ) in dockerfile builddockerfile build 中的类型错误 (RUN npm run build)
【发布时间】:2022-01-28 22:05:31
【问题描述】:

我正在尝试对使用 typescript 的节点应用程序进行 dockerize。

这是我的Dockerfile

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

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

还有这个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

我得到的错误是:

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

还有更多类似的错误......

当我尝试在本地构建时,即(npm run build)它构建完美,并且没有错误。 但是在执行docker build 时会出错。

这里是应用的依赖,

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

我正在尝试的命令是docker build -t assignment:1 . 我错过了什么吗?

本地node --version : v14.17.0 本地npm --version : 8.4.0

【问题讨论】:

  • 您能否编辑问题以包含实际错误,而不是屏幕截图?至少对我来说,微小的文本和深蓝色的黑色很难阅读,我无法复制错误的文本以在其他地方搜索。
  • 好的,我编辑了问题。

标签: node.js typescript docker


【解决方案1】:

您正在使用--production 选项安装包,因此类型声明被跳过。删除它,它应该可以工作。

如前所述,还需要去掉 NODE_ENV=production

【讨论】:

  • 我确实试过了..但得到了同样的错误。并且错误发生在npm run build 阶段
  • ENV NODE_ENV=production在第一阶段的效果是一样的。
  • 哦。谢谢你。它奏效了。
猜你喜欢
  • 2021-03-03
  • 2022-08-17
  • 1970-01-01
  • 2020-08-25
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 2022-06-16
相关资源
最近更新 更多