Lockfile
| Type | File generated by a package manager |
|---|---|
| Examples | package-lock.json, pnpm-lock.yaml, yarn.lock, Cargo.lock |
| Records | Exact version, download address and content hash of every installed package |
| OWASP Top 10 | A03:2025 |
| Related | Software composition analysis, SBOM |
By default npm saves a dependency in package.json with a version range rather than one exact version.1 The entry "my_dep": "^1.0.4" accepts any later 1.x release, because the caret allows minor and patch updates.2 Each of those packages declares ranges of its own, so the same project installed twice, a month apart, can download different code. A lockfile is the file in which the package manager writes down the tree it did install: the exact version of every direct and transitive dependency, the address it came from, and a hash of its contents. The next install reads it and rebuilds the same tree.3
npm writes package-lock.json, pnpm writes pnpm-lock.yaml, Yarn writes yarn.lock and Rust's Cargo writes Cargo.lock. In Python, pip reaches the same result with a requirements.txt in which every line is pinned and hashed.4 The file is meant to be committed to the repository, so that teammates, deployments and continuous integration install the same dependencies and the history shows each change to the tree.3
What an entry holds
Since npm 7, package-lock.json has a packages object keyed by each package's folder path relative to the project, with an empty key for the project itself.3 One entry looks like this (the hash is shortened):
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-<hash of the archive>"
}version is the release that was installed. resolved is where it came from: a tarball URL for a registry package, or the full git URL with a commit SHA for a git dependency. integrity is a sha512 or sha1 hash of the archive that was unpacked, written in the format also used for Subresource Integrity. An entry may also carry hasInstallScript, which marks a package that runs a preinstall, install or postinstall script.3 The top-level lockfileVersion shows which npm wrote the file: 1 for npm 5 and 6, 2 for npm 7 and 8, 3 for npm 9 and later.3
How installs read it
Run with no arguments, npm install compares package.json with the lockfile. While every locked version still falls inside its declared range, npm installs the locked versions. When a range changes so that the locked version no longer fits, for example from ^7.0.0 to ^8.0.0, npm resolves a new version and rewrites the lockfile.1 package.json therefore controls which versions are acceptable, and the lockfile controls which acceptable version is installed.
npm ci is the strict form, made for test servers, CI and deployment. It requires a lockfile, deletes any existing node_modules first, never writes to package.json or the lockfile, and stops with an error when the two disagree instead of repairing the lockfile.5 pnpm behaves this way on a CI server by default: the install fails when a lockfile is present but out of date, unless --no-frozen-lockfile is passed.6
pip performs no check against tampering by default. Its hash-checking mode requires every requirement to be pinned with == and to carry a --hash, and every dependency to be listed; one hashed line turns the mode on for the whole file, and --require-hashes forces it. sha256 is the recommended algorithm; md5, sha1 and sha224 are excluded as too weak.4
# requirements.txt
FooProject==1.2 \
--hash=sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
# install
pip install --require-hashes -r requirements.txtAgainst harmful releases
Because a locked version that still fits its range wins over a fresh resolution, a version published after the lockfile was written stays out of the project until someone updates the lockfile.1 That delay matters when an attacker publishes a harmful version of a trusted package, the pattern of a software supply chain compromise. The Shai-Hulud worm, disclosed on 15 September 2025, affected more than 500 npm packages and spread through a postinstall script and stolen publishing credentials.7 Among the steps advised to npm users afterwards was to lock dependency versions with package-lock.json, or to produce the lockfile with npm i --package-lock-only, which resolves versions without running install scripts so the result can be audited before anything executes.7 That flag updates only the lockfile and downloads nothing into node_modules.1
A package that was already harmful when it was locked is recorded as faithfully as a clean one. Checking the locked versions for known flaws and known malware is covered in Vulnerable dependencies, Malicious packages and Software composition analysis.
Lockfile injection
An install takes both versions and download addresses from the lockfile, so a changed resolved URL installs whatever that URL serves.8 In September 2019 Liran Tal of Snyk showed this with a pull request to an open-source project. Among ordinary version changes, he pointed the yarn.lock entry for ms@2.1.1 at a tarball in his own GitHub repository, whose copy ran a postinstall script. He observed that lockfile diffs of 500 lines are routine, that thousands of changed lines occur, and that GitHub folds large diffs by default; a typosquatted host such as registry.npmgs.org would be harder to notice than a GitHub address,8 the same trick that Look-alike domains rely on.
The defences he proposed are to read lockfile changes, to accept them only from core maintainers rather than occasional contributors, and to lint the file in CI with his tool lockfile-lint, which checks that every package resolves over HTTPS from a trusted host.8 Its --allowed-hosts option takes host names or the shorthands npm, yarn and verdaccio for those registries:9
npx lockfile-lint --path package-lock.json --type npm --allowed-hosts npm --validate-httpsApplications and libraries
npm uses package-lock.json only at the root of a project. The file cannot be published, and one found inside an installed dependency is ignored. npm-shrinkwrap.json has the same format but ships with a package and fixes the tree for everyone who installs it; npm advises it only for command-line tools and similar deployed packages, and it takes precedence when both files exist.3
Cargo states the same boundary for Rust. Cargo.lock gives a project's own builds identical dependencies at different times and on different machines, but people who depend on the package resolve from its Cargo.toml, and the lockfile has no effect on their builds.10 cargo new adds Cargo.lock to version control by default.10
Prompt for a coding agent
Check that this project's lockfile (package-lock.json, pnpm-lock.yaml, yarn.lock, Cargo.lock or a hashed requirements.txt) is committed to git and not listed in .gitignore. Change every CI and deployment install step to a form that refuses to update the lockfile: npm ci for npm, pnpm install --frozen-lockfile for pnpm. List every lockfile entry whose resolved URL does not point at the official registry, and every package marked hasInstallScript. Do not upgrade any package. Report each file you changed.
See also
- Vulnerable dependencies
- Software composition analysis
- Software bill of materials
- Dependabot
- Package install scripts
- Dependency confusion and typosquatting
- Responding to a malicious package installation
- Category: Software supply chain
References
- a b c d npm. "npm-install". npm Docs, CLI version 11.
- back npm. "About semantic versioning". npm Docs.
- a b c d e f npm. "package-lock.json". npm Docs, CLI version 11.
- a b Python Packaging Authority. "Secure installs". pip documentation.
- back npm. "npm-ci". npm Docs, CLI version 11.
- back pnpm. "pnpm install". pnpm documentation.
- a b Cullen, Christopher (29 September 2025, revised 23 October 2025). Vulnerability note VU#534320 on the npm supply chain compromise and credential theft. CERT/CC Vulnerability Notes. Carnegie Mellon University.
- a b c Tal, Liran (24 September 2019). "Why npm lockfiles can be a security blindspot for injecting malicious modules". Snyk blog.
- back Tal, Liran. "lockfile-lint" README. GitHub.
- a b The Rust Project. "Frequently Asked Questions". The Cargo Book.