简体   繁体   English

为 NodeJS 应用程序构建 docker 时找不到模块错误

[英]Can't find module error when building docker for NodeJS app

I wrote a DockerFile for a node application.我为节点应用程序编写了一个 DockerFile。 This is the docker file:这是泊坞窗文件:

FROM node:10.15.0

COPY frontend/  frontend/
WORKDIR frontend/ 
RUN npm install
RUN npm start

When I try to build this Dockerfile, I get this error: ERROR in ./app/main.js Module not found: Error: Can't resolve './ResetPwd' in '/frontend/app'当我尝试构建此 Dockerfile 时,出现此错误: ERROR in ./app/main.js Module not found: Error: Can't resolve './ResetPwd' in '/frontend/app'

So I added RUN ls & RUN ls /app in Dockerfile.所以我在 Dockerfile 中添加了RUN ls & RUN ls /app Both of the files are there!两个文件都在! I'm not familiar with NodeJS and it's build process at all.我不熟悉 NodeJS 和它的构建过程。 Can anybody help me with this?有人可以帮我解决这个问题吗?

Point: I'm not sure if it helps or not, but I'm using Webpack too.要点:我不确定它是否有帮助,但我也在使用 Webpack。

The problem was that our front-end developer considered that node imports are case insensitive and he was using windows.问题是我们的前端开发人员认为节点导入不区分大小写并且他使用的是windows。 I tried to run Dockerfile on mac and that's why it couldn't find the modules.我试图在 mac 上运行 Dockerfile,这就是它找不到模块的原因。 Module name was resetPass!模块名称已重置通过!

This question saved me!这个问题救了我!

hope this helps somebody else.希望这对其他人有帮助。

I have an angular app and I was trying to containerize it using docker.我有一个 angular 应用程序,我试图使用 docker 将它容器化。

I build the app on a windows machine.我在 Windows 机器上构建应用程序。 and I was trying to build it inside a linux container.我试图在 linux 容器中构建它。

the app was building fine on my windows machine and failing with the following error in the docker environment:该应用程序在我的 Windows 机器上构建良好,但在 docker 环境中失败并出现以下错误:

    ERROR in folder1/folder2/name.component.ts: - error TS2307: Cannot find module '../../../folder1/File.name'.
    
    import { Interface1} from '../../../folder1/File.name';

Cannot find module '../../../node_modules/rxjs/Observable.d.ts'.找不到模块“../../../node_modules/rxjs/Observable.d.ts”。 import { Observable } from 'rxjs/observable';从 'rxjs/observable' 导入 { Observable };

it was driving me nuts.这让我发疯。

I saw this question and at first did not think that it was what was going on.看到这个问题,一开始没想到是这么回事。 the next day I decided to build the same app in a linux environment just to make sure.第二天,我决定在 linux 环境中构建相同的应用程序,只是为了确保。 Used WSL 2 and boom:使用 WSL 2 和吊杆:

the real problem!真正的问题!

ERROR in error TS1149: File name '/../../node_modules/rxjs/observable.d.ts' differs from already included file name '/../../node_modules/rxjs/Observable.d.ts' only in casing. ERROR错误TS1149:文件名“/../../node_modules/rxjs/observable.d.ts”从已经包含文件名“/../../node_modules/rxjs/Observable.d.ts”不同之处仅在外壳中。

6 import { Observable } from 'rxjs/observable'; 6 import { Observable } from 'rxjs/observable';

SO it was a casing issue.所以这是一个外壳问题。 I corrected the casing and it builds fine!我纠正了外壳,它构建得很好!

I cant say if this will work for sure since I don't know if npm start actually triggers webpack, but if it doesn't you'll have to add an extra RUN line after the COPY frontend / line我不能说这是否会奏效,因为我不知道npm start真的触发了 webpack,但如果不是,你将不得不在COPY frontend /行之后添加一个额外的RUN

There are a few issues here, try using this docker file instead这里有一些问题,请尝试使用此 docker 文件代替

FROM node:10.15.0

# Copy dependency files and install packages
WORKDIR frontend
COPY frontend/package.* .
RUN npm install

# Copy src down and other stuff
COPY frontend /

# cd to the file with the package.json
WORKDIR /appDir/frontend

# Command that executes when container starts up
CMD ["npm", "start"]

Make sure that you also update your .dockerignore to include node_modules.确保您还更新了 .dockerignore 以包含 node_modules。 You'll have to build and run the container with the following commands.您必须使用以下命令构建和运行容器。

docker build -t frontendApp .
docker run -p 8080:8080 frontendApp

The -p and 8080:8080 have to do with exposing internal ports to the outside world so you can view it in a browser, just change it to whatever port web pack is using to display your stuff. -p 和 8080:8080 与将内部端口暴露给外部世界有关,以便您可以在浏览器中查看它,只需将其更改为 web pack 用于显示您的内容的任何端口即可。

I had to rebuild the disruptive package, like in this issue for node-sass我不得不重建破坏性的包,就像在这个问题中为 node-sass

The command would be npm rebuild <package-name>命令将是npm rebuild <package-name>

For me, this was npm rebuild node-sass对我来说,这是npm rebuild node-sass

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

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