![Docker 容器无法找到 express,即使它已安装并且 Dockerfile 编译正常](https://linux22.com/image/785874/Docker%20%E5%AE%B9%E5%99%A8%E6%97%A0%E6%B3%95%E6%89%BE%E5%88%B0%20express%EF%BC%8C%E5%8D%B3%E4%BD%BF%E5%AE%83%E5%B7%B2%E5%AE%89%E8%A3%85%E5%B9%B6%E4%B8%94%20Dockerfile%20%E7%BC%96%E8%AF%91%E6%AD%A3%E5%B8%B8.png)
当我尝试从刚刚使用“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"]