如何解决sh: react-scripts: command not found问题?

4
我使用create-react-app生成了一个应用程序。
create-react-app myapp

当我运行yarn start或者npm start时,我遇到了以下错误:

react-scripts start

sh: react-scripts: command not found npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! myapp@0.1.0 start: react-scripts start npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the myapp@0.1.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

当我运行npm ls react时:
myapp@0.1.0 /Users/johnno/foosball 
└── react@16.2.0

react-scripts目前已存在。

这是我的package.json文件:

{
  "name": "myapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-scripts": "1.0.17",
    "redux": "^3.7.2",
    "yarn": "^1.3.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

我尝试了这个建议,但仍然无济于事。如何解决这个问题?


sudo npm start 没有任何区别。 - bier hier
see above for result - bier hier
很好,现在我们知道问题出在哪里了。你的包管理器没有正确安装所有的节点模块。我使用Yarn,所以我建议你也使用它。首先卸载Yarn,然后按照这里的说明进行安装 https://yarnpkg.com/en/docs/install 现在使用 rm -rf node_modules 命令删除节点模块,然后再次运行yarn。这应该可以解决问题。 - sudo bangbang
Npm start 也应该可以正常工作,对吧? - bier hier
让我们在聊天中继续这个讨论。点击此处进入聊天室 - sudo bangbang
显示剩余8条评论
2个回答

9
我通过删除以下文件夹和文件来解决了这个问题:

node_modules
yarn.lock
manifest_lock.json

然后重新安装了必要的软件包。
npm install

或者

yarn

1
是的,谢谢,这对我也起作用了。一般来说,我期望所有的“lock”文件都应该被删除。(当然,运行npm install以重新生成node_modules/。) - nydame
我正在尝试在一个基于Web的项目中修复这个问题,“yarn”对我很有效。谢谢。 - Muzammil

0

以下步骤适用于我:

  1. 删除 node_modules 文件夹
  2. 如果你使用 yarn 命令,则删除 yarn.lock,如果你使用 npm 命令,则删除 package-lock.json
  3. 运行 yarn install(使用 yarn 命令)或者 npm install(使用 npm 命令)

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