Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to speedup npm ci using cache?

for now npm ci is the most common way to install node modules when using CI. But it is honestly really slow. Is there a way to speedup npm ci using cache or do not fully remove existing packages (whole node_modules folder)?

like image 839
Nedudi Avatar asked Mar 18 '19 21:03

Nedudi


People also ask

Does npm CI cache?

Caching node dependencies is one of circleci's features. In the first build, one can run npm install , save the resulting node_modules directory identified by a checksum of the package. json and when running the build again and package.

Is it safe to clean npm cache?

clean: Delete all data out of the cache folder. Note that this is typically unnecessary, as npm's cache is self-healing and resistant to data corruption issues. verify: Verify the contents of the cache folder, garbage collecting any unneeded data, and verifying the integrity of the cache index and all cached data.


Video Answer


2 Answers

NPM cache is located in ~/.npm but in most CIs you can only cache things inside your working directory.

What you can do to circumvent this is changing the cache directory to your current directory with npm set cache .npm. The NPM cache will now be located in ./.npm an you can cache this folder between CI jobs.

Example with GitLab CI:

my-super-job:   image: node:13-alpine   script:     - npm set cache .npm     - npm ci    cache:     paths:       - .npm 

EDIT: Just discovered that you can set the config as a command line flag so npm ci --cache .npm should do the same

like image 194
Kiliandeca Avatar answered Sep 22 '22 14:09

Kiliandeca


tl;dr ̶N̶o̶.̶ A little.

npm ci should be preferred in CI because it respects the package-lock.json file. Unlike npm install, which rewrites the file and always installs new versions.

By design this command always purges all local packages, by removing the node_modules directory in the beginning. This is the main reason for long builds. And there is no option to avoid this irritating behaviour.

On a local machine you may speed up npm ci by adding the option --prefer-offline, which tells NPM to ignore the cache minimum time and use locally cached packages right away instead of verifying them against the registry.

However, the NPM cache is located in ~/.npm on Unix, or %AppData%/npm-cache on Windows. These folders are not cacheable by default in most CIs. For example GitLab CI caches only have the repository as available workspace. Other directories like the home directory or system directories (eg. apt) are not cached. Therefore this setting probably wont affect your CI build time.

In older version of NPM the option --progress=false had a significant effect on build time, by removing progress bars. This issue seems to be gone however, I can not measure a noteworthy difference anymore.

A best practice and definitely a gain in speed is to separate packages into production and development. By passing the option --only=production NPM will then ignore development dependencies. Due to the reasons above this wont affect caching.

Update 2021-06: It is now possible to change the cache directory

As pointed out by the comments below, it is now possible to change the location of NPM's cache directory. For example by passing it as argument on every command (--cache .npm) or environment variable (npm_config_cache=.npm). So change this to a path to a temporary directory in your repository, add it to the CI cache stack but exclude it from deployment builds. Then you will be able to use the --prefer-offline argument in CI scripts as well.

Summary:

  • Split dependencies
  • Set up a local cache directory
  • Use npm ci --cache <local cache directory> --prefer-offline --only=production --silent
  • This will speed up the process, but due to the command design it can't be as fast as npm install
like image 30
pixelbrackets Avatar answered Sep 20 '22 14:09

pixelbrackets