Fix: Integrity check failed for "any-package-name" (yarn)

Here's the fix for the error - Integrity check failed for <package-name> (computed integrity doesn't match our records, got <integrity-hash-value>

Profile Picture

Sivanesh Shanmugam

3 min read

Fix: Integrity check failed for "any-package-name" (yarn)
Terminal that shows error Integrity check failed for "yargs-parser" package

If you face any issue something like this in your local terminal or in any of your task runners like Github Actions or Travis, You can solve them by using any one of the following possible solutions.

This is one of the common issues occur in yarn package manager.

Integrity checksums are a security feature used by Yarn to ensure that the contents of a package have not been tampered with since they were published to the registry. So, when you install a package, Yarn downloads the package's tarball from the registry and verifies that the integrity checksum matches the one listed in the yarn.lock file. - Source

Solutions

There's no single answer for this. Because there are different scenarios for different code base in which this error occurs. So I tried my best to list all possible answers that I learned fixing this issue.

🎯
If your project is a new/small project you can directly use Solution 4 at the bottom. If it doesn't work then start from solution 1.

Solution 1: Clearing cache

yarn cache clean

In some blogs they say this as one of the solution, but it never worked for me. You can give it a try. No hard in trying πŸ˜ƒ

Solution 2: Update Checksums

  • Use yarn --update-checksums or yarn install --update-checksums
  • If there’s a change in the yarn.lock file in that package-name's keys such as integrity and resolved then the solution is resolved. (Where package-name refers to the package name where it threw error Integrity check failed for package-name)

If the current solution is not working then

Solution 3: Reinstall that particular package

If that particular package is part of the package.json's dependencies (Or devDependencies), then

yarn remove <package-name>
yarn add <package-name> --update-checksums

Use --save-dev at the last if it's a dev dependency.

If the particular package is part of your dependencies in package.json and still it is not fixed then you can directly go to solution 4.

If that particular package is not part of the package.json's dependencies (Or devDependencies), then it is a child dependency of any of the package that you used. So find the parent package for that and reinstall using the same commands mentioned above.

To find the parent package

  1. Open yarn.lock file.
  2. Search for the package name.
  3. Find an occurrence where it will be under dependencies of another package.

This is how the structure of yarn.lock looks like for a package

<parent-package-name>@<version>":
  version "<version>"
  resolved "<link>"
  integrity <integrity-hash>
  dependencies:
    "<package-name-1>" "<version>"
    "<package-name-2>" "<version>"
    "<particular-package>" "<version>"
    
Format on how a package and it's dependencies look like
next@12.3.0:
  version "12.3.0"
  resolved "https://nexus.hackerrank.com/repository/hr-npm-group/next/-/next-12.3.0.tgz#0e4c1ed0092544c7e8f4c998ca57cf6529e286cb"
  integrity sha512-GpzI6me9V1+XYtfK0Ae9WD0mKqHyzQlGq1xH1rzNIYMASo4Tkl4rTe9jSqtBpXFhOS33KohXs9ZY38Akkhdciw==
  dependencies:
    "@next/env" "12.3.0"
    "@swc/helpers" "0.4.11"
    caniuse-lite "^1.0.30001332"
    postcss "8.4.14"
    styled-jsx "5.0.6"
    use-sync-external-store "1.2.0"
An example of how "next" package will look like. If the error is from @swc/helpers then reinstall next@12.3.0
  1. If that package name is part of your dependencies in package.json then run the scripts mentioned above (Under Solution 3) else recursively run the step 3 until you reach a package name where it is available as a dependency in package.json

If the current solution is not working then

Solution 4: Remove yarn.lock and do yarn install

This is the last and the brutal solution for this case, where in most case, it just works.

  1. Remove yarn.lock
  2. Do yarn install

Mostly it should work. Else you can remove both yarn.lock and node_modules folder and try again.

This should do.

If there are any more edge cases with any different solutions please do add them as comments or let me know. Thanks πŸ˜ƒ

MailLinkedInGitHubTwitter