Securing Node dependencies and the supply chain

Intermediate · 10 min

Reducing npm supply-chain risk with lockfiles and npm ci, install-script controls, release-age cooldowns, npm audit, provenance and fewer dependencies.

Before thispackage.json fields and exportsnpm scripts and lifecycle hooksPublishing an npm package

Better Stack · 10 min

Securing Node dependencies and the supply chain

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Better StackReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every dependency is code that runs with your permissions, on your laptop at install time and in your server at run time. A typical app has hundreds of transitive packages, and you vet almost none of them.
  • Commit the lockfile and install with `npm ci` in CI and production. It installs exactly what the lockfile says and fails if `package.json` and the lockfile disagree, so a new version cannot slip in.
  • Packages can run code during install through `preinstall`, `install` and `postinstall` scripts. `--ignore-scripts` and npm's `allowScripts` list let you decide which dependencies may do that.
  • `--min-release-age=<days>` makes npm resolve to versions at least that old. Most malicious versions are caught within days, so a cooldown skips the window, at the price of delaying security fixes.
  • `npm audit` reports known vulnerabilities, not malware, and it is noisy. Treat it as a prompt to look at reachable, high-severity findings, and `npm audit signatures` to verify registry signatures and provenance.
  • The safest dependency is the one you do not add. Node now ships a test runner, `fetch`, `--env-file`, `--watch` and more, and each package you skip is attack surface you do not have.

What you are actually installing

npm install express looks like adding one library. It adds that library and everything it depends on, and everything those depend on. A modest Express app routinely pulls in a hundred or more packages from maintainers you have never heard of. Each one runs with your permissions. In your server process it can read your environment variables and secrets. On your laptop, at install time, it can run a script before you have even imported it.

A supply-chain attack targets that trust. An attacker compromises a maintainer’s account, publishes a malicious new version of a popular package, or registers a name one letter off from a popular one and waits for a typo. Recent npm worms have done exactly this: steal tokens from the machines that installed the package, use them to publish poisoned versions of that victim’s own packages, and spread. You cannot audit hundreds of packages by hand, so the defenses are settings and habits that shrink the window and the blast radius. The browser-side version of this is in web security fundamentals for frontend, and package basics are in package managers and npm.

The lockfile and npm ci

package.json holds ranges, such as ^4.18.0. A fresh npm install with a range can resolve to whatever is newest today. package-lock.json records the exact version and integrity hash of every package in the tree. Commit it, and install from it:

npm ci

npm ci removes node_modules, installs exactly the lockfile’s contents, and refuses to continue if the lockfile and package.json disagree. I tested that by adding a dependency to package.json without updating the lock. npm ci stopped with can only install packages when your package.json and package-lock.json ... are in sync. That failure is the feature. It means a build cannot quietly pick up a newer version. Use npm ci in CI and in Docker builds, and use npm install only on a developer machine where you intend to change the lock. The video makes the same point as its “review the lockfile” and “use clean install commands” chapters. When a lockfile diff arrives in a pull request, skim it: a new package, a changed resolved URL pointing somewhere other than the registry, or a git dependency deserves a question.

Install scripts

Packages can declare preinstall, install and postinstall scripts that run during installation, as in npm scripts and lifecycle hooks. Some are legitimate, such as compiling a native addon. Many attacks use them, because they run before any code review of the package’s actual API.

I built a throwaway local package whose postinstall writes a file, and installed it. On npm 11.20 with no configuration, the script ran, and npm printed a warning listing the package as “not yet covered by allowScripts”. Installing with --ignore-scripts did not run it. npm’s own manual for its newer npm install-scripts command says install scripts are blocked by default unless allowed, but I observed the older behaviour with only a warning, so check what your npm version does. What I could confirm is the mechanism. npm install-scripts deny <pkg> writes "allowScripts": { "<pkg>": false } into package.json, and after that a clean install did not run the script. approve records true. The result is a reviewed list that lives in version control.

npm install-scripts ls             # which dependencies have install scripts
npm install-scripts approve <pkg>  # allow one
npm ci --ignore-scripts            # a blunt default for CI

Blocking all scripts breaks packages that need a native build, so approve those one at a time. The video’s “disable scripts” chapter covers the same idea across npm, pnpm and bun.

Cooldowns

Most malicious versions are noticed and removed quickly. A policy of not installing a version until it is a few days old avoids most of them. npm has a setting for it:

npm install lodash --min-release-age=7

