[英]Error in docker (cannot find module I exported) , not a problem when running npm start
Error: Cannot find module '../controllers/thermostatLogic'
Here is my dockerfile这是我的 dockerfile
FROM node:10-alpine
LABEL version="1.02"
RUN mkdir -p /app/node_modules && chown -R node:node /app
USER node
WORKDIR /app
COPY . /app
RUN npm install
RUN npm audit fix
RUN ls
RUN cd ./controllers && ls
RUN mkdir uploads &&\
cd uploads &&\
touch uploadedFile.csv
RUN cd ..
RUN ls
CMD npm start
EXPOSE 1000
I run the docker build -t hvacdoctor.
我运行docker build -t hvacdoctor.
command and then.命令,然后。 When it runs the ls command it returns:当它运行 ls 命令时,它返回:
---> Running in a6d2b49092ef
INSIDE
app.js
bin
controllers
env.js
exampleDevices.json
node_modules
package-lock.json
package.json
public
readme.md
routes
schema
test
views
yarn.lock
Removing intermediate container a6d2b49092ef
docker run hvacdoctordocke:latest docker 运行 hvacdoctordocke:最新
Error: Cannot find module '../controllers/t***Logic'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/node/app/controllers/fileLogic.js:8:19)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/node/app/routes/index.js:4:19)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
This all works with npm install & npm start without an error OUT of docker but why is this an issue within it?这一切都适用于 npm 安装和 npm 启动时没有错误 OUT of docker 但为什么这是其中的问题?
I've tried a few different methods (git clone is out though because i need SSO for that etc)我尝试了几种不同的方法(虽然 git clone 已经退出,因为我需要 SSO 等)
These are the install instructions
1) download bash / node
2) create a folder called uploads and a file called uploadedFile.csv -make sure the file is in the folder.
3) npm install
4) npm start
Edit - here is a screenshot of the current folder structure.编辑 - 这是当前文件夹结构的屏幕截图。
Thoughts?想法?
Edit - it was suggested I run the program with the following docker-compose.yml编辑 - 建议我使用以下 docker-compose.yml 运行程序
node-app:
container_name: node-app
image: node:latest
restart: always
volumes:
- ./:/home/node/app
working_dir: /home/node/app
ports:
- 4000:4000
networks:
- main-network
command: "tail -f /dev/null && npm start"
Changed package.json
"main": "index.js",
"scripts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon index.js",
}
^^ WITH this suggested start script nodemon starts index.js recursively - if I remove nodemon from the command it cannot find index.js. ^^ 使用这个建议的启动脚本 nodemon 递归地启动 index.js - 如果我从命令中删除 nodemon 它找不到 index.js。 - I will likely edit this as it is in the routes folder to start as routes/index.js - 我可能会编辑它,因为它在路由文件夹中以路由/index.js 开头
ALSO to add clarity - previously I did not work with nodemon - I was running NPM start to start my project if that makes a difference (ie I can git clone and run npm install && npm start and it will work. ALSO to add clarity - previously I did not work with nodemon - I was running NPM start to start my project if that makes a difference (ie I can git clone and run npm install && npm start and it will work.
Try the below docker configuration for a node application.为节点应用程序尝试以下 docker 配置。 It will work for you.它会为你工作。
node-app:
container_name: node-app
image: node:latest
restart: always
volumes:
- ./:/home/node/app
working_dir: /home/node/app
ports:
- 4000:4000
networks:
- main-network
command: "tail -f /dev/null && npm start"
Here below is package.json下面是 package.json
"main": "index.js",
"scripts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon index.js",
}
Turned out it could not find it as my mac is not type sensitive but ubuntu is.原来它找不到它,因为我的 mac 对类型不敏感,但 ubuntu 是。 Standardizing my file names fixed it.标准化我的文件名修复了它。
This dockerfile worked fine这个 dockerfile 工作正常
FROM node:12.14.0-alpine
LABEL version="1.02"
#create working directories etc.
RUN mkdir -p /app
# Create a /App Directory to install the application in
WORKDIR /app
# Copy all files from the main project to the container
COPY . /app/
RUN npm install
# COPY package*.json ./app/
# RUN npm install
# Standard npm commands
## DELETE THE FOLLOWING WHEN DOCKER CONTAINER IS FIXED
RUN cd node_modules
# create an uploads directory , go into it and add a placeholder file to avoid bug.
RUN mkdir uploads
RUN touch uploads/uploadedFile.csv
#go back into root folder
# RUN cd ..
# RUN ls
# start project and expose port 3000 as localhost
CMD node ./bin/www
EXPOSE 3000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.