简体   繁体   中英

Release of ASP.NET core App: The command "npm install" exited with code 9009

I have a ASP.NET Core solution with a React frontend. I now have a problem I cannot release the code using the normal release window inside Visual Studio. I publish to a web app inside Azure web apps, and it works perfectly for all my other solutions built the same way.

The code works perfectly locally, and I can run npm install locally without problems.

The error obviously comes from some file not found, when publishing. In my project file I have been debugging and found out this is the challenge:

 <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="wwwroot\dist\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

Error in Visual Studio :

Severity    Code    Description Project File    Line    Suppression State
Error       The command "npm install" exited with code 9009.    Likvido.CreditRisk  C:\Users\MYNAME\Documents\Github\Likvido.CreditRisk\Likvido.CreditRisk\Likvido.CreditRisk\Likvido.CreditRisk.csproj 75  

If I comment out the first three lines ( npm install and the two webpacks), I can publish the solution but without working JavaScript obviously.

Any idea how to solve this? And at least, how to debug it better?

Visual error in Visual Studio :

在此处输入图像描述

The log file refered in the GUI:

09/04/2018 11.07.03
System.AggregateException: One or more errors occurred. ---> System.Exception: Publish failed due to build errors. Check the error list for more details.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.VisualStudio.Web.Publish.PublishService.VsWebProjectPublish.<>c__DisplayClass40_0.<PublishAsync>b__2()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.ApplicationCapabilities.Publish.ViewModel.ProfileSelectorViewModel.<RunPublishTaskAsync>d__88.MoveNext()
---> (Inner Exception #0) System.Exception: Publish failed due to build errors. Check the error list for more details.<---

===================

So this was embarrassing but potentially useful for future Googlers.

The underlying problem was quite simple: it was a new computer which did not have Node.js (or NPM ) installed. That makes the error message quite useful: node was not found!

The solution is simple: install Node.js , make sure it's in your PATH and restart your computer. Then you will solve this problem.

If you install node.js tools from the Visual Studio installer, npm is not in your path by default, so something like this won't work:

<Target Name="RunNPMInstall" BeforeTargets="PreBuildEvent">
    <Exec Command="npm install" />
</Target>

Others have suggested editing your environment to add the path to npm to it, but this is annoying and will break if you change Visual Studio major versions.

Instead, you can do something like this, to make the Exec command reference the common relative sub-path that the node.js tools are installed into by the Visual Studio installer:

<Target Name="RunNPMInstall" BeforeTargets="PreBuildEvent">
    <PropertyGroup>
        <npmPath>"$(VsInstallRoot)\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"</npmPath>
    </PropertyGroup>
    <Exec Command="echo Running '$(npmPath)'" />
    <Exec Command="$(npmPath) install" ConsoleToMsBuild="true" />
    <Exec Command="echo Completed running '$(npmPath)'" />
</Target>

This will execute npm from the right path:

1>Running '"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"'
1>
1>up to date, audited 986 packages in 2s
1>
1>89 packages are looking for funding
1>  run `npm fund` for details
1>
1>30 vulnerabilities (3 low, 6 moderate, 14 high, 7 critical)
1>
1>To address issues that do not require attention, run:
1>  npm audit fix
1>
1>To address all issues possible (including breaking changes), run:
1>  npm audit fix --force
1>
1>Some issues need review, and may require choosing
1>a different dependency.
1>
1>Run `npm audit` for details.
1>Completed running '"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Microsoft\VisualStudio\NodeJs\npm.cmd"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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