Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM: Only install missing - how to speed up npm install

I have a lot of devdepencencies in my npm script. npm install takes a few minutes the first time, that's ok.

But since I'm integrating with TFS build server, it only needs to npm install once. After that, npm install is just wasting time because it takes 2-3 minutes to just determin the packages are already installed. Also, it seems to always reinstall the packages with -g global flag, even when existing.

How can I make it check if packages exist, and if so, skip npm install?

like image 733
TetraDev Avatar asked May 10 '16 17:05

TetraDev


People also ask

What is npm clean install?

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.

What is npm cache miss?

From Wikipedia, A cache miss is a failed attempt to read or write a piece of data in the cache, which results in a main memory access with much longer latency.

How to make NPM install faster?

The first way to make NPM install faster is to make the progress set to false. So, this makes NPM install packages without showing the progress status. It makes NPM install run 2x faster. 2. The second way is to use PNPM. It’s fast, disk space-efficient package manager. Besides, it’s a lot faster than NPM and Yarn.

What does NPM install-p mean?

`npm install` saves any specified packages into `dependencies` by default. * `-P, --save-prod`: Package will appear in your `dependencies`. This is the default unless `-D` or `-O` are present.

How do I use NPM ci instead of NPM install?

Here’s what you need to know: Your project must have a package-lock.json file for the following command to work. Instead of using npm install in your build configuration, use npm ci ("clean install") – this command runs faster than npm install and is designed for use in CI environments.

Should I use NPM If I never publish my package?

Even if you never publish your package, you can still get a lot of benefits of using npm if you just want to write a node program (a), and perhaps if you also want to be able to easily install it elsewhere after packing it up into a tarball (b). npm install (in package directory, no arguments):


1 Answers

You can use npm-cache as an alternative way if you use on-premise build agents for build.

It is useful for build processes that run [npm|bower|composer|jspm] install every time as part of their build process. Since dependencies don't change often, this often means slower build times. npm-cache helps alleviate this problem by caching previously installed dependencies on the build machine. npm-cache can be a drop-in replacement for any build script that runs [npm|bower|composer|jspm] install.

How it Works

When you run npm-cache install [npm|bower|jspm|composer], it first looks for package.json, bower.json, or composer.json in the current working directory depending on which dependency manager is requested. It then calculates the MD5 hash of the configuration file and looks for a filed named .tar.gz in the cache directory ($HOME/.package_cache by default). If the file does not exist, npm-cache uses the system's installed dependency manager to install the dependencies. Once the dependencies are installed, npm-cache tars the newly downloaded dependencies and stores them in the cache directory. The next time npm-cache runs and sees the same config file, it will find the tarball in the cache directory and untar the dependencies in the current working directory.

And you can also try with npm-install-missing.

However, if you are using VSTS Hosted Build Agent, then you cannot do this since every time you queue a build with Hosted Build Agent, a clean build agent is assigned for the build. That means there is no dependency package installed on the agent. You need to perform a complete npm install.

like image 154
Eddie Chen - MSFT Avatar answered Sep 25 '22 14:09

Eddie Chen - MSFT