javascript - docker 中的错误(找不到我导出的模块),运行 npm start 时没有问题
问题描述
Error: Cannot find module '../controllers/thermostatLogic'
这是我的码头文件
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
我运行docker build -t hvacdoctor .
命令,然后。当它运行 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
码头工人运行 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)
这一切都适用于 npm install & npm start 没有错误 OUT of docker 但为什么这是其中的一个问题?
我尝试了几种不同的方法(虽然 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
编辑 - 这是当前文件夹结构的屏幕截图。
想法?
编辑 - 建议我使用以下 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",
}
^^ 使用这个建议的启动脚本 nodemon 递归地启动 index.js - 如果我从命令中删除 nodemon 它找不到 index.js。- 我可能会编辑它,因为它在路由文件夹中以路由/index.js 开头
还要增加清晰度 - 以前我没有使用 nodemon - 我正在运行 NPM start 来启动我的项目,如果这会有所不同(即我可以 git clone 并运行 npm install && npm start 它会工作。
解决方案
为节点应用程序尝试以下 docker 配置。它会为你工作。
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"
下面是 package.json
"main": "index.js",
"scripts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon index.js",
}
推荐阅读
- typescript - Typescript:转换所有函数参数的通用函数类型
- java - 不推荐使用 userVisibleHint 的 getter 和 setter,在 Java 中不推荐使用
- c++ - 这个程序在 gcc 编译器中的输出是分段错误(核心转储)。我做错了什么?
- windows - Dockerfile 安装多个 msi
- sql - Oracle SQL Where 子句
- java - 关于使用子字符串而不必减去 1 的问题
- pandas - 我如何检查特定列是否具有任何行的相似值,. 一个类似的例子,这种情况是 456 类似于 654
- c - 逐像素模糊 c 中的图像 - 特殊情况
- ios - 没有名为“ChatInboxDelegate”类的类型或协议
- python - 如何为以下 Python 函数编写文档字符串?