尝试运行npm install或yarn时出现Zlib错误

Nil*_*r F 7 zlib node.js npm yarn-lock.json

我刚刚从 GitHub 上的库中提取了一些内容,我正在使用 Windows 计算机在 VSCode 中进行编码。代码没有问题,尽管当我尝试运行 npm install 或yarn install 来获取node_modules 和yarn.lock 时,我收到一个奇怪的错误并且包不起作用。我使用 ZSH 作为 Mac 的终端。

这是错误输出:

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/next-auth@3.15.0: This is a stub types definition. next-auth provides its own type definitions, so you do not need this installed.
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: 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 % 
Run Code Online (Sandbox Code Playgroud)

我的node、npm 和yarn 版本:

node: v14.18.0
npm: 6.14.15
yarn: 1.22.15
macos: Big Sur
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

deleting the yarn.lock
running: npm cache verify and then npm cache clean --force
Run Code Online (Sandbox Code Playgroud)

这是日志结果的结尾:

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 ]
Run Code Online (Sandbox Code Playgroud)

如果我在 Windows 计算机中运行 npm i 或 YARN,我不会收到任何错误

jwa*_*ins 9

在我们的一些工作站上遇到类似问题后,我想说它绝对看起来像 m1 Mac 和/或 Big Sur 上的 Node 14.18.x 中的错误(即使node它本身是为 编译的x86_64)。我怀疑 zlib 库存在链接问题,但这是针对 Node 问题跟踪器的讨论......

所以我的建议是:卸载 Node 14.18.x 并尝试使用 Node 14.17.x。

附带说明一下,您可能会发现首先安装nvm很有用。nvm允许快速安装多个版本的 Node,并从一个版本切换到另一个版本。例如,您可能会这样做:

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
Run Code Online (Sandbox Code Playgroud)


Alp*_*33k 9

切换到 Node 14.17、14.16 可能对您不起作用,因为新的基于 Apple M1 ARM 的 CPU 架构直到 Node v16 才出现在支持路径中。以下是适用于 M1/Apple Silicon/ARM 处理器的适当解决方案,该解决方案来自旧版本的 NodeJS(带有已安装在工作站上的 NVM)。

// 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 
Run Code Online (Sandbox Code Playgroud)

与往常一样,当您使用 NVM 切换到新的 NodeJS 版本时,使用 NPM 安装的任何全局模块都会被破坏。因此,请确保您的项目没有缺少任何必需的全局/系统 NodeJS 依赖项(在计算机级别安装),这些依赖项可能会也可能不会作为依赖项或开发依赖项包含在项目的 package.json 文件中(@angular-cli, create-react-app、typescript、ts-node、express-generator、AWS-CLI 等)

如果您由于不受支持的依赖项而收到运行时错误,则从 v14->v16 迁移时已弃用的 API 更改(您的 NodeJS 项目越大,与代码库相关的重大更改的可能性就越大)。如果在本地冒烟测试后,它在 NodeJS v16 上运行得很好,并且这不是您的个人项目,那么明智的做法是与您的领导、经理或架构师讨论提出拉取请求并在项目完成后完成完整的回归。更改已经到位,因为这可能会导致访问已弃用或不正确的签名以调用过时的 API 的回归。

警告:如果您收到有关未找到 NodeJS/NPM 的错误,在下载并切换版本后,请务必通过手动或以编程方式退出并打开来重新加载您的 shell 环境,具体取决于您的本地设置:

    // 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
 
Run Code Online (Sandbox Code Playgroud)

**综上所述:目前推荐的做法是安装并使用推荐版本的 NodeJS (v16),这是 Apple M1 ARM CPU 官方支持的版本。

这样,所有内容都可以进行测试,您和维护代码库的任何其他开发人员都可以充分利用 v16 中的所有新功能和修复,并通过强制他们运行黑客设置、尝试黑客解决方法来防止浪费开发时间。这样一切都可以得到彻底的测试。

注意:最终,有人需要升级 NodeJS 运行时版本,以便留在您的支持路径中并使用现代依赖项、安全/性能修复、利用现代 API,并针对最新支持的 JavaScript 版本。如果您正在与一个在不同类型的工作站上工作的开发人员团队一起工作,那么这个问题将在研究时消耗开发时间(并且来到这篇文章或找出他们自己的解决方法,要么可能是危险的并导致不常见、难以跟踪/调试错误和不一致的开发环境,而不仅仅是修复迁移到 v16 时出现问题所需的构建/运行时/依赖项升级/替换)。

  • 使用 16 的效果就像魔术一样。谢谢 (2认同)

小智 6

npm ERR! code Z_DATA_ERROR
npm ERR! errno -3
npm ERR! zlib: incorrect data check
Run Code Online (Sandbox Code Playgroud)

如果您的开发环境绝对需要较低版本的 Node

  • 基于 ARM 的 Apple M1 和 Node v12 是必须的

对我来说,问题是从 nvm 开始的,所以我必须从那里开始,但我设法使用此 NVM 故障排除指南修复它,并最终能够 npm 安装我的依赖项。在 shell 中执行此操作:

  1. 验证您是否在 arm64 中$ arch
  2. 安装罗塞塔$ softwareupdate --install-rosetta
  3. 更改架构$ arch -x86_64 zsh(将 zsh 更改为您正在使用的任何 shell)
  4. 源nvm$ source "${NVM_DIR}/nvm.sh"
  5. 安装您需要的任何旧版本的节点$ nvm install v12.22.1 --shared-zlib

“注意:您可能好奇为什么包含 --shared-zlib。Apple 系统 clang 编译器的最新版本中存在一个错误。如果您的系统上安装了这些损坏版本之一,则上述步骤可能仍然会成功,即使你没有包含 --shared-zlib 标志。但是,稍后,当你尝试使用旧版本的 Node.js npm 安装某些东西时,你会看到不正确的数据检查错误。如果你想避免可能的麻烦处理这个问题,包括该标志。有关更多详细信息,请参阅此问题和此评论“ 6. 退出回本机 shell

$ exit
$ arch
arm64
Run Code Online (Sandbox Code Playgroud)
  1. 验证您的节点架构是否正确。
$ node -p process.arch
x64
Run Code Online (Sandbox Code Playgroud)
  1. npm install删除后我能够:
  • /节点模块
  • package-lock.json 文件。