min-release-age is in days, and npm resolves to the newest version that is at least that old. In my test, --min-release-age=300 resolved lodash to 4.17.23, a January 2026 release, and a plain install picked 4.18.1. The first of those had two high-severity advisories according to npm audit. That is the trade-off in miniature: a cooldown protects you from fresh poison and delays fresh fixes. Pick a short window such as a few days, apply it to routine updates, and keep a way to override it when a patch is urgent. pnpm and bun have equivalents, which the video covers as its first tip.

Audit, signatures and provenance

npm audit              # known vulnerabilities in your tree
npm audit signatures   # verify registry signatures and publish provenance

npm audit compares your lockfile against a database of reported vulnerabilities. It is useful and noisy: many findings are in code your app never reaches, such as a dev-only tool, and it knows nothing about malware that has not been reported yet. It reported 0 vulnerabilities for my clean test project and 2 high for the older lodash. Read the findings and prioritize ones that are high-severity and reachable.

npm audit signatures checks that package tarballs match the registry’s signature, and reports packages published with provenance, a signed statement of which source repository and build produced them. Provenance does not prove a package is safe. It proves where it came from, which narrows the field when an account is compromised. The publisher’s side, including trusted publishing, is in publishing an npm package. The command needs registry-installed packages, and refused to run on my local-tarball project.

Scanners such as Socket and npq, which the video recommends, take a different route: they analyze what a package’s code does, for example whether it touches the network or the filesystem at install time, and warn before it reaches your machine. They complement audit rather than replace it.

Fewer dependencies

Every package is attack surface and maintenance. Before adding one, ask whether Node already does it. Over the lessons in this roadmap you have seen a test runner (the built-in test runner), fetch, an env-file loader (configuration and environment variables), watch mode and a CLI argument parser, each replacing a popular package. A small helper you can write in ten lines is often safer than a dependency with its own dependency tree. Use npm ls <pkg> or npm explain <pkg> to see why something is in your tree, and remove what you no longer use.

Also avoid git-based dependencies and URLs in package.json for production code, since they skip the registry’s checks and can change under you. Install with exact, reviewed versions in anything security-sensitive.

What the video covers

Better Stack’s ten-minute video, from May 2026, is chaptered across seven settings and habits: do not install brand-new packages (a minimum release age), disable install scripts, block git-based dependencies, scan dependencies with Socket and npq, review the lockfile, use clean install commands, and build better upgrade habits. It covers npm, pnpm and bun, so some configuration keys it shows are not npm’s. It does not discuss provenance or typosquatting at length, which this lesson touches. Its transcript was not available, so I’m describing it from its chapters and description. The npm ci failure, install-script behaviour and release-age results above come from running npm 11.20 locally.

Where this fits

Dependencies are one way untrusted code gets into your process. The next lesson narrows what any code can do once it is there: the permission model and hardening.

Check yourself

  1. 01What is the difference between `npm install` and `npm ci`, and why does CI use the second?

    `npm install` may update the lockfile and resolve newer versions that satisfy the ranges in `package.json`. `npm ci` deletes `node_modules`, installs exactly what the lockfile records, and fails if the lockfile and `package.json` disagree. That makes builds reproducible and stops an unreviewed new version from entering a deploy.

  2. 02A dependency adds a `postinstall` script. Why is that a risk, and how do you limit it?

    The script runs arbitrary code with your user's permissions as soon as the package is installed, before you have imported anything, which is how many supply-chain worms spread. Install with `--ignore-scripts` or, in recent npm, record decisions in `allowScripts` in `package.json` via `npm install-scripts approve` and `deny`, allowing only the few packages that need a native build.

  3. 03What does a release-age cooldown protect against, and what does it cost?

    Compromised or malicious versions are usually discovered and pulled within hours or days, so refusing versions younger than, say, a week avoids most of them. The cost is that legitimate security fixes also arrive later. I saw this in a test: a 300-day cooldown resolved lodash to an old version that `npm audit` flagged with two high-severity issues. Use a short window and keep an escape hatch for urgent fixes.

  4. 04`npm audit` says 0 vulnerabilities. Is the project safe from malicious packages?

    No. `npm audit` checks for vulnerabilities that have been reported and catalogued against a version, and a freshly published malicious package or compromised release has no advisory yet. It does not look for malware or typosquats. Pair it with lockfiles, script controls, cooldowns, and scanners that analyze package behavior.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark