简体   繁体   English

npm install 成功,但 npm run start 无法找到 rimraf 和 react-scripts 尽管它们已安装

[英]npm install succeeds but npm run start fails to find both rimraf and react-scripts despite them being installed

Running Windows 10 (Build 18362), using Node.js v10.16.0, npm v6.9.0, and .Net Core v2.2.300.运行 Windows 10(内部版本 18362),使用 Node.js v10.16.0、npm v6.9.0 和 .Net Core v2.2.300。 I created a sample React/Redux application in Visual Studio 2017 via File->New Project, selecting ASP.NET Core Web Application, then selecting React.js and Redux.我通过 File->New Project 在 Visual Studio 2017 中创建了一个示例 React/Redux 应用程序,选择 ASP.NET Core Web 应用程序,然后选择 React.js 和 Redux。 However, when starting the application via Visual Studio, the browser encounters a 500 error and says:但是,当通过 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! WebApplication1@0.1.0 start: `rimraf ./build && react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the WebApplication1@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

To start, I've uninstalled and reinstalled node and npm, deleted node_modules and re-ran npm install .首先,我卸载并重新安装了 node 和 npm,删除了 node_modules 并重新运行了npm install I rebooted before and after re-installing node.我在重新安装节点之前和之后重新启动。

I know that rimraf is installed in the local .\\node_modules directory, as I can successfully run .\\node_modules\\.bin\\rimraf .我知道 rimraf 安装在本地.\\node_modules目录中,因为我可以成功运行.\\node_modules\\.bin\\rimraf And react-scripts start starts the react portion of the application successfully if I run .\\node_modules\\.bin\\react-scripts start , but why would npm fail to find this?react-scripts start启动该应用程序的反应部分成功,如果我跑.\\node_modules\\.bin\\react-scripts start ,但为什么故宫不能找到呢?

It's to my understanding that npm adds the local node_modules\\.bin to the PATH at the time of script execution, yet it still fails to find both rimraf and react-scripts despite them being accessible if providing an absolute path to the shell.据我了解,npm 在脚本执行时将本地node_modules\\.bin到 PATH 中,但它仍然无法找到rimrafreact-scripts尽管如果提供绝对路径到 shell 可以访问它们。

I have also tried adding .\\node_modules\\.bin to my system PATH, which allows me (provided I'm in the root of my app directory) to just run react-scripts start and have the application load, but npm run start still fails.我还尝试将.\\node_modules\\.bin添加到我的系统路径,这允许我(假设我在我的应用程序目录的根目录中)只运行react-scripts start并加载应用程序,但npm run start仍然失败。

Here is my package.json:这是我的 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/"
  }
}

有同样的问题,而不是调试选项 IIS Express,选择调试选项 [您的应用程序名称]。

I resolved this running the ClientApp separately with yarn start and changing a command on Configure method of Startup.cs file:我解决了这个问题,单独运行ClientApp并使用yarn start并更改Startup.cs文件的Configure方法上的命令:

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
        //spa.UseReactDevelopmentServer(npmScript: "start");  <--- Remove this line

        // ****
        // **** Add this line
        // ****
        spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
    }
});

This enables it to start and restart faster.这使它能够更快地启动和重新启动。 It's no longer waiting for your React app to rebuild each time.它不再每次都等待您的 React 应用程序重新构建。

Used as reference: https://docs.microsoft.com/en-us/aspnet/core/client-side/spa/react?view=aspnetcore-3.1&tabs=netcore-cli用作参考: https : //docs.microsoft.com/en-us/aspnet/core/client-side/spa/react?view=aspnetcore-3.1&tabs=netcore-cli

It could likely be a pathing problem.这可能是路径问题。 In my case, instead of a vanilla .Net core web project I am hosting a WebHost inside of a TopShelf service.就我而言,我在 TopShelf 服务中托管了一个 WebHost,而不是普通的 .Net 核心 Web 项目。 I noticed that the path it gets executed from is different than the one generated via the project template: dotnet new react -o my-new-app .我注意到它执行的路径与通过项目模板生成的路径不同: dotnet new react -o my-new-app

In the project template version it runs rimraf from the ClientApp folder, whereas in my app it was running from the bin\\Debug\\netcoreapp3.0 folder.在项目模板版本中,它从 ClientApp 文件夹运行rimraf ,而在我的应用程序中,它从bin\\Debug\\netcoreapp3.0文件夹运行。 Changing my ClientApp\\package.json to reference the correct path fixes the problem though I wish there was a better solution:更改我的ClientApp\\package.json以引用正确的路径可以解决问题,但我希望有更好的解决方案:

"scripts": {
  "start": "cd ../../../../ClientApp && cd && .\\node_modules\\.bin\\rimraf ./build && .\\node_modules\\.bin\\react-scripts start",
}

I had de the same problem.我有同样的问题。 I am using VStudio 2019 and I was doing a React test App.我正在使用 VStudio 2019 并且我正在做一个 React 测试应用程序。

I had created the APP, then in that directory I installed node and npm.我创建了 APP,然后在该目录中安装了 node 和 npm。 I think that was the mistake.我认为那是错误。

I solved it, starting from zero... I Create a New Test Directory, then installed npm in that directory (node was already installed) and finally created the APP.我解决了,从零开始...我创建一个新的测试目录,然后在该目录中安装npm(节点已经安装),最后创建了APP。 When I run the APP it appeared a message saying "npm installing".. and it works..当我运行该应用程序时,它出现一条消息说“npm 正在安装”.. 并且它有效..

When the App is created, node and npm have to be already installed.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM