Zlib error when attempting to run npm install or yarn
Asked Answered
S

6

8

I have just made some pulls from my library's from GitHub, I was using my windows computer to do the coding in VSCode. The code has no problem, although when I attempt to run npm install or yarn install to get the node_modules and the yarn.lock I get a weird error and the packages don't work. I'm using ZSH as the terminal for my Mac.

This is the error output:

niltonsf@Niltons-MacBook-Pro ignews.nosync % sudo yarn install
Password:
yarn install v1.22.15
[1/4] πŸ”  Resolving packages...
[2/4] 🚚  Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/acorn/-/acorn-7.4.1.tgz: incorrect data check".
info If you think this is a bug, please open a bug report with the information provided in "/Users/niltonsf/Desktop/ignews.nosync/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
error https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-11.1.2.tgz: incorrect data check
error https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-11.1.2.tgz: incorrect data check
niltonsf@Niltons-MacBook-Pro ignews.nosync % npm install
npm WARN deprecated @types/[email protected]: This is a stub types definition. next-auth provides its own type definitions, so you do not need this installed.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm ERR! code Z_DATA_ERROR
npm ERR! errno -3
npm ERR! zlib: incorrect data check

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/niltonsf/.npm/_logs/2021-10-05T15_44_51_340Z-debug.log
niltonsf@Niltons-MacBook-Pro ignews.nosync % 

My versions from node, npm and yarn:

node: v14.18.0
npm: 6.14.15
yarn: 1.22.15
macos: Big Sur

What I have attempted:

deleting the yarn.lock
running: npm cache verify and then npm cache clean --force

This is the end of the log result:

1165 verbose stack     at PassThrough.Writable.write (internal/streams/writable.js:303:10)
1165 verbose stack     at PassThrough.ondata (internal/streams/readable.js:731:22)
1165 verbose stack     at PassThrough.emit (events.js:400:28)
1166 verbose cwd /Users/niltonsf/Desktop/github.nosync/ignews
1167 verbose Darwin 20.6.0
1168 verbose argv "/usr/local/Cellar/node@14/14.18.0/bin/node" "/usr/local/opt/node@14/bin/npm" "install"
1169 verbose node v14.18.0
1170 verbose npm  v6.14.15
1171 error code Z_DATA_ERROR
1172 error errno -3
1173 error zlib: incorrect data check
1174 verbose exit [ -3, true ]

If I run the npm i or yarn in my windows computer I dont get any error

Studbook answered 5/10, 2021 at 14:40 Comment(3)
Please do not post images of code, error messages or data when asking a question – Serum
All I can contribute is that Z_DATA_ERROR means that either the compressed data is corrupted, or it is not the expected compressed data at all. – Admonition
@MarkAdler I have freshly installed the macOS and installed node and npm using brew. And why would it conflict with my node, npm and yarn – Studbook
M
10

After facing similar issues on some of our workstations, I would say that it definitely looks like a bug in Node 14.18.x on m1 Macs and/or Big Sur (even though node itself is compiled for x86_64). I suspect a linking issue with the zlib library, but this is a discussion for Node's issue tracker...

So here's my suggestion: uninstall Node 14.18.x and try using Node 14.17.x instead.

As a side note, you may find it useful to first install nvm. nvm allows to quickly install several versions of Node, and switch from one to the other. For example, you might do:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

nvm install 14.17
nvm alias default 14.17

npm install -g yarn
yarn
Mccallum answered 27/10, 2021 at 17:48 Comment(1)
I tried at least 2 node v14.17.x version with no luck. Posted my workaround here: https://mcmap.net/q/1240635/-zlib-error-when-attempting-to-run-npm-install-or-yarn – Burgeon
A
9

Switching to Node 14.17, 14.16 may not do the trick for you, since the new Apple M1 ARM-based CPU architecture has not been in the support path, until Node v16. Here is the appropriate solution for M1/Apple Silicon/ARM processors coming from an older version of NodeJS w/ NVM already installed on the workstation.

// Clear your current local NVM cache.
nvm cache clear

