A maintainer account gets hijacked, a compromised version appears overnight, and your CI job installs it the next morning without anyone noticing. A few lines in .npmrc shrink that gap considerably. This tutorial shows how to secure npm dependencies: enforce the lockfile, approve install scripts instead of letting them run, delay new releases, block git dependencies and check where packages come from. I ran every command with Node.js 24.22.1 and npm 11.21.1. The outputs are real, and the version numbers are from December 2, 2026.
Where attacks on npm projects start
The npm supply chain runs on open source and on trust. A typical project has a few dozen direct dependencies and several hundred indirect ones (transitive dependencies), and an attacker needs only one of them, often a transitive dependency you never chose. Supply chain attacks on an npm package usually look like this: someone publishes a tampered version of an existing package, registers a similar package name, or hooks into a package that is pulled from a git repository. Malicious packages of this kind range from typosquats to dependency confusion attacks, where a public package takes the name of an internal one. The malicious code often runs at install time, because npm executes scripts such as postinstall before you have started a single line of your own code. Developer machines and CI pipelines hold credentials such as tokens and keys, which is exactly what this kind of malware would go after first, including npm tokens used for package publishing. We cover recent cases in our reports on npm dependency confusion and the cross-spawn vulnerability. A scanner that looks up known vulnerabilities cannot help with some of these, because the compromised version is in no database yet when the attack starts. These are the security risks that npm security best practices have to cover, and the steps below are the cheapest ones in software supply chain security. For such packages the only defence is to narrow the path: fewer automatic updates, less third-party code executed, provenance you can verify.
Secure npm dependencies, step 1: enforce the lockfile with npm ci
The first step costs nothing. npm install may change the versions in package-lock.json; npm ci may not. It installs exactly what the lockfile says and aborts when package.json and the lockfile disagree. In a test project with express I added a second dependency to package.json only and then ran npm ci:
npm ci
# npm error code EUSAGE
# npm error `npm ci` can only install packages when your package.json and
# npm error package-lock.json or npm-shrinkwrap.json are in sync. ...
# npm error Missing: lodash@4.18.1 from lock file
That is what you want in CI and at deployment: a hard failure instead of a silent new version. Locally, npm install stays the right tool, but changed package lockfiles belong in code review, for example in the pull request for your GitHub repositories. Setting save-exact=true in .npmrc makes npm write exact versions to package.json instead of ranges like ^5.3.0. That does not protect you from malicious versions you install yourself on purpose or by mistake, but it keeps a new patch release from sliding into the range unnoticed.
Control install scripts: ignore-scripts and allowScripts
When you install a package, it may run code of its own. I built a tiny local package whose postinstall only writes a file to my home directory. With the default settings the script ran, and npm printed nothing more than a warning:
npm install ./evil-demo-1.0.0.tgz
# npm warn install-scripts 1 package has install scripts not yet covered by allowScripts:
# npm warn install-scripts evil-demo@1.0.0 (postinstall: node -e "...")
The file was there afterwards. With ignore-scripts=true in .npmrc nothing ran. That is the blunt option, and it has a price: packages that really need a script, for example to fetch a binary, then stop working without extra steps. The approval list that ships with npm 11.21 is finer. You install without scripts, let npm list the candidates, and approve them one by one. My example is esbuild, which has a postinstall:
npm install esbuild --ignore-scripts
npm install-scripts ls
# 1 package has install scripts not yet covered by allowScripts:
# esbuild@0.28.2 (postinstall: node install.js)
npm install-scripts approve esbuild
# Approved esbuild:
# added esbuild@0.28.2
The approval ends up in the allowScripts field of package.json, pinned to the version you reviewed by default: "allowScripts": { "esbuild@0.28.2": true }. npm install-scripts deny esbuild writes a false instead, which survives a later blanket approval. The key switch in the npm CLI is strict-allow-scripts=true. Without it npm only warns. With it package installation fails as soon as a package has scripts that are not listed in allowScripts. My test with the local package ended in ESTRICTALLOWSCRIPTS and no marker file.
A caveat about the documentation: the manual page for npm install-scripts says scripts are blocked by default, while the page for approve-scripts still calls the field advisory. In my test with 11.21.1, scripts without an entry did run as long as strict-allow-scripts was not set. Trust your own test, not either sentence.
Delay new releases with min-release-age
The idea behind a waiting period: when malicious versions are discovered, they are usually pulled within a few days. If you accept new package versions only after a delay, you let others go first. That is no guarantee. The option is called min-release-age and counts in days. I tested it with vite, whose version 8.4.1 appeared this morning (December 2, 03:26 UTC):
npm install vite --package-lock-only # vite 8.4.1
npm install vite --package-lock-only --min-release-age=1 # vite 8.4.0
npm install vite --package-lock-only --min-release-age=7 # vite 8.3.8
npm install vite --package-lock-only --min-release-age=30 # vite 8.3.5
The results come from the generated package-lock.json. A value so high that no matching version exists ends with ENOVERSIONS. The counterpart with a fixed date is --before. Two side effects are worth knowing. First, the window also blocks security updates: according to the documentation, npm audit fix leaves the vulnerable version in place when the fix is too recent, and warns about it. Second, you can exempt your own packages with min-release-age-exclude[]=@yourcompany/*. One week is a compromise I would use as a starting value. Whether seven days is enough depends on how quickly findings spread in your environment, and I cannot measure that.
Block git dependencies with allow-git
Dependencies that point straight at a git repository bypass the public npm registry and its signatures. With allow-git=none npm refuses them. When I tried to install github:sindresorhus/is-plain-obj, npm stopped at once with EALLOWGIT: Fetching packages of type "git" have been disabled. The level root allows only git dependencies that you list in your own package.json, not those buried deep in the tree. That is usually the more practical value if you deliberately use a git package.
Check provenance: npm audit signatures
The command npm audit signatures checks that the installed packages carry the registry signature and verifies provenance attestations where authors published them. In my express test project it printed:
npm audit signatures
# 65 packages have verified registry signatures
# 6 packages have verified attestations
That only tells you the packages came from the registry unchanged, and that six of them have a provenance attestation. It says nothing about whether the code is good. For security vulnerabilities there is still npm audit, which runs a security audit of your tree against known vulnerabilities and lists vulnerable dependencies. With audit-level you decide at which severity the command returns an error code. I installed lodash@4.17.15 for this. Then npm audit --audit-level=critical ended with exit code 0 and --audit-level=high with 1, because the report listed several findings of severity high. As a gate in CI that works if you choose the level deliberately.
Look before you update: age, provenance and maintainers of a version
Before you accept a suspicious new version, take a look in the registry. npm view reads the metadata without installing anything. Get the publish time with npm view vite time --json; for 8.4.1 it showed December 2, 2026, 03:26 UTC. Whether a version has a provenance attestation is shown by npm view vite@8.4.1 dist.attestations. For me it returned an address under registry.npmjs.org/-/npm/v1/attestations/ and the type https://slsa.dev/provenance/v1. npm view vite@8.4.1 maintainers --json lists who looks after the package: three accounts in my test, one of them a bot. If an unknown name suddenly shows up, or an attestation that earlier versions had is missing, that is a reason to ask questions. It is not proof of an attack. In code review, every change to package-lock.json belongs in the pull request on GitHub or GitLab, so developers see unusual jumps before they reach the workflow.
Putting it together: one .npmrc for new projects
This is the configuration I used for a final test in a fresh project:
save-exact=true
min-release-age=7
allow-git=none
strict-allow-scripts=true
audit-level=high
The workflow: npm install express esbuild --ignore-scripts, then npm install-scripts ls, npm install-scripts approve esbuild, and finally npm ci. In the test npm ci ran without errors, npm audit signatures reported 67 verified signatures and 8 attestations, and npm audit found nothing. Thanks to save-exact, package.json held exact versions (express 5.3.0, esbuild 0.28.2), and the waiting window had already influenced which versions npm picked.
Three lines are enough for CI: npm ci, npm audit signatures and npm audit. I ran these commands locally, not on a CI platform such as GitHub Actions. If you publish your own packages, also set up trusted publishing so that releases come out of CI without long-lived access tokens. The npm guide describes the steps. I did not test that.
Frequently asked questions
Can npm install run malicious code? Whenever you install a package, yes: through the install scripts of packages. In my test, the script of a local package ran with the default settings. ignore-scripts, or strict-allow-scripts together with allowScripts, stops that. Code you start yourself afterwards still runs, of course.
Is npm safe to use? It depends on how you configure it. The defaults favour convenience. The options above narrow the path for attackers considerably, but they replace neither reviews nor scanners.
Does this apply to other package managers such as pnpm and Yarn? I did not test those tools here. The concepts (lockfile, waiting period, approving scripts) exist there too, in part under other names.
Does it work with older npm versions? I tested min-release-age, allow-git and allowScripts with npm 11.21.1. Older versions may not know them. npm help config shows whether an option exists.
Where to go from here
A lockfile tells you which packages you have. A software bill of materials makes that readable for scanners, as our tutorial Create an SBOM with Syft and Grype shows. Python projects follow similar rules, which we see in a concrete case in the report on the LiteLLM and PyPI backdoor. All options in detail are in the npm configuration reference, the approval commands are in the npm install-scripts documentation, and npm audit signatures is described on the npm audit page.




