当前位置: 移动技术网 > IT编程>脚本编程>NodeJs > 整理 node-sass 安装失败的原因及解决办法(小结)

整理 node-sass 安装失败的原因及解决办法(小结)

2020年05月13日  | 移动技术网IT编程  | 我要评论
npm install 时偶尔遇到报错:没有安装python或node-sass 安装失败的问题,百度之后发现是被墙了,但根据百度的方法换了淘宝镜像和用了vpn都安装失败,最后发现原来是因为没有卸载之

npm install 时偶尔遇到报错:没有安装python或node-sass 安装失败的问题,百度之后发现是被墙了,但根据百度的方法换了淘宝镜像和用了vpn都安装失败,最后发现原来是因为没有卸载之前安装失败的包导致的。作者本人最后的解决方案是npm uninstall node-sass,然后使用vpn重新安装了一遍就成功了。不能翻墙的同学请看下文

node-sass 安装失败的原因

npm 安装 node-sass 依赖时,会从 github.com 上下载 .node 文件。由于国内网络环境的问题,这个下载时间可能会很长,甚至导致超时失败。

这是使用 sass 的同学可能都会遇到的郁闷的问题。

解决方案就是使用其他源,或者使用工具下载,然后将安装源指定到本地。

解决方法一:使用淘宝镜像源(推荐)

设置变量 sass_binary_site,指向淘宝镜像地址。示例:

npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

// 也可以设置系统环境变量的方式。示例
// 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

或者设置全局镜像源:

npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/

之后再涉及到 node-sass 的安装时就会从淘宝镜像下载。

解决方法二:使用 cnpm

使用 cnpm 安装 node-sass 会默认从淘宝镜像源下载,也是一个办法:

cnpm install node-sass

解决方法三:创建.npmrc文件

在项目根目录创建.npmrc文件,复制下面代码到该文件。

phantomjs_cdnurl=http://cnpmjs.org/downloads
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
registry=https://registry.npm.taobao.org

保存后 删除之前安装失败的包(第一次安装请跳过此步)

npm uninstall node-sass

重新安装

npm install node-sass

作者后来另一个项目在没有使用vpn的情况下测试此方法,安装时报错 err! node-sass@3.8.0 postinstall: `node scripts/build.js 改用方法一成功。

解决方法四:下载 .node 到本地

到去根据版本号、系统环境,选择下载 .node 文件,然后安装时,指定变量 sass_binary_path,如:

npm i node-sass --sass_binary_path=/users/lzwme/downloads/darwin-x64-48_binding.node

安装失败后重新安装问题

之前安装失败,再安装就不去下载了,怎么办呢?那就先卸载再安装:

npm uninstall node-sass
npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

相关错误提示

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

gyp err! configure error
gyp err! stack error: can't find python executable "c:\users\zhuon\appdata\local\programs\python\python36\python.exe", you can set the python env variable.
gyp err! stack   at pythonfinder.failnopython (g:\workspace\manyan\manyan-nav\node_modules\node-gyp\lib\configure.js:483:19)
gyp err! stack   at pythonfinder.<anonymous> (g:\workspace\manyan\manyan-nav\node_modules\node-gyp\lib\configure.js:508:16)
gyp err! stack   at g:\workspace\manyan\manyan-nav\node_modules\graceful-fs\polyfills.js:284:29
gyp err! stack   at fsreqwrap.oncomplete (fs.js:152:21)
gyp err! system windows_nt 10.0.15063
gyp err! command "c:\\dev\\nodejs\\node.exe" "g:\\workspace\\manyan\\manyan-nav\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags="
"--libsass_library="
gyp err! cwd g:\workspace\manyan\manyan-nav\node_modules\node-sass
gyp err! node -v v8.4.0
gyp err! node-gyp -v v3.6.2
gyp err! not ok
build failed
npm warn co-mocha@1.2.0 requires a peer of mocha@>=1.18 <4 but none was installed.
npm warn egg-restapi-module-tool@1.0.0 no repository field.
npm warn egg-restapi-module-tool@1.0.0 scripts['server'] should probably be scripts['start'].

npm err! code elifecycle
npm err! errno 1
npm err! node-sass@3.8.0 postinstall: `node scripts/build.js`
npm err! exit status 1
npm err!
npm err! failed at the node-sass@3.8.0 postinstall script.
npm err! this is probably not a problem with npm. there is likely additional logging output above.

npm err! a complete log of this run can be found in:
npm err!   d:\nodejs\cache\_logs\2017-09-02t16_06_24_298z-debug.log

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网