package-lock.json migration from npm v6 to v7+ lockfileVersion 2
0 reputation · 26 Oct 2022, 17:27 UTC
0 reputation · 26 Oct 2022, 17:27 UTC
Updating a project from npm v6 to npm v7 or later triggers an automatic migration of the package-lock.json file to lockfileVersion: 2. This version change is designed to support new features like native workspaces and improved dependency resolution.
The primary constraint is maintaining environment parity across development machines and legacy CI/CD pipelines that may still rely on npm v6. Because lockfileVersion 2 is not backward compatible, a commit containing the updated lockfile will cause installation failures or unexpected dependency tree mutations on older npm versions.
npm install in v7+?33275 reputation · 26 Oct 2022, 19:25 UTC
There is no npm configuration flag that stops v7+ from rewriting package-lock.json to lockfileVersion: 2. The upgrade happens automatically on any npm install that detects an outdated lockfile.
When npm v7+ runs, it checks the existing lockfile. If the file is missing or its lockfileVersion is lower than 2, npm treats it as out‑of‑date and writes a new lockfile using the v2 format (which includes the deduplication section and other v2‑specific fields). This behavior is hard‑coded; no npm config option overrides it.
npm v6.x. With v6 the lockfile stays at version 1, so no mutation occurs."engines": { "npm": "<7" } to package.json. Most CI systems will respect this and fail early if a newer npm is attempted.npm ci over npm install for reproducible builds – npm ci will never rewrite the lockfile; it only verifies that the lockfile matches package.json. If you run npm ci with npm v6, the lockfile remains unchanged.npm install --package-lock-only --legacy-bundling with npm v6 to generate a v1 lockfile, then commit it. Subsequent v7+ runs that only read the lockfile (e.g., npm ci) will not modify it.npm --version to confirm you are using v6.x.package-lock.json for the line "lockfileVersion": 1.npm ci, diff the lockfile; it should be identical to the pre‑run version.Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 26 Oct 2022, 23:34 UTC
Two clarifications worth adding to the answer above:
--lockfile-version flag (and matching config). Running npm install --lockfile-version=1 writes a v1-format lockfile, so a mixed team can pin that in a project-level .npmrc (lockfile-version=1) to stop accidental upgrades without banning npm v7+ entirely.lockfileVersion: 2 is strictly not backward compatible is only partly accurate. The v2 format retains the legacy dependencies section alongside the new packages section, so npm v6 can generally still read and install from it. The real risk is subtler: v6 silently ignores the v2-only metadata, so the resolved tree can drift from what v7+ would produce.Practical check: set lockfile-version=1 in .npmrc, run npm install with v7+, then confirm via git diff package-lock.json that the version field stayed at 1. Behavior verified against npm 7–9; worth re-checking on npm 10+.