node.js 找不到在 docker compose 环境中运行的节点 js 应用程序的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42040317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:58:39  来源:igfitidea点击:

Cannot find module for a node js app running in a docker compose environment

node.jsdockernpmdocker-compose

提问by jemlifathi

I am sorry for my very newbie question, but I am having a terrible day figuring out this error, I have an Express app and I am trying to run it in docker compose. I've used this Dockerfile:

我很抱歉我的新手问题,但我在弄清楚这个错误的过程中度过了糟糕的一天,我有一个 Express 应用程序,我正在尝试在 docker compose 中运行它。我用过这个 Dockerfile:

FROM mhart/alpine-node
RUN mkdir -p /usr/src/app
RUN chmod -R 777 /usr/src/app
WORKDIR /usr/src/app
RUN npm install node-gyp -g
RUN npm install nodemon -g
ENV NODE_ENV development
EXPOSE 3000

And this portion of my docker-compose file:

我的 docker-compose 文件的这一部分:

backend:
    mem_limit: 100m
    build:
      context: .
      dockerfile: dockerfiles/node/Dockerfile
    command: npm start
    depends_on:
      - mongo
      - elasticsearch
    volumes:
      - ./backend/:/usr/src/app
    ports:
      - 3000:3000
    links:
      - "mongo:mongo"
      - "elasticsearch:elasticsearch"

When I do docker-compose up, I get this error:

当我执行 docker-compose up 时,出现此错误:

backend_1        | npm info it worked if it ends with ok
backend_1        | npm info using [email protected]
backend_1        | npm info using [email protected]
backend_1        | npm info lifecycle [email protected]~prestart: [email protected]
backend_1        | npm info lifecycle [email protected]~start: [email protected]
backend_1        | 
backend_1        | > [email protected] start /usr/src/app
backend_1        | > nodemon index.js
backend_1        | 
backend_1        | [nodemon] 1.11.0
backend_1        | [nodemon] to restart at any time, enter `rs`
backend_1        | [nodemon] watching: *.*
backend_1        | [nodemon] starting `node index.js`
backend_1        | module.js:471
backend_1        |     throw err;
backend_1        |     ^
backend_1        | 
backend_1        | Error: Cannot find module 'dotenv'
backend_1        |     at Function.Module._resolveFilename (module.js:469:15)
backend_1        |     at Function.Module._load (module.js:417:25)
backend_1        |     at Module.require (module.js:497:17)
backend_1        |     at require (internal/module.js:20:19)
backend_1        |     at Object.<anonymous> (/usr/src/app/index.js:1:63)
backend_1        |     at Module._compile (module.js:570:32)
backend_1        |     at Object.Module._extensions..js (module.js:579:10)
backend_1        |     at Module.load (module.js:487:32)
backend_1        |     at tryModuleLoad (module.js:446:12)
backend_1        |     at Function.Module._load (module.js:438:3)
backend_1        | [nodemon] app crashed - waiting for file changes before starting...

If I do ls -alin the backendcontainer, I get a full list of my backend app folder content, but it sounds like node_modules dependencies are not recognized.

如果我ls -al后端容器中这样做,我会得到我的后端应用程序文件夹内容的完整列表,但听起来像 node_modules 依赖项无法识别。

回答by CharlieBrown

You need to install the dependencies in the container, which is missing from your Dockerfile.

您需要在 Dockerfile 中缺少的容器中安装依赖项。

The common way is to create a Dockerfile that is already aware of your application, and make it copy your package.jsonfile and perform an npm install.

常见的方法是创建一个已经知道你的应用程序的 Dockerfile,并让它复制你的package.json文件并执行npm install.

This allows your container to find all your code dependencies when you later run your application.

这允许您的容器在您稍后运行应用程序时找到所有代码依赖项。

See and example here: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

请参阅此处的示例:https: //nodejs.org/en/docs/guides/nodejs-docker-webapp/

The sample Dockerfile:

样品Dockerfile

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

You may need to adapt paths for the COPYcommand, of course.

当然,您可能需要调整COPY命令的路径。

回答by M3RS

If your Dockerfileand package.jsonfiles are correct and still have the issue:

如果您的Dockerfilepackage.json文件正确并且仍然存在问题:

1) Make sure you've rebuilt your container images.

1) 确保你已经重建了你的容器镜像。

2) Try

2)尝试

docker-compose down -v

docker-compose down -v

before starting the containers again with docker-compose up.

在用 再次启动容器之前docker-compose up

This removes all volumes.

这将删除所有卷。

回答by Nishanth

I also faced the same issue today when I run docker-compose up.

我今天跑步时也遇到了同样的问题docker-compose up

The issue resolved by running docker-compose up --buildinstead of docker-compose up.

通过运行docker-compose up --build而不是docker-compose up.