25

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:

enter image description here

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.<---

===================
Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182

3 Answers3

50

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.

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • 5
    As an additional point: I installed the node.js tools from the visual studio installer and continued to get this error and assumed it must have been installed and there is something else going on. Well, it still wasn't installed. I couldn't find it anywhere on my computer and it didn't work until I downloaded and installed node.js from the website and restarted my computer. – Joel McBeth Aug 14 '18 at 18:35
  • 6
    I had the same issue as I had installed Node while Visual Studio was open. I then restarted VS and everything worked fine. – iltaf khalid Sep 01 '18 at 15:53
  • This worked for me. I downloaded it here: https://nodejs.org/en/ instead of using the package manager. – Tman Sep 28 '20 at 22:11
  • I had the same issue even after dowloading and installing nodejs. my problem solved after restarting visual studio thanks to @iltafkhalid. – afruzan May 02 '21 at 09:39
  • We found similar behavior on Windows when node is installed by Visual Studio but not installed anywhere central (e.g. with `nvm` to %ProgramFiles%\nodejs). – Tim M. Nov 17 '22 at 19:09
0

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"'
Chris Kline
  • 2,249
  • 26
  • 32
0

I would like to add specifically that you might need a reboot even after, like the other answers state, have installed Node.js and restarted Visual studio. The paths were set by the installer, however for an unknown reason I specifically needed to do a reboot for VS to start picking up npm. (I would have written this as a comment to an existing answer however I am not allowed to add comments as of yet.)

Groaznic
  • 21
  • 3