【问题标题】:Cannot find react-scripts when running react app as a docker container将反应应用程序作为 docker 容器运行时找不到反应脚本
【发布时间】:2022-01-13 12:21:31
【问题描述】:

我有一个我正在尝试 dockerize 的 react 应用程序。以下是我的 Dockerfile 的内容

FROM node:alpine
WORKDIR /src
COPY package.json /src
RUN npm install
COPY . /src
EXPOSE 3000
CMD ["npm", "run", "start"]

在构建 docker 文件时出现以下错误。

 > [5/6] RUN npm install:
#9 13.99 npm ERR! code ERESOLVE
#9 13.99 npm ERR! ERESOLVE unable to resolve dependency tree
#9 13.99 npm ERR! 
#9 14.00 npm ERR! While resolving: <project-name>@1.1.6
#9 14.00 npm ERR! Found: react@17.0.2
#9 14.00 npm ERR! node_modules/react
#9 14.00 npm ERR!   react@"^17.0.2" from the root project
#9 14.00 npm ERR! 
#9 14.00 npm ERR! Could not resolve dependency:
#9 14.00 npm ERR! peer react@"^15.0.1 || ^16.0.1" from react-text-loop@2.3.0
#9 14.00 npm ERR! node_modules/react-text-loop
#9 14.00 npm ERR!   react-text-loop@"^2.3.0" from the root project
#9 14.00 npm ERR! 
#9 14.00 npm ERR! Fix the upstream dependency conflict, or retry
#9 14.00 npm ERR! this command with --force, or --legacy-peer-deps
#9 14.00 npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
#9 14.00 npm ERR! 
#9 14.00 npm ERR! See /root/.npm/eresolve-report.txt for a full report.
#9 14.00 
#9 14.00 npm ERR! A complete log of this run can be found in:
#9 14.00 npm ERR!     /root/.npm/_logs/2021-12-08T14_34_26_174Z-debug-0.log

然后我将运行命令更改为使用RUN npm i -g npm,如下所示。

FROM node:alpine
WORKDIR /src
COPY package.json /src
RUN npm i -g npm
COPY . /src
EXPOSE 3000
CMD ["npm", "run", "start"]

这似乎构建了没有错误的图像,但是当我尝试将 docker 图像作为容器运行时,容器退出并且在获取日志时我看到以下错误

> <projectname>-ui@1.1.6 start
> react-scripts start

sh: react-scripts: not found

更新

当我使用RUN npm i --legacy-peer-deps 时出现以下错误