// Download and install NodeJS version 16, later verify that the v16
// NodeJS API does not break your applications build/runtime. 
nvm install v16 && nvm use v16 && node --version

// While at your projects root (same level as package.json, node_modules, typically)
// remove all traces of ./node_modules from last version's NPM & Node cache and node_modules directory.
rm -rf ./node_modules && npm cache clean --force

// All prior dependencies are now removed, reinstall with new NodeJS/NPM versions.
npm i

// Start your app
npm start 

Any global modules installed with NPM are going to get blown away when you switch into a new NodeJS version with NVM, as always. Therefore, make sure your project is not missing any required global/system NodeJS dependencies, installed at the machine level, which may or may not be included in your project's package.json file as a dependency or dev-dependency (@angular-cli, create-react-app, typescript, ts-node, express-generator, AWS-CLI, etc.)

If you get runtime error's due to out-of-support dependencies, deprecated API changes from the migration from v14->v16 (the bigger your NodeJS project, the more likely of a breaking change relating to your codebase). If it runs seemingly well with NodeJS v16 after a local smoke-test, and this is not your personal project then it would be prudent to talk to your lead, manager, or architect about making a pull request and having a full regression done after the changes are in place, as this will have a potential to cause regression's for accessing deprecated or incorrect signatures to call's to out-dated APIs.

Warning: If you get an error about NodeJS/NPM not being found, after downloading and switching versions be sure to reload your shell env, by exiting and opening manually or programmatically, depending on your local setup:

    // Bash with ~/.bash_profile for interactive shell
    source ~/.bash_profile

    // Bash reload from .bashrc non-interactive shell
    source ~/.bashrc

    // ZSH reload from .zshrc non-interactive shell
    source ~/.zshrc 

    // ZSH reload from ~/.zsh_profile for interactive shell
    source ~/.zsh_profile

    // Or just simply type the following into your shell, depending on which you are using.
    %> zsh
    #> sh 
    #> bash
 

**In summary: Currently, the recommended approach is to install and use the recommended version of NodeJS (v16), which is the officially supported version for the Apple M1 ARM CPU.

This way everything can be tested, and you and any other developers who maintain the codebase can take full advantage of all the new features and fixes in v16 and prevent burning development hours by forcing them to run a hacky setup, trying to a hacky workaround. This way everything can be tested thoroughly.

Note: Eventually, someone will need to upgrade the NodeJS runtime version in order to stay in your support path and use modern dependencies, security/performance fixes, and leverage modern APIs, and target the latest supported JavaScript versions. If you are working with a team of developers who are working on different types of workstations, then this problem will be eating up development time while the research (and come to this post or figure out their own workaround, either may be dangerous and lead to uncommon, hard to trace/debug errors and an inconsistent development environment, instead of just fixing the build/runtime/dependency upgrade/replacements needed if issues occur when migrating to v16).

Albarran answered 15/11, 2021 at 21:6 Comment(2)
Using 16 worked like magic. Thanks – Sawicki
Thanks so much, this worked! Specifically on v16.13.1, not on older v16 versions in our case, likely due to incompatible package requirements. Nonetheless, this reply was the sure path to a working build. – Lossa
D
7
npm ERR! code Z_DATA_ERROR
npm ERR! errno -3
npm ERR! zlib: incorrect data check

If your dev environment absolutely requires a lower version of Node

  • Apple M1 ARM-based and Node v12 was a must

For me, the issue started with nvm, so I had to start there but I managed to fix it using this NVM troubleshooting guide and ultimately was able to npm install my dependencies. Do this in a shell:

  1. Verify you are in arm64 with $ arch
  2. Install Rosetta $ softwareupdate --install-rosetta
  3. Change architecture $ arch -x86_64 zsh (change zsh to whatever shell you are using)
  4. Source nvm $ source "${NVM_DIR}/nvm.sh"
  5. Install whatever older version of node you need $ nvm install v12.22.1 --shared-zlib

