[英]Node cannot find node_modules in dockerized node app
I am new to docker. 我是码头工人的新手。 I tried to dockerize my existing node application.
我试图将现有节点应用程序停靠。 During docker-compose the app throws error stating it cannot find the node_modules.
在docker-compose期间,应用程序抛出错误,指出它无法找到node_modules。
Here's the error 这是错误
dockerised-yelpcamp | > dockerised-yelpcamp@1.0.1 start /usr/src/app
dockerised-yelpcamp | > node app.js
dockerised-yelpcamp |
dockerised-yelpcamp | internal/modules/cjs/loader.js:584
dockerised-yelpcamp | throw err;
dockerised-yelpcamp | ^
dockerised-yelpcamp |
dockerised-yelpcamp | Error: Cannot find module 'express'
dockerised-yelpcamp | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
dockerised-yelpcamp | at Function.Module._load (internal/modules/cjs/loader.js:508:25)
dockerised-yelpcamp | at Module.require (internal/modules/cjs/loader.js:637:17)
dockerised-yelpcamp | at require (internal/modules/cjs/helpers.js:22:18)
dockerised-yelpcamp | at Object.<anonymous> (/usr/src/app/app.js:1:77)
.
.
.
dockerised-yelpcamp | at Function.Module._load (internal/modules/cjs/loader.js:531:3)
dockerised-yelpcamp | npm ERR! code ELIFECYCLE
dockerised-yelpcamp | npm ERR! errno 1
dockerised-yelpcamp | npm ERR! dockerised-yelpcamp@1.0.1 start: `node app.js`
dockerised-yelpcamp | npm ERR! Exit status 1
dockerised-yelpcamp | npm ERR!
dockerised-yelpcamp | npm ERR! Failed at the dockerised-yelpcamp@1.0.1 start script.
dockerised-yelpcamp | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
dockerised-yelpcamp | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
dockerised-yelpcamp |
dockerised-yelpcamp | npm ERR! A complete log of this run can be found in:
dockerised-yelpcamp | npm ERR! /root/.npm/_logs/2019-04-17T11_14_17_109Z-debug.log
dockerised-yelpcamp exited with code 1
docker-compose.yml 泊坞窗,compose.yml
version: "3"
services:
main_app:
container_name: dockerised-yelpcamp
restart: always
build: .
ports:
- 80:3000
links:
- mongo
volumes: ["./:/usr/src/app"]
mongo:
container_name: mongo
image: mongo
ports:
- 27017:27017
Dockerfile Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
My docker file contains npm install
which is executed successfully while building image. 我的docker文件包含
npm install
,它在构建映像时成功执行。
You copy only package.json, so you need node_modules folder created: 您只复制package.json,因此您需要创建node_modules文件夹:
dockerised-yelpcamp | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
if my memory is good, docker container can't respond yes by default. 如果我的记忆力很好,默认情况下docker容器不能回答是。 So, he skip creation folder and node_modules stay missing.
因此,他跳过创建文件夹并且node_modules保持缺失状态。
Your volumes:
directive is hiding everything the Dockerfile
does with content from your local system. 您的
Dockerfile
volumes:
指令隐藏了Dockerfile
对本地系统内容所做的一切。 If you don't have a node_modules
directory there or its architecture or library stack is incompatible with what's in the container, this setup simply won't work. 如果那里没有
node_modules
目录,或者它的体系结构或库堆栈与容器中的内容不兼容,则此设置根本不起作用。
The common workaround seems to be to declare a second anonymous volume to hold node_modules
, which will work until you change your package.json
file. 常见的解决方法似乎是声明第二个匿名卷来保存
node_modules
,这将一直node_modules
,直到您更改package.json
文件。
My recommendation would be to remove the volumes:
directive entirely. 我的建议是完全删除
volumes:
指令。 Develop your application locally without Docker. 无需Docker即可在本地开发应用程序。 Once it works and you've tested it, use this
Dockerfile
to rebuild the image. 一旦它工作并且你已经测试过它,使用这个
Dockerfile
来重建图像。 This both gives you a simpler non-Docker development setup, and replicates the environment you have in production (where you're not going to copy the application code in addition to the Docker image that contains it). 这两者都为您提供了一个更简单的非Docker开发设置,并复制了您在生产环境中的环境(除了包含它的Docker镜像之外,您不会复制应用程序代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.