Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run npm command prebuild Visual Studio 2019

I am using Visual Studio 2019 to create a new Core RazorPage application. I need to run the following commands inside my WWWRoot folder

npm install
npm run build

I tried to add the following commands inside my pre-build script

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="npm i $(ProjectDir)wwwroot" />
  </Target>

but nothing happen and i am getting the folowing warning

npm WARN saveError ENOENT: no such file or directory, open 'E:\Dev\package.json'

it seems that it completely ignores the wwwroot in the path

like image 583
Pacman Avatar asked Sep 16 '19 18:09

Pacman


2 Answers

VS19 doesn't like not having the package.json at the root of the project. I was able to add a package.json at the root of the project, and add the script:

"scripts": {
    "build": "cd wwwroot && npm i && npm run build"
  },

the node_modules folder was then created in the wwwroot folder and the npm run build command was run inside the child folder. I would also recommend the Extension "NpmTaskRunner" (https://github.com/madskristensen/NpmTaskRunner). You can then bind the root build script to the VS Build event.

like image 196
Ryan Avatar answered Oct 05 '22 08:10

Ryan


After moving an ASP.NET Web Application to ASP.NET Core Application I had, almost, the same problem with npm. If I created the package.json file inside a Project I end up with:

  • npm installing a lot of packages
  • npm command does not run in Package Manager Console
  • I would need package.json (and node_modules directory) inside other Web Application in the same Solution

After struggling for a while I followed a different approach, and it is working up to now:

  • Create a package.json (and node_modules) at the Solution level
  • Install dependencies here
  • Copy path you need to wwwroot using Gulp
  • Create an EMPTY package.json at the Project Level
  • Install del and gulp globally (DEV dependencies)
  • Create a symlink for del and gulp at project level

There is a detailed description of this approach (compared to the usual) at the link below:

A better way to use Visual Studio with npm (and Gulp)

Please let me now if this works for your problem ?

like image 26
user3570484 Avatar answered Oct 05 '22 08:10

user3570484