Node.js 当NODE_ENV=production正在使用时,生成docker映像会出现错误(找不到模块的声明文件)

Node.js 当NODE_ENV=production正在使用时,生成docker映像会出现错误(找不到模块的声明文件),node.js,reactjs,typescript,docker,dockerfile,Node.js,Reactjs,Typescript,Docker,Dockerfile,当我们的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} ENV NODE\u ENV=production 对于纱线安装 Step #0: Step 7/9 : RUN

当我们的
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}
ENV NODE\u ENV=production

对于
纱线安装

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

您可能需要首先运行
安装
,然后设置env。还有,为什么要在全球范围内安装rimraf和typescript

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}

由于在运行
warn install
之前
NODE\u ENV=production
,您不会得到任何
devdependency
,其中包括
tsc
和Typescript
@types/..
模块。考虑一个多级的构建,如果你想要一个较小的运行时映像,它不包含最终图像中的这些依赖关系。@ DavidMaze,你能给我一篇文章,让它更好地理解吗?我可能缺少关于DEV VS生产的构建的信息,它是同一个构建脚本,它是否不同等等…您是如何导入或要求打包的?它是由
package.json
中的
warn install
完成的。