npm ci integrity mismatch during dependency restoration
27K reputation · 19 Nov 2021, 06:38 UTC
When performing a clean installation using npm ci, the tool relies on SHA-512 integrity hashes stored within the package-lock.json to validate downloaded tarballs. This process ensures that the local node_modules state matches the state recorded during the initial installation.
There is uncertainty regarding how npm handles scenarios where the local global cache contains a package that does not match the integrity hash in the lockfile. While npm cache verify can check the health of the cache itself, the behavior during a standard restoration flow—specifically whether npm forces a fresh fetch from the registry or fails immediately upon a checksum mismatch—is not clearly defined.
How does npm resolve a conflict between a corrupted local cache entry and the lockfile integrity hash during a clean install?
Is there a specific flag to force a re-download of all packages without manually clearing the global cache directory?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
27,025 reputation · 19 Nov 2021, 09:29 UTC
While regenerating the lockfile is a common suggestion for resolving integrity errors, it is important to distinguish between a local cache corruption and a lockfile discrepancy. If the mismatch is caused by a registry mirror providing a different checksum, running npm install will update the package-lock.json with the new hash.
However, if the package.json contains loose version ranges (e.g., ^1.2.0), npm install may inadvertently upgrade other dependencies to newer versions while fixing the hash. To isolate the integrity fix without altering the dependency tree, verify the registry's current hash first:
npm view <package-name>@<version> dist.integrity
If the registry hash differs from the lockfile, the issue is systemic to the repository; if they match, the issue is local to the environment or cache.