原文连接
做者 李平海
node-sass是咱们开发中很常见的依赖包,也是安装时间冗长和最多见到报错的依赖。
安装node-sass失败缘由有不少种,咱们在说失败缘由以前,先来分析一下node-sass的安装过程(如下node版本为v10.15.3):node
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有几个步骤:python
binding.node
,若有即跳过安装;binding.node
则从github下载该二进制文件并将其缓存到全局;binding.node
下载失败,则尝试本地编译出该文件;package-lock.json
;由此看到,实际上node-sass依赖了一个二进制文件binding.node
,从npm源安装完本体后还会从github下载binding.node
。linux
所以安装node-sass相关的失败缘由有如下几种:git
因为众所周知的国内网络环境,从国内安装官方源的依赖包会很慢。能够将npm源设置成国内镜像源(如淘宝npm):github
npm config set registry https://registry.npm.taobao.org
或者经过.npmrc
文件设置:chrome
// .npmrc registry=https://registry.npm.taobao.org/
node-sass除了npm部分的代码,还会下载二进制文件binding.node
,默认源是github,国内访问较慢,特殊时期甚至没法访问。咱们也能够将其改为国内源:shell
// 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
文件设置:npm
// .npmrc sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
有相似问题的还有chromedriver
,phantomjs
,electron
等常见依赖包,咱们能够一并写到.npmrc
中:json
// .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,对应版本兼容以下(或参考官方仓库):api
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的版本便可。
假如本地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。假如你遇到前面几种状况解决了,实际上也不会出如今本地构建的状况了,咱们就不谈这种失败中失败的状况吧 :-)