在網(wǎng)上看到了很多人在安裝node-sass的時候遇到了難以安裝的問題,網(wǎng)上給出的解決辦法也有很多。這里給總結(jié)下,主要原因有以下三種:
1.Node.js與node-sass的版本沖突
打開node-sass的介紹https://www.npmjs.com/package/node-sass。Node 8需要的node-sass版本是4.5.3+,Node 10需要的node-sass版本是4.9++,確認(rèn)工程中依賴的node-sass版本和Node.js版本是否沖突。
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 |
2.沒有安裝node-gyp先決條件
以下是https://www.npmjs.com/package/node-sass文檔中的部分內(nèi)容:
Install
npm install node-sass
Compiling on Windows machines requires the node-gyp prerequisites.(安裝node-sass必須先安裝node-gyp)
Are you seeing the following error? Check out our Troubleshooting guide.
SyntaxError: Use of const in strict mode.
Having installation troubles? Check out our Troubleshooting guide.
3.無法下載win32-x64-x_binding.node文件
很多人遇到如下圖所示的錯誤,無法下載win32-x64-x_binding.node。
官方文檔已經(jīng)說明,先設(shè)置國內(nèi)的鏡像再進(jìn)行安裝:
Install from mirror in China
npm install -g mirror-config-china --registry=http://registry.npm.taobao.org npm install node-sass
也可以按以下命令進(jìn)行安裝(見Troubleshooting guide):
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass
npm install node-sass
另外,國內(nèi)安裝一些npm工具時,常常遇到類似無法下載其依賴文件的問題,可以統(tǒng)一使用cnpm進(jìn)行安裝:
rem 安裝淘寶定制的cnpm命令
npm install --global cnpm
rem 使用cnpm命令進(jìn)行安裝
cnpm install node-sass
上述所說的三種安裝方式經(jīng)過驗證,均可成功安裝node-sass。