#10 103.3 npm notice 
#10 103.3 npm notice New minor version of npm available! 8.1.4 -> 8.2.0
#10 103.3 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.2.0>
#10 103.3 npm notice Run `npm install -g npm@8.2.0` to update!
#10 103.3 npm notice 
#10 103.3 npm ERR! code 1
#10 103.3 npm ERR! path /src/node_modules/node-sass
#10 103.3 npm ERR! command failed
#10 103.3 npm ERR! command sh -c node scripts/build.js
#10 103.3 npm ERR! Building: /usr/local/bin/node /src/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
#10 103.3 npm ERR! gyp info it worked if it ends with ok
#10 103.3 npm ERR! gyp verb cli [
#10 103.3 npm ERR! gyp verb cli   '/usr/local/bin/node',
#10 103.3 npm ERR! gyp verb cli   '/src/node_modules/node-gyp/bin/node-gyp.js',
#10 103.3 npm ERR! gyp verb cli   'rebuild',
#10 103.3 npm ERR! gyp verb cli   '--verbose',
#10 103.3 npm ERR! gyp verb cli   '--libsass_ext=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_cflags=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_ldflags=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_library='
#10 103.3 npm ERR! gyp verb cli ]
#10 103.3 npm ERR! gyp info using node-gyp@7.1.2
#10 103.3 npm ERR! gyp info using node@17.2.0 | linux | x64
#10 103.3 npm ERR! gyp verb command rebuild []
#10 103.3 npm ERR! gyp verb command clean []
#10 103.3 npm ERR! gyp verb clean removing "build" directory
#10 103.3 npm ERR! gyp verb command configure []
#10 103.3 npm ERR! gyp verb find Python Python is not set from command line or npm configuration
#10 103.3 npm ERR! gyp verb find Python Python is not set from environment variable PYTHON
#10 103.3 npm ERR! gyp verb find Python checking if "python3" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python3" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python3" is not in PATH or produced an error
#10 103.3 npm ERR! gyp verb find Python checking if "python" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python" is not in PATH or produced an error
#10 103.3 npm ERR! gyp verb find Python checking if "python2" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python2" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python2" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
#10 103.3 npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
#10 103.3 npm ERR! gyp ERR! find Python checking if "python3" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python checking if "python" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python checking if "python2" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python2" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! find Python **********************************************************
#10 103.3 npm ERR! gyp ERR! find Python You need to install the latest version of Python.
#10 103.3 npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#10 103.3 npm ERR! gyp ERR! find Python you can try one of the following options:
#10 103.3 npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#10 103.3 npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
#10 103.3 npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
#10 103.3 npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
#10 103.3 npm ERR! gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
#10 103.3 npm ERR! gyp ERR! find Python For more information consult the documentation at:
#10 103.3 npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#10 103.3 npm ERR! gyp ERR! find Python **********************************************************
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! configure error 
#10 103.3 npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.fail (/src/node_modules/node-gyp/lib/find-python.js:302:47)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.runChecks (/src/node_modules/node-gyp/lib/find-python.js:136:21)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/src/node_modules/node-gyp/lib/find-python.js:179:16)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (/src/node_modules/node-gyp/lib/find-python.js:266:16)
#10 103.3 npm ERR! gyp ERR! stack     at exithandler (node:child_process:404:5)
#10 103.3 npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:416:5)
#10 103.3 npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
#10 103.3 npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
#10 103.3 npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:475:16)
#10 103.3 npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
#10 103.3 npm ERR! gyp ERR! System Linux 5.10.47-linuxkit
#10 103.3 npm ERR! gyp ERR! command "/usr/local/bin/node" "/src/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
#10 103.3 npm ERR! gyp ERR! cwd /src/node_modules/node-sass
#10 103.3 npm ERR! gyp ERR! node -v v17.2.0
#10 103.3 npm ERR! gyp ERR! node-gyp -v v7.1.2
#10 103.3 npm ERR! gyp ERR! not ok 
#10 103.3 npm ERR! Build failed with error code: 1
#10 103.6 
#10 103.6 npm ERR! A complete log of this run can be found in:
#10 103.6 npm ERR!     /root/.npm/_logs/2021-12-08T14_59_25_774Z-debug.log
------
executor failed running [/bin/sh -c npm i --legacy-peer-deps]: exit code: 1

我做错了什么?

【问题讨论】:

  • 你可能还应该把COPYpackage-lock.json文件放入图片中;如果您已经在主机系统上以某种形式解决了依赖问题,则应该通过锁定文件将其转移到您的映像构建中。
  • 我在 COPY package.json 命令下添加了 COPY package-lock.json 但是复制 package-lock.json 并不能解决问题我仍然遇到相同的 react-scripts not found 错误
  • 也许你在本地使用较旧的 npm 版本,因为依赖错误是较新的东西。但你必须使用npm i --legacy-peer-deps 运行它,它应该可以工作。
  • 还有RUN npm installRUN npm i -g npm 做一些完全不同的事情。一个安装本地依赖项,另一个安装全局 npm。
  • @TheWuif 我也尝试过几次,但都没有成功,我添加了一个更新,他在使用 --legacy-peer-deps 时发布了错误

标签: reactjs docker react-redux dockerfile containers


【解决方案1】:

我认为我遇到的问题是我使用的是 node:alpine 基础映像,这导致了 Python 错误。现在我已将基础映像更改为 node:16.6.1,docker 映像构建良好。

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 1970-01-01
    • 2021-01-05
    • 2018-02-22
    • 1970-01-01
    • 2020-03-19
    • 2018-10-28
    • 2018-09-13
    • 2021-03-30
    相关资源
    最近更新 更多