简体   繁体   English

如何修复运行“找不到模块”错误的 docker 容器?

[英]How to fix docker container running 'cannot find module' error?

I am new to docker and trying to move my MERRN stack application to AWS.我是 docker 新手,正在尝试将我的 MERRN 堆栈应用程序移至 AWS。 I want to dockerize it before I move it.我想在移动它之前对其进行 dockerize。 However, when I followed this online tutorial at Medium - Docker with MERN stack I get to the part with starting up my docker image with docker run -p 5000:5000 -d backend and I get an error in VS code.但是,当我在Medium - Docker with MERN stack 上学习这个在线教程时,我开始使用docker run -p 5000:5000 -d backend启动我的 docker 镜像,并且在 VS 代码中出现错误。 I am not sure what this error means but I theorize it has to do with my folder management.我不确定这个错误是什么意思,但我认为它与我的文件夹管理有关。

I will attach a snippet of my folder structure as well as the docker file I used.我将附上我的文件夹结构的片段以及我使用的 docker 文件。 So my main problem is I am trying to get everything setup but this issue is hindering my progress.所以我的主要问题是我正在尝试设置所有内容,但这个问题阻碍了我的进步。 So any help would be appreciated.所以任何帮助将不胜感激。 Thanks谢谢

在此处输入图片说明

DockerFile: Docker 文件:

FROM node:10.19.0
# Create app directory
WORKDIR /usr/src/app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# Bundle app source
COPY . .
# Install app dependencies
RUN npm install
EXPOSE 5000
CMD [ "npm", "start" ]

Error:错误:

> exodus@1.0.0 start /usr/src/app

> node server


internal/modules/cjs/loader.js:638

throw err;

^


Error: Cannot find module './routes/api/Email'

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> (/usr/src/app/server.js:24: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 Function.Module.runMain (internal/modules/cjs/loader.js:831:12)

at startup (internal/bootstrap/node.js:283:19)

at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! exodus@1.0.0 start: `node server`

npm ERR! Exit status 1



npm ERR!

npm ERR! Failed at the exodus@1.0.0 start script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


npm ERR! A complete log of this run can be found in:

npm ERR! /root/.npm/_logs/2020-10-28T20_13_53_160Z-debug.log

Search...
Stick to bottom

Your Dockerfile seems ok and your project structure, too.你的 Dockerfile 看起来没问题,你的项目结构也是如此。 I assume that the docker image is faulty for some reason and missing the files.我认为 docker 镜像由于某种原因有问题并且丢失了文件。

Can you try to build the image again?您可以尝试再次构建映像吗? Is the COPY step executed correctly? COPY 步骤是否正确执行?

// Make sure to execute this in the project root.
docker build -t backend .

If the error persists, log into the docker container using bash or sh and try to manually start the node server with npm start :如果错误仍然存​​在,请使用 bash 或 sh 登录到 docker 容器并尝试使用npm start手动启动节点服务器:

docker run -p 5000:5000 -d backend /bin/bash
or
docker run -p 5000:5000 -d backend /bin/sh

Logging into the docker container will also help to investigate if eg some files were not transferred.登录 docker 容器也将有助于调查某些文件是否未传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM