ASP.NET Core和React给出无法启动'npm'/目录名称无效

时间:2019-04-25 09:27:04

标签: reactjs visual-studio asp.net-core

我在Visual Studio 2017中有一个带有Create React App的Core 2.2项目,在该项目暂停了3个月后,我现在返回了,但我无法再运行该应用程序。运行项目时,我在浏览器中收到此错误;

AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'...

它给了我两种解决方法;

Ensure that 'npm' is installed and can be found in one of the PATH directories...

See the InnerException for further details of the cause. ---> System.ComponentModel.Win32Exception: The directory name is invalid
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at Microsoft.AspNetCore.NodeServices.Npm.NpmScriptRunner.LaunchNodeProcess(ProcessStartInfo startInfo)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.NodeServices.Npm.NpmScriptRunner.LaunchNodeProcess(ProcessStartInfo startInfo)
at Microsoft.AspNetCore.NodeServices.Npm.NpmScriptRunner..ctor(String workingDirectory, String scriptName, String arguments, IDictionary`2 envVars)
at Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer.ReactDevelopmentServerMiddleware.StartCreateReactAppServerAsync(String sourcePath, String npmScriptName, ILogger logger)

Npm已安装并且位于建议的目录之一中,因此不应该成为问题。文件没有更改,因此无法确定目录名称为何无效-自上次以来唯一的更改是Visual Studio版本控制升级。我已经安装了VS2019,但它给了我同样的错误。在VS中进行调试不会中断,该错误只会在浏览器中显示。

该怎么办?

2 个答案:

答案 0 :(得分:0)

来自GitHub上的以下问题:https://github.com/aspnet/AspNetCore/issues/6342#issue-395713046

  
      
  1. JavaScriptServices将对npm的调用中的任何失败报告为NPM的PATH问题,   当合理的可能性(如果不太可能)是真正的问题是SourcePath设置为不存在的目录时。
  2.   
  3. 我们的“ ClientApp”模板的SourcePath并非在所有情况下都有效
  4.   

答案 1 :(得分:0)

如下设置源路径对我有用;

app.UseSpa(spa => 
{
       spa.Options.SourcePath = env.ContentRootPath+ "\\..\\" + "ShifuCodeClient";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
});