How to automate dependency updates with lockfiles

Commit package-lock.json, install with npm ci and let Dependabot raise update pull requests. A step by step guide with the checks that prove it worked.

Dependency updates: Automate npm updates without surprises

TL;DR

  • Goal: keep your npm dependencies current without a person remembering to do it, and without surprise changes in production.
  • Lockfile: commit package-lock.json and install from it in CI and deployments with npm ci, so every build gets the same tree.
  • Automation: add a dependabot.yml file so Dependabot opens pull requests on a schedule, and turn on Dependabot security updates for vulnerable packages.
  • Check: merge only when CI is green, then run npm audit and confirm the lockfile shows the new version.

Why it matters

Most of the code in a Node.js application is somebody else's. When a flaw is published in one of those packages, the fix is usually a small version bump, but the bump has to be noticed, tested and shipped. Teams that update by hand tend to batch the work, and a batch of many changes is harder to test and easier to postpone. Teams that update continuously get small, reviewable changes, and the dependency that matters on the day it matters is already close to current.

Automation brings its own risk. If every install can pick a different version, then a build that passed on Monday may behave differently on Tuesday with no change in your code. The lockfile is what makes automation safe: it records the exact tree, so the only changes that reach production are the ones in a pull request you reviewed. For the related task of moving one vulnerable package to a safe version, see our guide to upgrading a vulnerable npm dependency, and for how Stackurity turns open issues into a short list of fixes, see the remediation documentation. If you are weighing tools for library risk in deployed apps, how Stackurity compares with Snyk sets out the differences.

Before you start

  • A project with a package.json file, in a repository on GitHub, because Dependabot is a GitHub feature. Any other host needs a different tool.
  • Write access to the repository, since the configuration file lives in it.
  • A CI job that runs your tests, so that an automatic pull request is proven before a person merges it.
  • A clean working tree, so the first lockfile change is easy to review.

Step by step

  1. Create and commit the lockfile. Run npm install in the project. npm writes package-lock.json whenever it changes the dependency tree, so commit that file next to package.json. Do not add it to your ignore list. The npm documentation also describes npm-shrinkwrap.json, but it recommends that for publishing command-line tools, not for ordinary applications.
  2. Install from the lockfile in CI and deployments. Replace the install step in your pipeline with npm ci. It removes any existing node_modules folder, installs exactly what the lockfile lists and fails if the lockfile and package.json disagree. That failure is useful: it catches a change someone forgot to commit.
  3. Keep install flags consistent. If you created the lockfile with an option such as --legacy-peer-deps, give the same option to npm ci. The documentation suggests recording it once with npm config set legacy-peer-deps=true --location=project and committing the .npmrc file, so the pipeline and your laptop behave alike.
  4. Turn on Dependabot security updates. In the repository settings, enable Dependabot alerts and Dependabot security updates. Once enabled, a vulnerable dependency that has a patch gets a pull request for the minimum version that includes the fix. GitHub notes that this works on dependencies named in a manifest or lock file.
  5. Control the noise. Version updates can be grouped, limited and delayed. The groups option combines related updates into one pull request, and the cooldown option holds back a brand new release for a number of days. Cooldown is only for version updates: security updates are not delayed by it.
  6. Make CI the gate. Require your test job to pass before any pull request can be merged. An automatic update that fails the tests should wait for a person, and one that passes can be merged quickly.

Add the Dependabot configuration. Create dependabot.yml in the .github folder of the repository. This minimal file, adapted from GitHub's reference, checks the npm dependencies in the root folder every week:

version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"

Keep the indentation of a normal YAML file when you paste it. Choose "daily" instead of "weekly" if you want more frequent pull requests.

How to confirm it worked

After the first pull request is merged, check three things. First, the diff of package-lock.json in the pull request shows the old and new versions, which is the record of what changed. Second, a fresh checkout followed by npm ci succeeds without editing any file, which shows that the lockfile and package.json agree. Third, run npm audit and read the result for the packages you meant to fix.

Also look at the pull request list a week later. If Dependabot opened nothing, check that the file is named dependabot.yml, that it is on the default branch and that the ecosystem is spelled "npm". If many pull requests are open and none merged, tighten the schedule or use groups until the queue is something your team can actually clear.

How Stackurity helps

Dependabot works one repository at a time. Stackurity adds the view across all of them. It reads npm lockfiles in your repositories and matches the packages against vulnerability advisories every hour, together with the CISA Known Exploited Vulnerabilities catalogue. Instead of a list of CVEs it gives you fixes: one per upgrade, with the version to move to and every issue it clears, most urgent first.

Fixes page listing package upgrades such as log4j-core 2.14.1 to a fixed version, ordered by urgency.
Fixes instead of a list of CVEs: each upgrade that clears issues, most urgent first, with every place it is needed.
Recommended actions on the Stackurity dashboard for the fictional Northwind Retail workspace: install available security updates on a server, and upgrade log4j-core 2.14.1 to 2.26.1 in two places.
The dashboard's recommended actions: the few fixes that clear the most urgent issues across every server, application and repository, most urgent first.

After a fix is merged, the Check again action asks for a fresh report and records whether the issue is passed, partly fixed or still open, so you do not rely on a closed pull request alone. To stop a vulnerable change before it ships, the Stackurity scan command can fail a CI build by severity, priority or CISA KEV. Libraries copied into an application outside a package manager or lockfile are not covered, so keep your dependencies in the lockfile. Start a free trial to see the fixes for your own repositories.

FAQ

What is the difference between npm ci and npm install?

The npm documentation says npm ci is built for pipelines that test, integrate and deploy code without a person watching. It needs an existing lockfile, stops with an error if the lockfile and package.json disagree, and never writes to either file. Use npm install on your own machine when you want to change dependencies.

Should I commit package-lock.json?

Yes. The npm documentation says the file is intended to be committed to source control, so that teammates, deployments and continuous integration install exactly the same dependencies and so that changes to the tree show up as readable diffs.

Does Dependabot update dependencies that have no known vulnerability?

Only if you enable version updates. Security updates raise pull requests for vulnerable dependencies, while version updates keep dependencies current even when nothing is wrong. The dependabot.yml file configures the schedule and other settings for both.

Can I delay updates to brand new releases?

For version updates, yes. Dependabot has a cooldown option that holds back a new release for a number of days. It does not apply to security updates, which is the behaviour you want when a fix is urgent.

How do I know an update really fixed the problem?

Merge the pull request, let CI install from the lockfile with npm ci, then run npm audit and look at the result. Stackurity also checks again after a change and records whether the issue is really gone.

Sources

Sources: npm documentation, npm-ci, npm documentation, package-lock.json, GitHub documentation, Dependabot options reference, GitHub documentation, Dependabot security updates and Stackurity product facts.