"Note: You're probably curious why --shared-zlib is included. There's a bug in recent versions of Apple's system clang compiler. If one of these broken versions is installed on your system, the above step will likely still succeed even if you didn't include the --shared-zlib flag. However, later, when you attempt to npm install something using your old version of node.js, you will see incorrect data check errors. If you want to avoid the possible hassle of dealing with this, include that flag. For more details, see this issue and this comment" 6. Exit back to your native shell

$ exit
$ arch
arm64
  1. Verify your node architecture is correct.
$ node -p process.arch
x64
  1. I was able to npm install after removing:
  • /node_modules
  • package-lock.json files.
Duppy answered 18/3, 2022 at 14:8 Comment(4)
github.com/nvm-sh/nvm#macos-troubleshooting Note: You're probably curious why --shared-zlib is included. There's a bug in recent versions of Apple's system clang compiler. If one of these broken versions is installed on your system, the above step will likely still succeed even if you didn't include the --shared-zlib flag. However, later, when you attempt to npm install something using your old version of node.js, you will see incorrect data check errors. If you want to avoid the possible hassle of dealing with this, include that flag. For more details, check the link above – Pettus
github.com/nodejs/node/issues/39313#issuecomment-902395576 – Pettus
arch will output i386 also, which means compatible with intel. i386 means you are running compatible version of terminal which emulates intel. – Pettus
I had this issue with clang-14, nvm, node 16.15.1, npm 8.11.0, macOS 13.3.1 (22E261) – Pettus
V
4

My project is not running on new Node.js, but also I had the same issue with installing deps on Node 14 on my M1 laptop. Switching to Node 14.17, 14.16 doesn't help.

Here is my workaround for m1 users:

nvm use 16
npm i --force
nvm use 14
npm run start // <- your app start command
Vial answered 15/11, 2021 at 10:10 Comment(0)
B
2

I started to get the same errors using any node v14 on a M1 mac

npm ERR! code Z_DATA_ERROR
npm ERR! errno -3
npm ERR! zlib: incorrect data check

Tried a dosen things and this is what worked for me:

Install node v16 which has native support for M1 macs
And that might be the only thing you need to do...

But I need to work on some projects, where everybody is using node v14 and npm v6
What worked for me was to use node 16 with a downgraded npm:

  1. Install a separate node version for this: nvm install v16.13.1
  2. Switch to it and downgrade npm: npm i -g [email protected]
  3. Optionally alias it: nvm alias compat v16.13.1

In the end you should have something like:

nvm use compat 
Now using node v16.13.1 (npm v6.14.16)

For me the only problem with v16 was it changed the package-lock.json version on older projects.


What I've tried and doesn't work

Other node v14 versions with no luck (same error)

  • v14.17.4
  • v14.17.6
  • v14.18.2

Tried to upgrade npm on v14

Trying to install [email protected] itelsef caused the zlib error

npm doctor

says everything is fine

npm cache clean -f

same error after

Go to a new folder and start a new project.

Installing some packages worked, but trying one for which I've got a zlib error on my other project caused the same error in the new project

Deleted node_modules

Similar to above. Some packages manage to install but not all (zlib)


Interesting fact:
Installing any node version bellow v16 on a M1 mac is getting compiled locally and adds about 10GB to nvm cache (https://github.com/nvm-sh/nvm/issues/2418)

This cache would not be used again unless you plan on removing and installing the (exact) same node version

You can use nvm cache clear to take the space back

nvm cache after installing node v14.17.4

nvm cache after installing node v14.17.4

Burgeon answered 20/1, 2022 at 20:58 Comment(1)
Thanks, this also solved the same problem for me on an Apple M2 chip – Taxpayer
N
0

For those using macbook with m1 chip I was having this issue and tried installing the following node versions with nvm but the error persisted:

  • v14.5.5
  • v14.16.0
  • v14.16.1
  • 14.17.6

I tried cleaning cache and all the solutions mentioned here, I even tried different combinations of node and npm but no luck.

To fix the problem I had to run the command to switch architecture: arch -x86_64 zsh

After that I was able to npm install with no issues.

Needleful answered 11/2, 2022 at 1:7 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.