Does the npm install on your continuous integration (CI) builds take longer than you’d like? The good news is that you might be able to speed it up. Here’s what you need to know:
- Your project must have a
package-lock.jsonfile for the following command to work. - Instead of using
npm installin your build configuration, usenpm ci("clean install") – this command runs faster thannpm installand is designed for use in CI environments. Read the npm documentation to learn more. - Cache the npm cache files – these are typically stored in the
~/.npmdirectory. - You should generally avoid caching the
node_modulesdirectory in your CI builds as it can break between Node.js versions (andnpm ciautomatically removes this directory before installing anything).
Most CI services have support for caching files between builds. If you’re using GitHub Actions they have an example for caching npm cache files, and CircleCI has built in configuration features for caching.