跳到主内容

node-sass总是安装失败?

· 2分钟阅读

tags: [faq]

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 的版本不兼容,就会找不到对应的 binding.node 而报错

例如你的 node 是 10.15.3,装 node-sass4.6.1,则会尝试安装 v4.6.1/win32-x64-64_binding.node,但这个版本的 binding.node 是不存在的。

此时改 node-sass 或 node 的版本即可。

缓存中 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或清除缓存重新安装即可。

提示没有安装 python、build 失败等

假如拉取binding.node失败,node-sass 会尝试在本地编译binding.node,过程就需要 python