Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安装 node-sass 的正确姿势 #28

Open
lmk123 opened this issue Nov 19, 2015 · 67 comments
Open

安装 node-sass 的正确姿势 #28

lmk123 opened this issue Nov 19, 2015 · 67 comments
Labels

Comments

@lmk123
Copy link
Owner

lmk123 commented Nov 19, 2015

2019.04.25 更新:

大家可以用 dart-sass 无缝替换 node-sass 了!dart-sass 兼容 node-sass 的 API,而且安装过程无需下载二进制文件,这样大家就不需要用本篇文章的方式安装 node-sass 了。


安装 node-sass 的时候总是会各种不成功,今天我琢磨了一会儿总算知道要怎么解决了。

首先要知道的是,安装 node-sass 时在 node scripts/install 阶段会从 github.com 上下载一个 .node 文件,大部分安装不成功的原因都源自这里,因为 GitHub Releases 里的文件都托管在 s3.amazonaws.com 上面,而这个网址在国内总是网络不稳定,所以我们需要通过第三方服务器下载这个文件。(顺带一提,你可以看看这个好玩的 commit

方法一:使用淘宝镜像

macOS 系统直接运行下面的命令即可:

SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

我们一般更希望能跨平台、并且直接使用 npm install 安装所有依赖,所以我的做法是在项目内添加一个 .npmrc 文件:

sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
electron_mirror=https://npm.taobao.org/mirrors/electron/
registry=https://registry.npm.taobao.org

这样使用 npm install 安装 node-sasselectronphantomjs 时都能自动从淘宝源上下载,但是在使用 npm publish 的时候要把 registry 这一行给注释掉,否则就会发布到淘宝源上去了。

方法二:使用梯子

假设你的梯子在你本地机器上开启了一个第三方服务器 127.0.0.1:1080,那么只需按照下面的方法配置一下就能正常安装 node-sass 了(如果你开启的是 PAC 模式而不是全局模式,那还需要将 s3.amazonaws.com 加入 PAC 列表):

npm config set proxy http://127.0.0.1:1080
npm i node-sass

# 下载完成后删除 http 代理
npm config delete proxy

嗯,这样下来就能正常安装了。


给自己的软件打个广告:跨平台 & 一站式划词、截图、网页全文、音视频翻译扩展——划词翻译 https://hcfy.app/docs/guides/summary

@lmk123 lmk123 added the Sass label Jan 20, 2016
@lzxb
Copy link

lzxb commented Jul 26, 2016

卧槽,搞了一个下午,还是楼主的第一条管用

@lmk123
Copy link
Owner Author

lmk123 commented Jul 26, 2016

哈哈,我当初也是搞了一下午才弄明白

@lzxb
Copy link

lzxb commented Jul 26, 2016

@lmk123 669

@wanming
Copy link

wanming commented Aug 20, 2016

alec-baldwin-approval-Cgo9XkgzRcTPW

@megone
Copy link

megone commented Aug 29, 2016

楼主威武

@lmk123
Copy link
Owner Author

lmk123 commented Oct 8, 2016

@dear-lizhihua 我的做法是在项目内添加一个 .npmrc 文件:

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

这样使用 npm install 安装 node-sass 和 phantomjs 时都能自动从淘宝源上下载,但是在使用npm publish 的时候要把 registry 这一行给注释掉,否则就会发布到淘宝源上去了。

sdvcrx added a commit to sdvcrx/dotfiles that referenced this issue Oct 15, 2016
@yyzych
Copy link

yyzych commented Feb 5, 2017

楼主大发!

@xiaofuyesnew
Copy link

对windows下的同学如果第一种办法无效的话可以拆分成两个命令

set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
npm install node-sass

然后每次安装之前都要set一下。秒装。

@paysage
Copy link

paysage commented Feb 8, 2017

请问对应的依赖也会一起安装吗? 还是还得单独安装?

@xiaofuyesnew
Copy link

@paysage 不需要单独安装,如果依赖中有node-sass的话,直接在npm install前面加SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
也就是这样:

SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install

在遇到node-sass的二进制文件时,会自动从淘宝的镜像里面开始下载。

结论: node-sass不需要单独安装

@xhlwill
Copy link

xhlwill commented Feb 13, 2017

windows下在 git bash 窗口里(前提是安装了git)也可以直接运行这条命令

SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

因为安装git就同时为windows安装了一个gnu环境 - mingw

@xiaofuyesnew
Copy link

@xhlwill 对,但是个人觉得git bash在win环境下的运行效率有点低,毕竟是杂交的环境。
用Cmder的话更快一点,也兼容了大部分bash命令。

@leiboo
Copy link

leiboo commented Feb 24, 2017

给大神跪了,搞了一上午,还是第一种方法管用。

@xhlwill
Copy link

xhlwill commented Feb 27, 2017

@xiaofuyesnew 用了Cmder 也感觉很慢 TT

@zicongxie
Copy link

well done!

@hy-inger
Copy link

第一种方法管用。楼主威武。

@hardmen
Copy link

hardmen commented Apr 5, 2017

第一种方法有用

@nellochen
Copy link

卧槽,前来报到,被坑了。。。一直提示.net framework的问题,windows 10系统标识蛋疼,第一种方法可行!

@yifeikong
Copy link

谢谢楼主,每当遇到这种问题,就想骂一句「他喵的方滨兴」

@onlywhite
Copy link

onlywhite commented May 8, 2017

感谢楼主

好了,我是Windows系统,按楼上方法把第一个命令拆分成两个命令就好了。最后问一下,
set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
操作完成之后,我需要把上面set的删除吗?

SASS_BINARY_SITE不是内部命令是怎么回事啊

@xhlwill
Copy link

xhlwill commented May 8, 2017

具体怎么出现报错

@hucheng91
Copy link

angular-cli-node-sass-bug-3

@hucheng91
Copy link

自己下载下来,放到cache 目录下就可以了,lunix 系在 当前用户 .npm 目录下

@ShumRain
Copy link

ShumRain commented Aug 1, 2018

非常感谢!

@tsingroo
Copy link

tsingroo commented Aug 1, 2018

Ubuntu下使用如下命令设置NodeSass源
export SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/

@codevvvv9
Copy link

我是deepin linux系统。
方法一之前用的deeepin 15.7能行,现在重装15.8完后竟然淘宝源都下载不了,重启无数遍,仍然无法下载,宣告失败。
方法二:我买的ss,可以FQ,npm设置代理,全局系统代理却没用,宣告失败。
只能通过下载.node文件安装。
前辈遇到过我这种问题吗,真是醉醉的了

@codevvvv9
Copy link

@tsingroo 我的淘宝源竟然都不行……

@tsingroo
Copy link

tsingroo commented Dec 4, 2018

@tsingroo 我的淘宝源竟然都不行……

没用过deepin,不太清楚

@HTPAUN
Copy link

HTPAUN commented Jul 5, 2019

谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!!!!!!!!!!!!我装不了自闭了一天!!!!!!!!!!!!!我用的

对windows下的同学如果第一种办法无效的话可以拆分成两个命令

set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
npm install node-sass
然后每次安装之前都要set一下。秒装。

这个方法!

@llyer
Copy link

llyer commented Dec 11, 2019

好用

@Q-kil
Copy link

Q-kil commented Feb 26, 2020

我太难了,从楼上试到楼下
binding.target.mk:131: recipe for target 'Release/obj.target/binding/src/binding.o' failed
make: *** [Release/obj.target/binding/src/binding.o] Error 1
make: Leaving directory '/d/code/apowo/platform-client/node_modules/node-sass/build'
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/d/code/apowo/platform-client/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack at ChildProcess.emit (events.js:311:20)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Linux 4.4.0-18362-Microsoft
gyp ERR! command "/usr/bin/node" "/d/code/apowo/platform-client/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /d/code/apowo/platform-client/node_modules/node-sass
gyp ERR! node -v v12.16.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

@cnscorpions
Copy link

环境的问题,要足够淡定 @HTPAUN

@LinZhaoguan
Copy link

mark

@djkloop
Copy link

djkloop commented Jun 29, 2020

@frei-x
Copy link

frei-x commented Jul 6, 2020

mark

@webszy
Copy link

webszy commented Dec 14, 2020

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

@Yuhemei
Copy link

Yuhemei commented Nov 2, 2021

node V14.15.3 install success

@toFrankie
Copy link

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

yarn 的 config 会继承 npm 的 config,所以只用设置 npm 的映射就行, 当然,如果不放心也可以:

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

上述命令其实就通过命令方式是改了 .npmrc.yarnrc,和手动修改是等价的。

自 yarn v2 起,就不能从 .npmrc 读取配置作为补充了,👉 Don't use .npmrc files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests