使用create-react-app时出现npm start错误

108

我有一个项目已经两周没碰了。现在我想重新运行npm start,但是出现了以下错误。

> react-scripts start

sh: react-scripts: command not found

npm ERR! Darwin 16.0.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v6.7.0
npm ERR! npm  v3.10.3
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! UpScore@0.6.0 start: `react-scripts start`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the UpScore@0.6.0 start script 'react-scripts start'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the UpScore package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     react-scripts start
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs UpScore
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls UpScore
npm ERR! There is likely additional logging output above.
  • Node 6.7.0
  • NPM 3.10.3
  • Mac Sierra 10.12

Package.json

{
  "name": "UpScore",
  "version": "0.6.0",
  "private": true,
  "devDependencies": {
    "react-addons-test-utils": "^15.3.1",
    "react-scripts": "0.4.1",
    "react-test-renderer": "^15.3.1",
    "redux-logger": "^2.6.1"
  },
  "dependencies": {
    "@yoshokatana/medium-button": "^1.1.0",
    "axios": "^0.14.0",
    "bcrypt": "^0.8.7",
    "bcrypt-nodejs": "0.0.3",
    "bcryptjs": "^2.3.0",
    "body-parser": "^1.15.2",
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.4.3",
    "draft-js": "^0.8.1",
    "draft-js-editor": "^1.7.2",
    "draft-js-export-html": "^0.4.0",
    "ejs": "^2.5.2",
    "email-verification": "^0.4.5",
    "express": "^4.14.0",
    "express-session": "^1.14.1",
    "flexboxgrid": "^6.3.1",
    "highlight.js": "^9.6.0",
    "immutable": "^3.8.1",
    "katex": "^0.6.0",
    "lodash": "^4.15.0",
    "markdown-it-mathjax": "^1.0.3",
    "material-ui": "^0.15.4",
    "medium-editor": "^5.22.0",
    "minutes-seconds-milliseconds": "^1.0.3",
    "moment": "^2.15.0",
    "moment-duration-format": "^1.3.0",
    "mongod": "^1.3.0",
    "mongodb": "^2.2.9",
    "mongoose": "^4.6.0",
    "monk": "^3.1.2",
    "morgan": "^1.7.0",
    "normalize.css": "^3.0.3",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "react": "^15.3.1",
    "react-dom": "^15.3.1",
    "react-markdown": "^2.4.2",
    "react-medium-editor": "^1.8.1",
    "react-redux": "^4.4.5",
    "react-redux-form": "^0.14.5",
    "react-rich-markdown": "^1.0.1",
    "react-router": "^2.7.0",
    "react-router-redux": "^4.0.5",
    "react-tap-event-plugin": "^1.0.0",
    "react-tinymce": "^0.5.1",
    "redux": "^3.6.0",
    "redux-form": "^6.0.5",
    "redux-form-material-ui": "^4.0.1",
    "redux-promise-middleware": "^4.0.0",
    "redux-thunk": "^2.1.0",
    "reselect": "^2.5.3",
    "screenfull": "^3.0.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "start:prod": "pushstate-server build",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "server": "cd client/api && pm2 start server.js --watch",
    "proxy": "http://128.199.139.144:3000"
  },
  "eslintConfig": {
    "extends": "./node_modules/react-scripts/config/eslint.js"
  }
}

我也尝试克隆我的仓库,但出现了相同的错误。如果有人能告诉我如何查找原因,谢谢。


4
你尝试过删除 node_modules 文件夹并再次运行 npm install 吗? - Nitsew
1
是的,我清理了 node_modules 并执行了 npm install。在 npm install 过程中没有出现任何错误。 - EQuimper
你能展示一下你的 npm start 命令吗? - Steeve Pitis
我已经添加了 package.json 文件。 - EQuimper
不,我只是尝试了一下,看起来他想要运行,但是我得到了这个错误信息: 无法找到所需的文件。 名称:index.html 他们在公共区域搜索吗?为什么现在我会得到所有这些错误,而上周它还能正常工作?你认为是因为 create-react-app 的新更新引起的吗? - EQuimper
显示剩余3条评论
24个回答

283

创建React应用程序的作者在此。

您绝对不应该全局安装react-scripts
此答案所示,您也不需要package.json中加入./node_modules/react-scripts/bin/

如果您看到以下内容:

npm ERR! UpScore@0.6.0 start: `react-scripts start`
npm ERR! spawn ENOENT

这意味着在第一次安装依赖项时出现了问题。

我建议执行以下三个步骤:

  1. npm install -g npm@latest 更新npm,因为它有时会出现故障。
  2. rm -rf node_modules 删除现有模块。
  3. npm install 重新安装项目依赖项。

这应该解决问题。
如果没有解决,请提交问题并提供您的项目链接以及Node和npm的版本。


3
执行您的步骤时,我遇到了这个错误。提示“rm”不是内部或外部命令。我认为系统还原删除了运行这些Node命令所需的某些内容,但我不知道原因。 - MartinDuo
3
P.P.S. 我手动删除了 node_modules 文件夹并运行了 npm install。现在它可以工作了!感谢您提供有关替换 node_modules 的线索。 - MartinDuo
1
那对我仍然不起作用!!实际上,当我在Heroku上部署时它失败了! - aman_novice
@aman_novice,你有解决这个问题吗?对我来说,在本地工作正常,但在Heroku部署时失败。 - deathandtaxes
@deathandtaxes:老兄,我实际上不记得这个问题是什么了。在为此问题苦苦挣扎数小时后,我最终将应用部署在AWS上。 - aman_novice
显示剩余13条评论

