[英]Running a nodejs app on a docker container gives “ Error: Cannot find module '/usr/src/app/nodemon' ”
Here is my Dockerfile which is at the root of the nodejs application.这是我的 Dockerfile,它位于 nodejs 应用程序的根目录。
# Build from LTS version of node (version 12)
FROM node:12
# Create app directory
RUN mkdir -p /usr/src/app
# Define app diretory inside image
WORKDIR /usr/src/app
# package.json AND package-lock.json are copied where available
COPY package*.json /usr/src/app/
# install modules
RUN npm install
# Bundle app source
COPY . /usr/src/app
# Bind app to port 3000
EXPOSE 3000
# Command to run app
CMD [ "nodemon", "./bin/www" ]
Here is my docker-compose.yml
file这是我的
docker-compose.yml
文件
version: '2'
services:
mongo:
container_name: mongo
image: 'mongo:3.4.1'
ports:
- "27017:27017"
backend-app:
container_name: school-backend
restart: always
build: ./server
ports:
- "3000:3000"
frontend-app:
container_name: angular-frontend
restart: always
build: ./angular-app
ports:
- "4200:4200"
I execute the command docker-compose up
我执行命令
docker-compose up
Then I get this error然后我得到这个错误
school-backend | Error: Cannot find module '/usr/src/app/nodemon'
school-backend | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
school-backend | at Function.Module._load (internal/modules/cjs/loader.js:842:27)
school-backend | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
school-backend | at internal/main/run_main_module.js:17:47 {
school-backend | code: 'MODULE_NOT_FOUND',
school-backend | requireStack: []
school-backend | }
In the Dockerfile, I copy the package.json
to the working directory /usr/src/app\
.在 Dockerfile 中,我将
package.json
复制到工作目录/usr/src/app\
。
Then I do npm install
which would install nodemon since it is declared in the package.json
然后我执行
npm install
安装 nodemon 因为它在package.json
中声明
But, why is the module given as absent?但是,为什么模块不存在?
It's not globally installed then.那时它不是全局安装的。
In this case, you have to call the nodemon bin inside the node_modules: ./node_modules/nodemon/bin/nodemon.js
.在这种情况下,您必须在 node_modules:
./node_modules/nodemon/bin/nodemon.js
中调用 nodemon bin。
You can use npx
like this CMD [ "npx", "nodemon", "./bin/www" ]
.您可以像这样使用 npx
npx
CMD [ "npx", "nodemon", "./bin/www" ]
。
npx will run programs from the node_modules/.bin
directory. npx 将从
node_modules/.bin
目录运行程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.