无法启动“npm”:确保已安装“npm”并且可以找到

Ill*_*u36 7 visual-studio node.js npm reactjs

在 Visual Studio 2019 v16.4.1 中尝试运行安装了 React 的 ASP.NET Core Web 应用程序时,我在加载网页中收到错误消息:

Failed to start 'npm': Ensure that 'npm' is installed and can be found in one of the PATH directories.
Run Code Online (Sandbox Code Playgroud)

在系统环境变量中,Path 包括 C:\Program Files\nodejs 和 C:\Users\me\AppData\Roaming\npm 等。

在 VS 2019 包管理器控制台中输入“npm”不会产生错误并以 npm@6.12.1 C:\Program Files\nodejs\node_modules\npm. 所以这看起来没问题。

页面上的 InnerException 包括:

System.ComponentModel.Win32Exception (267): The directory name is invalid.
Run Code Online (Sandbox Code Playgroud)

此错误已在以下位置记录并标记为已解决:

https://github.com/aspnet/AspNetCore/issues/6342

"but the real rub is hidden in the InnerException:

System.ComponentModel.Win32Exception: The directory name is invalid.
"
Run Code Online (Sandbox Code Playgroud)

但是我仍然遇到 VS 2019 的问题...

任何想法如何解决这个问题?

System.AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'. To resolve this:.

[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.
    Current PATH enviroment variable is: C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\PuTTY\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\nodejs\;C:\Users\00082563\AppData\Local\Microsoft\WindowsApps;C:\Users\00082563\.dotnet\tools;C:\Users\00082563\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\00082563\AppData\Roaming\npm;C:\Users\00082563\.nuget\packages\node.js\5.3.0\tools
    Make sure the executable is in one of those directories, or update your PATH.

[2] See the InnerException for further details of the cause.))

 ---> System.AggregateException: One or more errors occurred. (Failed to start 'npm'. To resolve this:.

[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.
    Current PATH enviroment variable is: C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\PuTTY\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\nodejs\;C:\Users\00082563\AppData\Local\Microsoft\WindowsApps;C:\Users\00082563\.dotnet\tools;C:\Users\00082563\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\00082563\AppData\Roaming\npm;C:\Users\00082563\.nuget\packages\node.js\5.3.0\tools
    Make sure the executable is in one of those directories, or update your PATH.

[2] See the InnerException for further details of the cause.)

 ---> System.InvalidOperationException: Failed to start 'npm'. To resolve this:.

[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.
    Current PATH enviroment variable is: C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\PuTTY\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\nodejs\;C:\Users\00082563\AppData\Local\Microsoft\WindowsApps;C:\Users\00082563\.dotnet\tools;C:\Users\00082563\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\00082563\AppData\Roaming\npm;C:\Users\00082563\.nuget\packages\node.js\5.3.0\tools
    Make sure the executable is in one of those directories, or update your PATH.

[2] See the InnerException for further details of the cause.

 ---> System.ComponentModel.Win32Exception (267): 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)

   --- End of inner exception stack trace ---

   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)

   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

   at System.Threading.Tasks.Task`1.get_Result()

   at Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer.ReactDevelopmentServerMiddleware.<>c.<Attach>b__2_0(Task`1 task)

   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()

   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)

   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)

--- End of stack trace from previous location where exception was thrown ---

   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)

   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

   --- End of inner exception stack trace ---

   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)

   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

   at System.Threading.Tasks.Task`1.get_Result()

   at Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout[T](Task`1 task, TimeSpan timeoutDelay, String message)

   at Microsoft.AspNetCore.SpaServices.Extensions.Proxy.SpaProxy.PerformProxyRequest(HttpContext context, HttpClient httpClient, Task`1 baseUriTask, CancellationToken applicationStoppingToken, Boolean proxy404s)

   at Microsoft.AspNetCore.Builder.SpaProxyingExtensions.<>c__DisplayClass2_0.<<UseProxyToSpaDevelopmentServer>b__0>d.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)

   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Run Code Online (Sandbox Code Playgroud)

我已将问题提交至:

https://developercommunity.visualstudio.com/content/problem/858687/failed-to-start-npm-ensure-that-npm-is-installed-a.html

Fun*_*y33 13

此处讨论并解决了此问题:

https://github.com/dotnet/aspnetcore/issues/6342#issue-395713046

错误消息是一个红鲱鱼。真正的问题是您的客户端应用程序的路径是错误的。Visual Studio 模板将其放在错误的位置。

打开 startup.cs,查找 Configuration.RootPath 和 spa.Options.SourcePath,并确保它们设置为真实路径。如果没有,请修复它们。


小智 4

我遇到了同样的错误。我以管理员身份打开 cmd 并npm install -g npm按照 user12076510 所说运行命令并重新启动系统 - 错误消失了