Docker 容器无法找到 express,即使它已安装并且 Dockerfile 编译正常

Docker 容器无法找到 express,即使它已安装并且 Dockerfile 编译正常

当我尝试从刚刚使用“docker run”构建的映像运行 docker 容器时,Node.js 找不到 express,即使它已安装并且 Dockerfile 编译正常。

输出如下

> dev
> nodemon

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
node:internal/modules/cjs/loader:1063
  throw err;
  ^

Error: Cannot find module 'express'
Require stack:
- /app/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1060:15)
    at Module._load (node:internal/modules/cjs/loader:905:27)
    at Module.require (node:internal/modules/cjs/loader:1127:19)
    at require (node:internal/modules/helpers:112:18)
    at Object.<anonymous> (/app/index.js:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1246:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)
    at Module.load (node:internal/modules/cjs/loader:1103:32)
    at Module._load (node:internal/modules/cjs/loader:942:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/app/index.js' ]
}

Node.js v19.5.0
[nodemon] app crashed - waiting for file changes before starting...

这是我的 Dockerfile

FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN rm -rf node_modules
RUN rm -f package-lock.json
RUN npm cache clean --force
RUN npm init -y
RUN npm install -g [email protected]
RUN npm install -g express --save
RUN npm install -g nodemon --save
RUN npm install -g validator --save
RUN npm install -g is-empty --save


COPY . .
CMD ["npm", "run", "dev"]

相关内容