node-sass是咱們開發中很常見的依賴包,也是安裝時間冗長和最多見到報錯的依賴。緣由有不少種,咱們在說報錯緣由以前,先來分析一下node-sass的安裝過程(如下出現的node版本爲v10.15.3):python
PS D:\demo> npm i node-sass
> node-sass@4.13.0 install D:\demo\node_modules\node-sass
> node scripts/install.js
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
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
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有幾個步驟:linux
binding.node
,若有即跳過安裝;binding.node
則從github下載該二進制文件並將其緩存到全局;binding.node
下載失敗,則嘗試本地編譯出該文件;package-lock.json
;由此看到,實際上node-sass依賴了一個二進制文件binding.node
,從npm源安裝完本體後還會從github下載binding.node
。git
所以安裝node-sass相關的失敗緣由有幾種:github
因爲衆所周知的國內網絡環境,從國內安裝官方源的依賴包會很慢。能夠將npm源設置成國內鏡像源(如淘寶npm):chrome
npm config set registry https://registry.npm.taobao.org
複製代碼
或者經過.npmrc
文件設置:shell
// .npmrc
registry=https://registry.npm.taobao.org/
複製代碼
node-sass除了npm部分代碼,還會下載二進制文件binding.node
,默認源是github,國內訪問較慢,特殊時期甚至沒法訪問。咱們也能夠將其改爲國內源:npm
// 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
文件設置:json
// .npmrc
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
複製代碼
有相似問題的還有chromedriver
,phantomjs
,electron
等常見依賴包,咱們能夠一併寫到.npmrc
中:api
// .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-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-sass或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
或手動刪掉原目錄後再安裝便可。
假如拉取binding.node
失敗,node-sass會嘗試在本地編譯binding.node
,過程就須要python。假如你遇到前面幾種狀況解決了,實際上也不會出如今本地構建的狀況了,咱們就不談這種失敗中失敗的狀況吧。