简体   繁体   English

在 docker 容器上运行 nodejs 应用程序会给出“错误:找不到模块'/usr/src/app/nodemon'”

[英]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.

相关问题 错误:使用 docker-compose 时找不到模块“/usr/src/app/nodemon” - Error: Cannot find module '/usr/src/app/nodemon' while using docker-compose docker ubuntu 中的 nodejs 找不到模块 /usr/src/app/index.js - nodejs in docker ubuntu cannot find module /usr/src/app/index.js Docker:错误:找不到模块/app/src/myapp.js - Docker: Error: Cannot find module /app/src/myapp.js 运行nodemon时找不到模块“ express”。 引发错误,应用程序崩溃 - Cannot find module 'express' while running nodemon. Throw error, app crashed 在 Docker 中找不到 NodeJS 应用程序的模块? - Cannot find module for NodeJS app inside Docker? 找不到在 docker 环境中运行的节点 js 应用程序的模块错误 - Cannot find module error for a node js app running in a docker environment 重新启动时Nodemon应用程序找不到模块 - Nodemon app cannot find module when restarting Docker-无法解析'/ usr / src / app / src'中的节点模块 - Docker - Can't resolve node module in '/usr/src/app/src' 在docker swarm中运行的docker容器中的firebase身份验证错误nodejs应用程序 - firebase authentication error nodejs app in docker container running in docker swarm NodeJS 在运行 Handlebars 时出现无法找到模块 './parser' 错误 - NodeJS gives Cannot find module './parser' error while running Handlebars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM