Skip to main content
← Back to workflows

How to install npm dependencies in GitHub Actions with caching

bahmutov-npm-install -
GitHub Action
v1.10.2
652
Contributors
Contributor - bahmutov
Categories
CICUBE ANALYTICS INSIGHTS
Engineering Velocity: 25% Team Time Lost to CI Issues
View Platform →
3.5h
Time Saved/Dev/Week
40%
Faster Releases
Click for next insight
Usage
name: main
on: [push]
jobs:
build-and-test:
runs-on: ubuntu-latest
name: Build and test
steps:
- uses: actions/checkout@v4
- uses: bahmutov/npm-install@v1
- run: npm t

npm-install logo

npm-install

GitHub Action for install npm dependencies with caching without any configuration


Using the bahmutov/npm-install action simplifies dependency management in our CI/CD pipelines. It provides flexibility in handling different project structures and optimizes caching strategies to improve build times.

These configurations will help us efficiently manage dependencies, reduce build times with optimized caching, and ensure consistent environments across different workflows. This setup enhances our CI/CD process, making it more reliable and faster.

How to handle subfolders in your repository?

If your repository contains packages in separate folders, use the following configuration

name: main
on: [push]

jobs:
build-and-test:
runs-on: ubuntu-latest
name: Build and test
steps:
- uses: actions/checkout@v4

- uses: bahmutov/npm-install@v1
with:
working-directory: app1
- uses: bahmutov/npm-install@v1
with:
working-directory: app2

- name: App1 tests
run: npm t
working-directory: app1
- name: Run app2
run: node .
working-directory: app2

This setup installs dependencies for each subfolder and runs specific commands in each.

How to specify multiple subfolders in a single action?

Use the following configuration to handle multiple subfolders

name: main
on: [push]

jobs:
build-and-test:
runs-on: ubuntu-latest
name: Build and test
steps:
- uses: actions/checkout@v4
- uses: bahmutov/npm-install@v1
with:
working-directory: |
app1
app2

This setup installs dependencies for all specified subfolders in a single action.

How to use a lock file?

By default, this action uses a lock file like package-lock.json, npm-shrinkwrap.json, or yarn.lock. To disable this, set useLockFile: false:

- uses: bahmutov/npm-install@v1
with:
useLockFile: false

This might be preferable for building libraries where a lock file is not needed.

How to use time-based rolling cache?

Enable rolling cache to reuse the cache across runs with different lockfiles or dependencies:

- uses: bahmutov/npm-install@v1
with:
useRollingCache: true

This setting prevents cache misses due to changes in dependencies, optimizing build times for large projects.

How to install production dependencies only?

Set the NODE_ENV environment variable to production:

- uses: bahmutov/npm-install@v1
env:
NODE_ENV: production

This ensures only production dependencies are installed, excluding development dependencies.

How to use a custom install command?

Specify your own install command using the install-command input:

- uses: bahmutov/npm-install@v1
with:
install-command: yarn --frozen-lockfile --silent

This allows flexibility in the install process, accommodating specific project requirements.

How to add a cache prefix?

Insert custom cache prefix strings into the cache keys to separate caches for different tools:

- name: Install tool A
uses: bahmutov/npm-install@v1
with:
useLockFile: false
install-command: 'npm install tool-a'
cache-key-prefix: tool-a

- name: Install tool B
uses: bahmutov/npm-install@v1
with:
useLockFile: false
install-command: 'npm install tool-b'
cache-key-prefix: tool-b

This ensures that each tool has a separate cache, preventing conflicts.

How to specify a Node version?

Use the actions/setup-node action before installing dependencies:

- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
- uses: bahmutov/npm-install@v1

This ensures that the specified Node version is used for the installation process.