首页 > 解决方案 > 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 它会工作。

标签: javascriptnode.js

解决方案


为节点应用程序尝试以下 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",
}

推荐阅读