Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run npm install only when needed and/or partially

Tags:

We call Gulp from our csproj file as we're using Visual Studio 2013 for this project:

<Target Name="AfterBuild">   <Exec Command="gulp" /> </Target> 

However, since we're still crafting this fresh project, we often extend the gulpfile.js to include new packages. The dev will do e.g. npm install gulp-util --save-dev and write a new task, and all is well.

The developer then checks the gulpfile.js and packages.json in to our VCS. Currently:

  • Teamcity has an extra build step npm install;
  • Every developer has to run npm install manually;

Having to remember something that has to be done manually is not a great spot to be in. At one point we had this in our csproj file inside the Task...

 <Exec command="npm install" /> 

...just before the gulp exec, so developers could not "forget" to take this manual step. However, this can take one or even multiple seconds on each (re)build, which is annoying.

Is there any better way to solve this? How do you handle updates to packages.json in projects where large(ish) teams develop using Visual Studio?

like image 266
Jeroen Avatar asked Feb 16 '16 14:02

Jeroen


People also ask

What does npm run clean do?

The npm clean-install command (or npm ci for short) is an in-place replacement for npm install with two major differences: It does a clean install: if the node_modules folder exists, npm deletes it and installs a fresh one. It checks for consistency: if package-lock.

Do I need to run npm install for every project?

No, once you run npm install, packages should be downloaded/installed in the node_modules folder.

What is the difference between npm install and npm install?

npm install , or npm i , is used to install dependencies: It will install all the dependencies. If you use ^ or ~ when you specify the version of your dependency, npm may not install the exact version you specified. npm install can update your package-lock.

Is there any difference between npm I & npm install?

There is no difference, since "npm i" is an alias for "npm install". They both do the exact same thing (install or update all the dependencies in your package-lock.


1 Answers

An approach that works for me is to use MSBuild incremental build support in combination with a stamp file:

<PropertyGroup>   <!-- File with mtime of last successful npm install -->   <NpmInstallStampFile>node_modules/.install-stamp</NpmInstallStampFile> </PropertyGroup> <Target Name="NpmInstall"   BeforeTargets="BeforeBuild"   Inputs="package.json"   Outputs="$(NpmInstallStampFile)">   <Exec Command="npm install" />   <Touch Files="$(NpmInstallStampFile)" AlwaysCreate="true" /> </Target> 

The NpmInstall Target only runs when package.json is newer than node_modules/.install-stamp and it touches that file after a successful npm install. This way npm install is only run once after each change to package.json.

like image 173
Kevinoid Avatar answered Oct 15 '22 02:10

Kevinoid