0

i'm new to Asp.Net Core, Visual Studio and so on. A project i'm working on has an error which occurs just on my machine whenever i try to run the Solution. Here is the error:

The command "npm install && npm run build --prefix /my_path_to_project/MyProject.Web/" exited with code 127.

The error occurs in MyProject.Web.csproj where i have something like:

 <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="npm install &amp;&amp; npm run build --prefix $(ProjectDir)" />
  </Target>

The path of ProjectDir is correct (at least looking from the error message).

I'm able to run successfully npm i && npm build from my terminal in the project directory and from Visual Studio terminal. But when i run the Solution i hit the error. Removing those lines, the project runs properly.

I'm working on Mac 10.13

with Visual Studio 2019 v8.7

I have installed node and npm with nvm

node v12.18.3

npm v6.14.7

Does anyone ever had this problem? My guesses are:

  • Visual Code cannot find where npm is on my machine (but i have no idea how to investigate in this)
  • More rights are needed to run npm command, like sudo (very weird but at this point it might be)

Would be amazing if someone can explain me how Visual Studio find and run npm commands.

UPDATE It turn out that installing Node manually from source solved all the problems. Look like NVM install node in position that is not where VS go to search for it.

Does anyone use NVM with Visual Studio?? Would be very helpful

Giorgia Sambrotta
  • 1,133
  • 1
  • 15
  • 45
  • Not sure why you want to execute the npm script in the Web.csproj file. Generally, we are use it to install and manage the packages in asp.net core application and Visual Studio. To find the Package Manager Console, you could click the Visual Studio **Tools** -> **Nuget Package Manager** -> **Package Manager Console**. Then, you could find the [Package Manager Console panel](https://i.stack.imgur.com/pI0Kp.png). Besides, you could also [Install and manage packages using the NuGet Package Manager](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-visual-studio) – Zhi Lv Aug 11 '20 at 07:47
  • is a project already set up by others and as i said i don't know much about asp.net so unfortunately i really don't know why they choose this way. Is that weird? Nobody use npm in asp.net community? I have seen other posts talking about npm installing instead of Nuget packages... – Giorgia Sambrotta Aug 11 '20 at 10:28
  • Also, i'm on mac and when i go in Tools i don't see the Nuget Package Manager 0.o! I will try to research how to get to the package manager consol panale on mac. Thanks for your screenshots, they were very helpful! – Giorgia Sambrotta Aug 11 '20 at 10:29
  • Add npm to your PATH environment variable. – Adam Vincent Aug 11 '20 at 11:27
  • @AdamVincent if i echo $PATH i have, among other stuff: `/usr/local/share/npm/bin`. Is that what you meant? Also is in bashrc not in bash_profile. Would that make a different? afaik not, they are both reachable globally – Giorgia Sambrotta Aug 11 '20 at 11:48
  • yep. can you call npm commands from your terminal in any directory? `npm -v` – Adam Vincent Aug 11 '20 at 11:53
  • yes i can reach it from everywhere – Giorgia Sambrotta Aug 11 '20 at 11:58
  • Is possible i need to configure Visual Studio to find global npm? – Giorgia Sambrotta Aug 11 '20 at 11:59
  • @ZhiLv-MSFT FYI it turns out VS Mac doesn't have a Package Manager Console!! https://stackoverflow.com/questions/44086572/cant-find-package-manager-console-in-visual-studio-for-mac – Giorgia Sambrotta Aug 11 '20 at 12:04
  • @GiorgiaSambrotta Sorry for my mistake, To opening the NuGet Package Manager Console window in VS mac, you could click the View menu, select Pads, then select NuGet Package Manager Console. More detail information, check [this link](https://lastexitcode.com/blog/2019/05/05/NuGetPowerShellCoreConsoleVisualStudioForMac8-0/). – Zhi Lv Aug 12 '20 at 07:36
  • @ZhiLv-MSFT thanks for the link.I don't have NuGet Package Manager Console under View > Pads infact reading the article you linked me, look like the console is provided by this extension: https://github.com/mrward/monodevelop-nuget-extensions Still good to know i can have a package manager console if needed! :) – Giorgia Sambrotta Aug 12 '20 at 10:19

1 Answers1

0

Since npm command works fine from the project folder you might need to change working directory in your script first. Try the following

cd $(ProjectDir) && npm install && npm run build --prefix $(ProjectDir)
Alexander
  • 9,104
  • 1
  • 17
  • 41
  • thanks for your answer! Unfortunately the error is still there :( I also try to run it as you suggest but without "--prefix $(ProjectDir)" and still no luck. But i also have the feeling is a path problem. I already try to move the project at the root of my machine and in other locations but no luck so far :( – Giorgia Sambrotta Aug 11 '20 at 10:26