node-sass是我們開發中很常見的依賴包,也是安裝時間冗長和最常見到報錯的依賴。
安裝node-sass失敗原因有很多種,我們在說失敗原因之前,先來分析一下node-sass的安裝過程(以下node版本為v10.15.3):
PS D:\demo> npm i node-sass
// 從npm源安裝到node_modules > node-sass@4.13.0 install D:\demo\node_modules\node-sass > node scripts/install.js // 下載binding.node Downloading binary from https://github.com/sass/node-sass/releases/download/v4.13.0/win32-x64-64_binding.node Download complete .] - : Binary saved to D:\demo\node_modules\node-sass\vendor\win32-x64-64\binding.node // 緩存binding.node Caching binary to C:\Users\leepi\AppData\Roaming\npm-cache\node-sass\4.13.0\win32-x64-64_binding.node > node-sass@4.13.0 postinstall D:\demo\node_modules\node-sass > node scripts/build.js Binary found at D:\demo\node_modules\node-sass\vendor\win32-x64-64\binding.node Testing binary Binary is fine // 寫package-lock.json npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN demo@1.0.0 No description npm WARN demo@1.0.0 No repository field. + node-sass@4.13.0 added 174 packages from 138 contributors and audited 529 packages in 24.379s found 0 vulnerabilities
我們可以看到,安裝node-sass有幾個步驟:
- 校驗本地node_modules中是否已安裝node-sass,版本是否一致;
- 如未安裝或版本不符,從npm源安裝node-sass本體;
- 檢測全局緩存和本地中是否有binding.node,如有即跳過安裝;
- 沒有binding.node則從github下載該二進制文件並將其緩存到全局;
- 假如binding.node下載失敗,則嘗試本地編譯出該文件;
- 將版本信息寫到package-lock.json;
由此看到,實際上node-sass依賴了一個二進制文件binding.node,從npm源安裝完本體后還會從github下載binding.node。
因此安裝node-sass相關的失敗原因有以下幾種:
原因一: npm源速度慢
由於眾所周知的國內網絡環境,從國內安裝官方源的依賴包會很慢。可以將npm源設置成國內鏡像源(如淘寶npm):
npm config set registry https://registry.npm.taobao.org
或者通過.npmrc文件設置:
// .npmrc registry=https://registry.npm.taobao.org/
原因二: binding.node源無法訪問或速度慢
node-sass除了npm部分的代碼,還會下載二進制文件binding.node,默認源是github,國內訪問較慢,特殊時期甚至無法訪問。我們也可以將其改成國內源:
// linux、mac 下 SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass // window 下 set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sass
或者通過.npmrc文件設置:
// .npmrc sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
有類似問題的還有chromedriver,phantomjs,electron等常見依賴包,我們可以一並寫到.npmrc中:
// .npmrc sass_binary_site=https://npm.taobao.org/mirrors/node-sass chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs electron_mirror=https://npm.taobao.org/mirrors/electron
原因三: node版本與node-sass版本不兼容
node-sass版本兼容性並不好,老項目中依賴的node-sass很可能已經不兼容新的node,對應版本兼容如下(或參考官方倉庫):
| NodeJS | Minimum node-sass version | Node Module |
|---|---|---|
| Node 13 | 4.13+ | 79 |
| Node 12 | 4.12+ | 72 |
| Node 11 | 4.10+ | 67 |
| Node 10 | 4.9+ | 64 |
| Node 8 | 4.5.3+ | 57 |
本文開頭的安裝例子中,binding.node的版本是v4.13.0/win32-x64-64_binding.node,可以看到,里面包括node-sass版本號v4.13.0,平台win32,架構x64,以及Node Module的版本64。Node Module是node的一個模塊,其版本號可以在進程process.versions中查到:
PS D:\demo> node > console.log(process.versions); { http_parser: '2.8.0', node: '10.15.3', v8: '6.8.275.32-node.51', uv: '1.23.2', zlib: '1.2.11', ares: '1.15.0', modules: '64', nghttp2: '1.34.0', napi: '3', openssl: '1.1.0j', icu: '62.1', unicode: '11.0', cldr: '33.1', tz: '2018e' } undefined >
如上顯示,node10.15.3對應的module版本為64。
假如node-sass與node的版本不兼容,就會找不到對應的binding.node而報錯,例如你的node是10.15.3,裝node-sass4.6.1,則會嘗試安裝v4.6.1/win32-x64-64_binding.node,但這個版本的binding.node是不存在的。
此時改node-sass或node的版本即可。
PPT模板下載大全https://www.wode007.com
原因四: 緩存中binding.node版本不一致
假如本地node版本改了,或在不同機器上運行,node版本不一致,會報類似錯誤:
Found bindings for the following environments: - Windows 64-bit with Node.js 6.x
這是因為原有binding.node緩存跟現node版本不一致。按提示npm rebuild node-sass或清除緩存重新安裝即可。
原因五: 安裝失敗后重新安裝
安裝失敗后重新安裝,有可能無權限刪除已安裝內容,此時npm uninstall node-sass或手動刪掉原目錄后再安裝即可。
原因六: 提示沒有安裝python、build失敗等
假如拉取binding.node失敗,node-sass會嘗試在本地編譯binding.node,過程就需要python。假如你遇到前面幾種情況解決了,實際上也不會出現在本地構建的情況了,我們就不談這種失敗中失敗的情況吧 :-)