16

看起来你在全局环境中没有 react-scripts。这里有两个可能的解决方法:

npm install -g react-scripts

或者在你的 package.json 文件中修改 script 部分如下:

  "scripts": {
    "start": "./node_modules/react-scripts/bin/react-scripts.js start",
    "start:prod": "pushstate-server build",
    "build": "./node_modules/react-scripts/bin/react-scripts.js build",
    "test": "./node_modules/react-scripts/bin/react-scripts.js test --env=jsdom",
    "eject": "./node_modules/react-scripts/bin/react-scripts.js eject",
    "server": "cd client/api && pm2 start server.js --watch",
    "proxy": "http://128.199.139.144:3000"
  },

现在我遇到了一个问题,index.html似乎在public目录中被create-react-app搜索到了?但以前我从来没有遇到过这种情况。 - EQuimper
1
看一下更新日志吧 ;) https://github.com/facebookincubator/create-react-app/blob/master/CHANGELOG.md#050-september-23-2016 - Steeve Pitis
24
这个答案是错误的。这可能会解决问题,但并不是使用Create React App的预期方式。 - Dan Abramov

10

是的,您不应该全局安装react-scripts,这样行不通。

我认为在我第一次创建项目时(在另一台机器上),我没有使用--save选项,所以对我来说,这解决了问题:

npm install --save react react-dom react-scripts

嗨,--save标志会导致它被本地安装而不是全局安装吗? - lifelonglearner

9

如果你像我一样不太幸运,连续三天都在尝试解决这个问题,而这里提出的所有解决方案都让我失望了...在项目根目录下创建一个.env文件,并添加这段代码SKIP_PREFLIGHT_CHECK=true。祝好运。


我猜你之所以不幸,是有原因的。 你的回答帮了我很多。 - Freduah Gideon
谢谢!突然间我开始收到这些错误,然后问题得到了解决。有人知道这是怎么解决的吗?预检中发生了什么? - HellaDev
这也是唯一帮助我的事情。不过我想知道这是否会在未来引起问题。 - Tejas Jaggi

6

这是为了帮助完全不了解React并且在尝试使用npm install和其他修复方法后,仍然无法启动第一个应用程序的人。我在Windows 10上安装了最新版本的npm create-react-app,但在简单的my-app演示文件夹中进行简单的npm start时,遇到了一个又一个的错误。

一开始看起来与OP错误相似,但实际上略有不同,代码以ERRNO 4058开头,接着是'ENOENT' syscall: 'spawn cmd', path: ''cmd' ...'

最终从github create-react-app论坛弄清楚了,快速解决此问题的方法是将cmd注册到“path”变量中。为此,转到系统属性>环境变量。单击路径变量编辑,添加C:\Windows\System32的新条目。重新启动CMD提示,就可以开始了。


当我所有的高级修复都失败时,这就是我的问题。不知道它是如何被取消设置的,但这就是问题所在。 - Raydot
作为一个初学者,您能告诉我为什么要添加Windows System32路径吗?如果我们添加了Node文件夹路径,我可能会更好地理解。您能解释一下吗? - Yusuf

5

我可能晚回答了这个问题,但这是对我有用的方法,它可能会帮助某些人重新进入开发轨道!

nvm install v12.0 // You may need to install nvm, if not already done
rm -rd node_modules/
npm cache clean --force
npm install

干杯!


5
正如Dan所正确指出的那样,
如果您看到了这个:
npm ERR! UpScore@0.6.0 start: `react-scripts start`
npm ERR! spawn ENOENT

这意味着在第一次安装依赖项时出现了错误。但是,有时运行“npm install -g npm@latest”更新npm可能会导致出现此错误:
npm ERR! code ETARGET
npm ERR! notarget No matching version found for npm@lates
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

因此,我建议您执行以下步骤而不是运行 npm install -g npm@latest

 npm i -g npm //which will also update npm
 rm -rf node_modules/ && npm cache clean // to remove the existing modules and clean the cache.
 npm install //to re-install the project dependencies.

这应该可以让你重新站起来。


2

对于我来说,问题就在于我没有将 react-scripts 添加到项目中,因此:

npm i -S react-scripts

如果这个方法不起作用,那么可以尝试删除node_modules文件夹,正如其他人所建议的那样。
rm -r node_modules
npm i

这对我有用。我在一个新的React项目中遇到了错误,显然react-scripts没有自动安装? - Nemo

2

在Windows中添加环境变量

  1. C:\WINDOWS\System32
  2. C:\Program Files\nodejs
  3. C:\Program Files\nodejs\node_modules\npm\bin
  4. C:\WINDOWS\System32\WindowsPowerShell\v1.0
  5. C:\Users{你的系统名称(不包括花括号)}\AppData\Roaming\npm

这5个是必须要加入路径的。

同时,请使用最新版本的node.js。


2

当我运行

npm start

时,我遇到了相同的错误。

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! protest-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the protest-app@0.1.0 start script.

我在多个标签页上尝试了其他开发人员提供的解决方案,但都没有成功。

最终,在使用Ubuntu操作系统时,我关闭了我的vscode编辑器并重新启动了电脑,所有问题都得到了解决。(哈哈,只是这一个问题)


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