Lockfiles and npm ci
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Read a resolved version
const lock = { lockfileVersion: 3, packages: { "": { dependencies: { "demo-parser": "^1.2.3" } }, "node_modules/demo-parser": { version: "1.5.0" } } };console.log(lock.packages[""].dependencies["demo-parser"]);console.log(lock.packages["node_modules/demo-parser"].version);Run in this fixture directory:
node main.mjsExpected stdout:
^1.2.31.5.0Expected exit status: 0. Expected stderr: empty. The root requirement and resolved version are different kinds of information. This is a simplified illustrative lock snapshot, not an installable lockfile.
How it works
npm install resolves dependencies and can update the lockfile. npm ci requires a lockfile (or shrinkwrap), fails if manifest requirements and lockfile disagree, removes an existing node_modules directory, and does not rewrite the manifest or lockfile. The same relevant npm flags must be used when the lock was created and when it is consumed. Integrity metadata helps detect unexpected downloaded content, but a locked dependency can still be vulnerable. For the offline examples, inspect fictional lock snapshots only. No packages are installed and npm ci itself is not executed. In an approved real repository review dependencies and lockfile, then run npm ci using the pinned toolchain and reviewed settings; lifecycle scripts and network access require separate approval.
Inspect a changed root declaration
const manifest = { dependencies: { "demo-parser": "^2.0.0" } };const previousLockRoot = { dependencies: { "demo-parser": "^1.2.3" } };console.log(manifest.dependencies["demo-parser"] === previousLockRoot.dependencies["demo-parser"] ? "unchanged" : "review lockfile");Run in this fixture directory:
node main.mjsExpected stdout:
review lockfileExpected exit status: 0. Expected stderr: empty. The comparison flags a changed declaration for review. It does not reproduce npm ci dependency validation.
Common mistake
Do not delete a committed lockfile just to make a CI failure disappear. Reconcile the intended change with the manifest, regenerate with the approved toolchain, review the diff, and commit both files.
Try it yourself
Given a declared range ^1.2.3 and a resolved 1.5.0, explain why the two values need not be identical. Describe the response to a changed manifest with an outdated lock.
Further reading: Official documentation 1. Related ByteMind reference candidate: node-npm-ci (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lockfiles and npm ci — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What happens if npm ci finds incompatible manifest and lock requirements?
What does a locked version prove about security?
Which directory can npm ci remove before installing?