我有一个React应用程序,我正在尝试Dockerize。以下是我的Dockerfile和Docker-compose:
第一个
不知何故,我需要指定npm版本,也安装react-scripts,否则它会在另一台计算机上出错。除此之外,在我的计算机上一切都运行良好,然而,我的目标是任何人都可以克隆我的项目,并通过简单地运行“docker-compose up”来构建它。
我在我同事的电脑上测试了它,错误如下:
Error: Cannot find module 'react'
WEB-CNSS | Require stack:
WEB-CNSS | - /usr/local/lib/node_modules/react-scripts/scripts/start.js
WEB-CNSS | at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
WEB-CNSS | at Function.resolve (node:internal/modules/cjs/helpers:108:19)
WEB-CNSS | at Object.<anonymous> (/usr/local/lib/node_modules/react-scripts/scripts/start.js:43:31)
WEB-CNSS | at Module._compile (node:internal/modules/cjs/loader:1101:14)
WEB-CNSS | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
WEB-CNSS | at Module.load (node:internal/modules/cjs/loader:981:32)
WEB-CNSS | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
WEB-CNSS | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
WEB-CNSS | at node:internal/main/run_main_module:17:47 {
WEB-CNSS | code: 'MODULE_NOT_FOUND',
WEB-CNSS | requireStack: [ '/usr/local/lib/node_modules/react-scripts/scripts/start.js' ]
WEB-CNSS | }
也许是我的package.json有一些错误,所以这里也有:
{
"name": "editor",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/runtime": "^7.18.3",
"@convergence/convergence": "^1.0.0-rc.12",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"ace-builds": "^1.4.14",
"bootstrap": "^5.1.3",
"dropzone": "^6.0.0-beta.2",
"easymde": "^2.16.0",
"node-sass": "^7.0.1",
"react": "^18.0.0",
"react-ace": "^9.5.0",
"react-bootstrap": "^2.2.3",
"react-dom": "^18.0.0",
"react-drag-drop-files": "^2.3.7",
"react-dropzone": "^14.2.2",
"react-pro-sidebar": "^0.7.1",
"react-scripts": "5.0.0",
"react-simplemde-editor": "^5.0.2",
"react-sticky-box": "^1.0.2",
"simplemde": "^1.11.2",
"web-vitals": "^2.1.4"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@convergencelabs/ace-collab-ext": "^0.6.0",
"gh-pages": "^4.0.0"
}
}
我还测试了另一个类似的问题的答案张贴在这里的StackOverflow,但他们没有工作。
3条答案
按热度按时间jaxagkaj1#
您能否使用此模式检查是否再次出现此错误
mrfwxfqh2#
请按如下所示移除文档文件中的命令行:
希望这对你有帮助。
bwitn5fc3#
当您的Dockerfile
COPY
在package.json
和package-lock.json
文件中时,您实际上从未使用过RUN npm install
或类似命令。Dockerfile通常使用npm ci
以获得更好的可重复性。请将其放在包元数据中的COPY
之后。为什么它能在你的系统上工作呢?我想到了两个想法,两个都指向了在你的设置中要验证的东西。
您应该确保您有一个排除主机库树的
.dockerignore
文件,该文件至少包含以下行如果你没有这个,那么后面的
COPY ./ ./
行将复制主机的node_modules
目录,覆盖npm ci
安装的目录。在没有npm ci
的原始形式中,这可能是唯一提供node_modules
目录的东西,但它依赖于首先在主机上运行npm
命令。第二种可能是
node_modules
目录的匿名卷。即使容器被删除并重新创建,匿名卷也会保留下来,其内容会隐藏镜像中的内容。如果您曾经使用过
docker-compose exec web-cnss npm install
这样的调试命令,其结果会保留在匿名卷中,但它不会成为镜像的一部分,您也不会看到它对干净安装的影响。