在Docker中出现"sh: 1: react-scripts: not found"错误

19
我有一个 Docker 容器,在构建时克隆了一个 React.js 应用程序,但在进行 npm 安装时失败了。有谁知道如何解决这个问题吗?
错误信息:
sh: 1: react-scripts: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! myrideweb@0.1.0 build: `react-scripts build`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the myrideweb@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-06-19T04_00_09_065Z-debug.log
Dockerfile:
FROM ubuntu

Run apt-get update

Run apt-get install curl -y

Run curl -sL https://deb.nodesource.com/setup_10.x

Run apt-get install -y nodejs

Run apt-get install -y git

Run apt-get install npm -y

Run apt-get update -y

Run git clone https://github.com/sdrafahl/TerraWeb.git

WORKDIR /TerraWeb

Run npm install -g --save npm@latest

...

从 package.json 安装 npm? - Shankar Shastri
1
你需要在你的Dockerfile中运行 npm install - Ferrybig
7个回答

12

我有类似的问题,通过更改以下内容进行了解决:

"start: "react-scripts start"

更改为:

"start: "./node_modules/.bin/react-scripts start"

package.jsonscripts部分。
我使用经典的node基础图像,但它应该工作得差不多。
而且,我仍在研究为什么NPM无法在Docker内运行时解析react-scripts,非常欢迎任何进一步的帮助:-)


1
最好使用 npx 而不是直接使用节点模块二进制路径。 - Nick Brady

2
当package.json文件中将'react-scripts'列在'devDependencies'下时,我遇到了这个问题。将其移至“dependencies”部分解决了此问题。

0

我遇到了react-scripts@4.0.1的问题。我卸载并重新安装,这似乎解决了问题。

  1. npm uninstall react-scripts
  2. npm install -D react-scripts

0

我最近也遇到了这个问题,但是我已经解决了它,不过我并不太喜欢这个解决方案,但我认为它还可以,因为它应该可以在 Docker 或本地机器上运行。话虽如此,我的建议是更新 package.json 如下:

{
  "scripts": {
    "start": "$(npm bin)/react-scripts start",
  }
}

npm-bin文档


0

在构建容器之前,删除package-lock.json并使用npm install重新安装包可以解决我的问题。


0

好的,我刚刚成功地通过几个简单的步骤解决了这个问题,希望这些说明也能帮助到你。

我在 Windows 10 上运行了一个 Ubuntu 的 Docker,并通过卷共享了 node_modules 文件夹,如果这对你来说很熟悉,那么我相信这个解决方案会对你有所帮助。不小心我从我的 Windows 用户中安装了 node 模块,这就导致了整个权限问题。

所以我做的是删除 node_modules 目录。然后我从 Ubuntu 终端安装了 node 模块,运行 npm install

你可能需要在 Ubuntu 上安装 npm,我使用 curl 下载了 nvm 安装文件,按照这些说明 如何在 Linux 的 WSL 上安装 node,然后你可以运行 npm install,然后 react 脚本就能够运行了。

在你从 Ubuntu 运行 npm install 后,构建并运行你的 Docker。


0
我在运行docker-compose时遇到了这个错误,我尝试了以上的所有方法但都没有解决问题。最后,我运行了docker-compose rm命令,删除了历史容器,并重新运行docker-compose builddocker-compose up,问题得到解决。希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接