它不能在Docker环境中运行。
Dockerfile
FROM node:18.17.0 AS builder
WORKDIR /usr/src/app
RUN yarn global add @nestjs/cli
COPY package*.json yarn.lock ./
RUN yarn --prod
COPY . .
RUN yarn build
FROM node:18.17.0-alpine
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/package*.json ./package*.json
COPY --from=builder /usr/src/app/yarn.lock ./yarn.lock
COPY --from=builder /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
错误信息
Error: Cannot find module '@nestjs/core'
Require stack:
| - /usr/src/app/dist/main.js
| at Module.\_resolveFilename (node:internal/modules/cjs/loader:1077:15)
| at Module.\_load (node:internal/modules/cjs/loader:922:27)
| at Module.require (node:internal/modules/cjs/loader:1143:19)
| at require (node:internal/modules/cjs/helpers:110:18)
| at Object.\<anonymous\> (/usr/src/app/dist/main.js:3:16)
| at Module.\_compile (node:internal/modules/cjs/loader:1256:14)
| at Module.\_extensions..js (node:internal/modules/cjs/loader:1310:10)
| at Module.load (node:internal/modules/cjs/loader:1119:32)
| at Module.\_load (node:internal/modules/cjs/loader:960:12)
| at Function.executeUserEntryPoint \[as runMain\] (node:internal/modules/run_main:81:12) {
| code: 'MODULE_NOT_FOUND',
| requireStack: \[ '/usr/src/app/dist/main.js' \]
| }
|
| Node.js v18.17.0
我试着添加这个,但没有任何变化
RUN yarn add @nestjs/core
此错误发生在更改Dockerfile之后。我认为Dockerfile设置可能有问题
1条答案
按热度按时间n8ghc7c11#
Nest.js应用程序似乎无法找到所需的模块,因为它没有安装在生产环境中。在你的Dockerfile中,你在构建阶段使用--prod标志和yarn命令,这告诉Yarn只安装生产依赖。这可能是错误的原因。
若要解决此问题,您应该在构建阶段同时安装生产和开发依赖项,然后在最终映像中删除开发依赖项,以保持较小的映像大小。你可以通过修改你的Dockerfile来做到这一点: