npm安装成功,但是npm运行启动无法同时找到rimraf和react-scripts

问题描述 投票:3回答:1

使用Node.js v10.16.0,npm v6.9.0和.Net Core v2.2.300运行Windows 10(内部版本18362)。我在Visual Studio 2017中通过File-> New Project创建了一个示例React / Redux应用程序,选择ASP.NET Core Web Application,然后选择React.js和Redux。但是,通过Visual Studio启动应用程序时,浏览器遇到500错误并显示:

An unhandled exception occurred while processing the request.
AggregateException: One or more errors occurred. (One or more errors occurred. (The NPM script 'start' exited without indicating that the create-react-app server was listening for requests. The error output was: 'rimraf' is not recognized as an internal or external command,

operable program or batch file.
npm ERR! code ELIFECYCLE

npm ERR! errno 1
npm ERR! [email protected] start: `rimraf ./build && react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

[首先,我已经卸载并重新安装了node和npm,删除了node_modules并重新运行npm install。我在重新安装节点之前和之后重新启动。

我知道rimraf已安装在本地.\node_modules目录中,因为我可以成功运行.\node_modules\.bin\rimraf。如果我运行react-scripts start,并且.\node_modules\.bin\react-scripts start成功启动了应用程序的react部分,但是npm为什么会找不到这个?

据我了解,npm在脚本执行时将本地node_modules\.bin添加到PATH中,尽管它提供了指向shell的绝对路径,但仍无法找到rimrafreact-scripts,尽管它们都可以访问。

我还尝试将.\node_modules\.bin添加到我的系统PATH中,这使我(只要我在我的应用目录的根目录中)可以只运行react-scripts start并加载应用程序,但是npm run start仍然失败。

这是我的package.json:

{
  "name": "WebApplication1",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^4.3.1",
    "jquery": "3.3.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^5.0.0-alpha.8",
    "react-scripts": "^1.1.5",
    "reactstrap": "^6.3.0",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "rimraf": "^2.6.2"
  },
  "devDependencies": {
    "ajv": "^6.0.0",
    "babel-eslint": "^7.2.3",
    "cross-env": "^5.2.0",
    "eslint": "^4.1.1",
    "eslint-config-react-app": "^2.1.0",
    "eslint-plugin-flowtype": "^2.50.3",  
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.11.1"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "test": "cross-env CI=true react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint": "eslint ./src/"
  }
}
node.js reactjs visual-studio npm npm-scripts
1个回答
0
投票

这可能是一个路径问题。就我而言,不是在普通的.Net核心Web项目中,而是在TopShelf服务内部托管WebHost。我注意到它从中执行的路径与通过项目模板生成的路径不同:dotnet new react -o my-new-app

在项目模板版本中,它从ClientApp文件夹运行rimraf,而在我的应用程序中,它从bin\Debug\netcoreapp3.0文件夹运行。更改我的ClientApp\package.json以引用正确的路径可以解决此问题,尽管我希望有更好的解决方案:

"scripts": {
  "start": "cd ../../../../ClientApp && cd && .\\node_modules\\.bin\\rimraf ./build && .\\node_modules\\.bin\\react-scripts start",
}
© www.soinside.com 2019 - 2024. All rights reserved.