【内推字节】欢迎简历chengxinsong@bytedance.com
整理 node-sass 安装失败的原因及解决办法
发布于 作者 suning 672 次浏览 来自 分享

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文件

复制下面代码到该文件

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


相关错误提示

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

gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (E:\2018github\react-chat\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (E:\2018github\react-chat\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\2018github\react-chat\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:152:21)
gyp verb `which` failed  python2 { Error: not found: python2
gyp verb `which` failed     at getNotFoundError (E:\2018github\react-chat\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (E:\2018github\react-chat\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\2018github\react-chat\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:152:21)
gyp verb `which` failed   stack: 'Error: not found: python2\n    at getNotFoundError (E:\\2018github\\react-chat\\node_modules\\which\\which.js:13:12)\n    at F (E:\\2018github\\react-chat\\no
de_modules\\which\\which.js:68:19)\n    at E (E:\\2018github\\react-chat\\node_modules\\which\\which.js:80:29)\n    at E:\\2018github\\react-chat\\node_modules\\which\\which.js:89:16\n    at E
:\\2018github\\react-chat\\node_modules\\isexe\\index.js:42:5\n    at E:\\2018github\\react-chat\\node_modules\\isexe\\windows.js:36:5\n    at FSReqWrap.oncomplete (fs.js:152:21)',
gyp verb `which` failed   code: 'ENOENT' }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` failed Error: not found: python
gyp verb `which` failed     at getNotFoundError (E:\2018github\react-chat\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (E:\2018github\react-chat\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\2018github\react-chat\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:152:21)
gyp verb `which` failed  python { Error: not found: python
gyp verb `which` failed     at getNotFoundError (E:\2018github\react-chat\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (E:\2018github\react-chat\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\2018github\react-chat\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\2018github\react-chat\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:152:21)
gyp verb `which` failed   stack: 'Error: not found: python\n    at getNotFoundError (E:\\2018github\\react-chat\\node_modules\\which\\which.js:13:12)\n    at F (E:\\2018github\\react-chat\\nod
e_modules\\which\\which.js:68:19)\n    at E (E:\\2018github\\react-chat\\node_modules\\which\\which.js:80:29)\n    at E:\\2018github\\react-chat\\node_modules\\which\\which.js:89:16\n    at E:
\\2018github\\react-chat\\node_modules\\isexe\\index.js:42:5\n    at E:\\2018github\\react-chat\\node_modules\\isexe\\windows.js:36:5\n    at FSReqWrap.oncomplete (fs.js:152:21)',
gyp verb `which` failed   code: 'ENOENT' }
gyp verb could not find "python". checking python launcher
gyp verb could not find "python". guessing location
gyp verb ensuring that file exists: C:\Python27\python.exe
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (E:\2018github\react-chat\node_modules\node-gyp\lib\configure.js:483:19)
gyp ERR! stack     at PythonFinder.<anonymous> (E:\2018github\react-chat\node_modules\node-gyp\lib\configure.js:508:16)
gyp ERR! stack     at E:\2018github\react-chat\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:152:21)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "D:\\Program Files\\nodejs\\node.exe" "E:\\2018github\\react-chat\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libs
ass_ldflags=" "--libsass_library="
gyp ERR! cwd E:\2018github\react-chat\node_modules\node-sass
gyp ERR! node -v v8.11.3
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
Build failed with error code: 1
npm WARN acorn-jsx@5.0.1 requires a peer of acorn@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.9.0 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.9.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:\Program Files\nodejs\node_cache\_logs\2019-02-27T07_39_14_912Z-debug.log


发布评论
微信群
saucxs聊天机器人
saucxs
hi ,欢迎来到sau交流学习社区,欢迎与我聊天,问我问题哦!
您正在使用的浏览器是,不在支持范围内!
为了您的正常使用与展示,推荐使用Chrome浏览器68以上版本
支持浏览器:
火狐浏览器最新版
Safari浏览器最新版
Edge浏览器最新版
IE浏览器10,11