首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试运行npm安装或纱线时的Zlib错误

尝试运行npm安装或纱线时的Zlib错误
EN

Stack Overflow用户
提问于 2021-10-05 14:40:40
回答 6查看 7.5K关注 0票数 4

我刚刚从我的GitHub库中提取了一些数据,我正在用我的windows计算机在VSCode中进行编码。代码没有问题,尽管当我尝试运行npm安装或纱线安装以获得node_modules和yarn.lock时,我会得到一个奇怪的错误,并且包不能工作。我正在用ZSH作为我的Mac的终端。

这是错误输出:

代码语言:javascript
运行
复制
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 % 

我来自节点、npm和纱线的版本:

代码语言:javascript
运行
复制
node: v14.18.0
npm: 6.14.15
yarn: 1.22.15
macos: Big Sur

我所尝试的是:

代码语言:javascript
运行
复制
deleting the yarn.lock
running: npm cache verify and then npm cache clean --force

这是日志结果的结尾:

代码语言:javascript
运行
复制
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 ]

如果我在windows计算机中运行npm i或纱线,则不会出现任何错误。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2021-10-27 17:48:30

在我们的一些工作站上遇到类似的问题之后,我想说,它看起来确实像m1 Mac和/或Big上Node14.18.x中的一个bug (尽管node本身是为x86_64编译的)。我怀疑zlib库存在链接问题,但这是Node问题跟踪器的讨论.

下面是我的建议:卸载Node14.18.x并尝试使用Node14.17.x。

另外,您可能会发现第一次安装nvm很有用。nvm允许快速安装多个Node版本,并从一个版本切换到另一个版本。例如,您可以:

代码语言:javascript
运行
复制
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
票数 8
EN

Stack Overflow用户

发布于 2021-11-15 21:06:21

切换到Node 14.17,14.16可能对你没有好处,因为新的基于苹果M1 ARM的CPU体系结构还没有进入支持路径,直到Node v16。以下是M1/Apple硅/ARM处理器的适当解决方案,它来自已经安装在工作站上的NodeJS w/ NVM的旧版本。

代码语言:javascript
运行
复制
// 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 

与往常一样,当您切换到一个新的NodeJS版本时,任何与NVM一起安装的全局模块都会被淘汰。因此,确保您的项目不缺少安装在机器级别上的任何必需的全局/系统NodeJS依赖项,这些依赖项可能包含在项目的package.json文件中,也可能不包含在项目的依赖项中(@angular、create-react、typescript、ts-节点、express-生成器、AWS等)。

如果您得到运行时错误是由于缺乏支持的依赖项,则从v14->v16的迁移(您的NodeJS项目越大,就越有可能发生与代码库相关的重大更改),就会发生不推荐的API更改。如果在进行了本地烟雾测试后,NodeJS v16似乎运行良好,而且这不是您的个人项目,那么与您的领导、经理或架构师讨论提出拉请求并在更改到位后进行完全回归是谨慎的,因为这将有可能导致回归,从而访问废弃的或不正确的签名来调用过时的API。

警告:如果发现NodeJS/NPM错误,下载和切换版本后,请根据本地设置,通过手动或编程方式退出和打开shell env,确保重新加载shell env:

代码语言:javascript
运行
复制
    // 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

**总之:目前,推荐的方法是安装和使用推荐版本的NodeJS (v16),这是Apple的官方支持版本。

这样就可以测试所有东西,并且您和任何其他维护代码库的开发人员都可以充分利用v16中的所有新特性和修复程序,并通过强制他们运行一个黑客设置来避免烧掉开发时间,从而试图找到一个棘手的解决方案。这样一切都可以被彻底的测试。

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

票数 7
EN

Stack Overflow用户

发布于 2021-11-15 10:10:56

我的项目不是在新的Node.js上运行,而是在我的M1笔记本电脑的Node 14上安装dep时也遇到了同样的问题。切换到节点14.17,14.16没有帮助。

以下是我为m1用户提供的解决方案:

代码语言:javascript
运行
复制
nvm use 16
npm i --force
nvm use 14
npm run start // <- your app start command
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69452504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档