【问题标题】:Building a docker image gives errors when NODE_ENV=production in use (Could not find a declaration file for module)使用 NODE_ENV=production 时构建 docker 映像会出错(找不到模块的声明文件)
【发布时间】:2020-03-29 06:41:08
【问题描述】:

当我们有Dockerfile:

ENV NODE_ENV=production

yarn install 命令出现以下错误:

Step #0: Step 7/9 : RUN yarn install; npm i -g rimraf typescript; yarn build:prod
Step #0:  ---> Running in 2ca9c250ce30
Step #0: yarn install v1.19.1
Step #0: [1/4] Resolving packages...
Step #0: [2/4] Fetching packages...
Step #0: info fsevents@1.2.9: The platform "linux" is incompatible with this module.
Step #0: info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
Step #0: [3/4] Linking dependencies...
Step #0: [91mwarning "request-promise > request-promise-core@1.1.3" has unmet peer dependency "request@^2.34".
Step #0: [0m[91mwarning " > request-promise@4.2.5" has unmet peer dependency "request@^2.34".
Step #0: [0m[4/4] Building fresh packages...
Step #0: Done in 12.79s.
Step #0: /usr/local/bin/rimraf -> /usr/local/lib/node_modules/rimraf/bin.js
Step #0: /usr/local/bin/tsc -> /usr/local/lib/node_modules/typescript/bin/tsc
Step #0: /usr/local/bin/tsserver -> /usr/local/lib/node_modules/typescript/bin/tsserver
Step #0: + rimraf@3.0.0
Step #0: + typescript@3.7.3
Step #0: added 13 packages from 5 contributors in 2.037s
Step #0: yarn run v1.19.1
Step #0: $ rimraf build && tsc -b
Step #0: src/lib/pg-typed/client.ts(1,16): error TS7016: Could not find a declaration file for module 'pg'. '/var/www/node_modules/pg/lib/index.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/pg` if it exists or add a new declaration (.d.ts) file containing `declare module 'pg';`
Step #0: src/lib/pg-typed/column.ts(1,15): error TS7016: Could not find a declaration file for module 'lodash'. '/var/www/node_modules/lodash/lodash.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/lodash` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash';`
Step #0: src/lib/pg-typed/database.ts(1,16): error TS7016: Could not find a declaration file for module 'pg'. '/var/www/node_modules/pg/lib/index.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/pg` if it exists or add a new declaration (.d.ts) file containing `declare module 'pg';`
Step #0: src/lib/pg-typed/sql.ts(1,15): error TS7016: Could not find a declaration file for module 'lodash'. '/var/www/node_modules/lodash/lodash.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/lodash` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash';`
Step #0: src/lib/pg-typed/select.ts(1,15): error TS7016: Could not find a declaration file for module 'lodash'. '/var/www/node_modules/lodash/lodash.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/lodash` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash';`
Step #0: src/lib/pg-typed/select.ts(73,49): error TS7006: Parameter 'key' implicitly has an 'any' type.
Step #0: src/lib/pg-typed/select.ts(74,50): error TS7006: Parameter 'col' implicitly has an 'any' type.
Step #0: src/lib/pg-typed/select.ts(80,7): error TS7006: Parameter 'rel' implicitly has an 'any' type.
Step #0: src/lib/pg-typed/table.ts(1,15): error TS7016: Could not find a declaration file for module 'lodash'. '/var/www/node_modules/lodash/lodash.js' implicitly has an 'any' type.
Step #0:   Try `npm install @types/lodash` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash';`

坦率地说,我不明白这一点。为什么会失败?

我们的package.json 是:

{
  "scripts": {
    "testdb": "dbmate -e TEST_DATABASE_URL drop && dbmate -e TEST_DATABASE_URL --no-dump-schema up",
    "test": "yarn run testdb && jest",
    "build": "rimraf build && tsc",
    "build:prod": "rimraf build && tsc -b",
    "dev": "micro-dev build/src --watch build"
  },
  "jest": {
    "preset": "ts-jest",
    "globalSetup": "./tests/globalSetup.ts",
    "testPathIgnorePatterns": [
      "<rootDir>/build/",
      "<rootDir>/node_modules/"
    ]
  },
  "dependencies": {
    "dotenv": "^8.2.0",
    "fastest-validator": "^1.0.0-beta4",
    "lodash": "^4.17.15",
    "micro": "^9.3.4",
    "microrouter": "^3.1.3",
    "murmurhash": "^0.0.2",
    "pg": "^7.14.0"
  },
  "devDependencies": {
    "@types/jest": "^24.0.22",
    "@types/lodash": "^4.14.147",
    "@types/micro": "^7.3.3",
    "@types/microrouter": "^3.1.1",
    "@types/pg": "^7.11.2",
    "jest": "^24.9.0",
    "micro-dev": "^3.0.0",
    "request-promise": "^4.2.5",
    "rimraf": "^3.0.0",
    "test-listen": "^1.1.0",
    "ts-jest": "^24.1.0",
    "tslint": "^5.20.1",
    "typescript": "^3.7.2"
  }
}

有人能说明我们应该如何运行这个构建以进行生产吗?

我们有这个Dockerfile

FROM node:lts-slim
ENV NODE_ENV=production
RUN yarn install; npm i -g rimraf typescript; yarn build:prod 
CMD yarn run micro -l tcp://0.0.0.0:${PORT} build/src
EXPOSE ${PORT}

【问题讨论】:

  • 自从NODE_ENV=production 之前你运行yarn install,你没有得到任何你的devDependencies,其中包括像tsc和打字稿@987654333 @模块。如果您想要一个较小的运行时映像,并且最终映像中不包含这些依赖项,请考虑多阶段构建。
  • @DavidMaze 你能指点我一篇文章来更好地理解这一点吗,我可能缺乏关于如何为开发和生产构建它的信息,它是相同的构建脚本,是否不同等等......
  • 你如何导入或要求包?
  • yarn install 来自package.json 完成。

标签: node.js reactjs typescript docker dockerfile


【解决方案1】:

您可能需要运行yarn install 之前NODE_ENV 设置为production。另外,为什么要全局安装rimraftypescript

FROM node:lts-slim
RUN yarn install
ENV NODE_ENV=production
RUN yarn build:prod 
CMD yarn run micro -l tcp://0.0.0.0:${PORT} build/src
EXPOSE ${PORT}

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2021-03-19
    • 2017-08-13
    • 2018-04-23
    • 2021-12-21
    • 1970-01-01
    • 2021-01-02
    • 2019-04-29
    相关资源
    最近更新 更多