簡體   English   中英

嘗試運行 npm install 或 yarn 時出現 Zlib 錯誤

[英]Zlib error when attempting to run npm install or yarn

我剛剛從我的圖書館 GitHub 中提取了一些內容,我正在使用我的 windows 計算機在 VSCode 中進行編碼。 代碼沒有問題,盡管當我嘗試運行 npm install 或 yarn install 來獲取 node_modules 和 yarn.lock 時,我收到了一個奇怪的錯誤,並且軟件包無法正常工作。 我正在使用 ZSH 作為我的 Mac 的終端。

這是錯誤 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/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 % 

我來自 node、npm 和 yarn 的版本:

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

我嘗試了什么:

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

這是日志結果的結尾:

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 或 yarn,我不會收到任何錯誤

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

如果您的開發環境絕對需要較低版本的 Node

  • Apple M1 基於 ARM 和 Node v12 是必須的

對我來說,問題始於 nvm,所以我不得不從那里開始,但我設法使用此 NVM 故障排除指南修復了它,最終能夠 npm 安裝我的依賴項。 在 shell 中執行此操作:

  1. 使用$ arch驗證你在 arm64 中
  2. 安裝 Rosetta $ 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 安裝某些東西時,您將看到不正確的數據檢查錯誤。如果您想避免處理可能的麻煩this, include that flag. For more details, see this issue and this comment" 6.退出回到你的本地 shell

$ exit
$ arch
arm64
  1. 驗證您的節點架構是否正確。
$ node -p process.arch
x64
  1. 刪除后我能夠npm install
  • /節點模塊
  • package-lock.json 文件。

在我們的某些工作站上遇到類似問題后,我會說它肯定看起來像是 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

我的項目沒有在新的 Node.js 上運行,但我在 M1 筆記本電腦上的 Node 14 上安裝 deps 時也遇到了同樣的問題。 切換到 Node 14.17、14.16 沒有幫助。

這是我對 m1 用戶的解決方法:

nvm use 16
npm i --force
nvm use 14
npm run start // <- your app start command

切換到 Node 14.17、14.16 可能對您不起作用,因為在 Node v16 之前,新的基於 Apple M1 ARM 的 CPU 架構尚未進入支持路徑。 這是適用於 M1/Apple Silicon/ARM 處理器的適當解決方案,這些處理器來自已安裝在工作站上的帶有 NVM 的舊版 NodeJS。

// 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 

像往常一樣,當您使用 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
 

**總結:目前推薦的方式是安裝使用推薦版本的NodeJS(v16),這是Apple M1 ARM CPU官方支持的版本。

通過這種方式,所有內容都可以進行測試,並且您和維護代碼庫的任何其他開發人員可以充分利用 v16 中的所有新功能和修復程序,並通過強制他們運行 hacky 設置,嘗試 hacky 解決方法來防止浪費開發時間。 這樣一切都可以徹底測試。

注意:最終,有人將需要升級 NodeJS 運行時版本,以便留在您的支持路徑中並使用現代依賴項、安全/性能修復和利用現代 API,並針對最新的受支持 JavaScript 版本。 如果你正在與一個開發人員團隊一起工作,他們在不同類型的工作站上工作,那么這個問題將在研究期間占用開發時間(並且來到這篇文章或找出他們自己的解決方法,要么可能很危險並導致不常見的、難以跟蹤/調試的錯誤和不一致的開發環境,而不僅僅是在遷移到 v16 時出現問題時修復所需的構建/運行時/依賴項升級/替換)。

我開始在 M1 mac 上使用任何節點 v14 時遇到相同的錯誤

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

嘗試了很多東西,這對我有用:

安裝對 M1 mac 有本機支持的 ​​node v16
這可能是你唯一需要做的事情......

但我需要處理一些項目,每個人都在使用 node v14 和 npm v6
對我有用的是將節點 16 與降級的 npm 一起使用:

  1. 為此安裝一個單獨的節點版本: nvm install v16.13.1
  2. 切換到它並降級 npm: npm i -g npm@6.14.16
  3. 可選別名: nvm alias compat v16.13.1

最后你應該有類似的東西:

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

對我來說,v16 的唯一問題是它更改了舊項目的 package-lock.json 版本。


我試過但沒有用

其他沒有運氣的節點 v14 版本(同樣的錯誤)

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

嘗試在 v14 上升級 npm

嘗試安裝npm@6.14.16 itelsef 導致 zlib 錯誤

npm doctor

說一切都很好

npm cache clean -f

之后同樣的錯誤

轉到一個新文件夾並開始一個新項目。

安裝一些包是可行的,但是嘗試一個我在另一個項目中遇到zlib錯誤的包在新項目中導致了同樣的錯誤

刪除node_modules

與上面類似。 一些軟件包設法安裝但不是全部(zlib)

對於那些使用帶 m1 芯片的 macbook 的人,我遇到了這個問題,並嘗試使用 nvm 安裝以下節點版本,但錯誤仍然存在:

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

我嘗試清理緩存和此處提到的所有解決方案,我什至嘗試了節點和 npm 的不同組合,但沒有成功。

為了解決這個問題,我必須運行命令來切換架構: arch -x86_64 zsh

之后,我能夠順利安裝 npm。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM