lmk123 / blog

个人技术博客,博文写在 Issues 里。
https://github.com/lmk123/blog/issues
625 stars 35 forks source link

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

Open lmk123 opened 8 years ago

lmk123 commented 8 years ago

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

lzxb commented 7 years ago

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

lmk123 commented 7 years ago

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

lzxb commented 7 years ago

@lmk123 669

wanming commented 7 years ago

alec-baldwin-approval-Cgo9XkgzRcTPW

megone commented 7 years ago

楼主威武

lmk123 commented 7 years ago

@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 这一行给注释掉,否则就会发布到淘宝源上去了。

yyzych commented 7 years ago

楼主大发!

xiaofuyesnew commented 7 years ago

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

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

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

paysage commented 7 years ago

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

xiaofuyesnew commented 7 years ago

@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 commented 7 years ago

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

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

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

xiaofuyesnew commented 7 years ago

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

leiboo commented 7 years ago

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

xhlwill commented 7 years ago

@xiaofuyesnew 用了Cmder 也感觉很慢 TT

zicongxie commented 7 years ago

well done!

hy-inger commented 7 years ago

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

hardmen commented 7 years ago

第一种方法有用

nellochen commented 7 years ago

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

yifeikong commented 7 years ago

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

onlywhite commented 7 years ago

感谢楼主

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

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

xhlwill commented 7 years ago

具体怎么出现报错

krystalcl commented 7 years ago

楼主666

krystalcl commented 7 years ago

正好遇到这个问题,感谢感谢

xhlwill commented 7 years ago

@onlywhite 不用的,set设置的环境变量只在当前console生效,只是临时的,要永久用setx

yin-fan commented 7 years ago

感谢楼主,问题解决了

andyiac commented 6 years ago

使用梯子还有一种一劳永逸的姿势:

安装配置下 proxychains4

brew install proxychains-ng

在配置文件

/usr/local/etc/proxychains.conf

下添加一行

socks5  127.0.0.1 1080

然后

proxychains4 npm install 

以后下载被河蟹的内容加上 proxychains4 就行了

revanli commented 6 years ago

各种google, 后来用中文搜一下,找到这里,第一条管用,蛋疼的问题

feizhen commented 6 years ago

其实可以安装cnpm,然后使用cnpm来安装就好了

fanerge commented 6 years ago

我是用方法三弄好了,社区的力量强大

yl1003 commented 6 years ago

方法1直接就可以了

delsedin commented 6 years ago

感谢感谢,用方法1终于搞定。

xoxander commented 6 years ago

感谢感谢感谢!折腾半天了!楼主威武!方法一弄好的

JoeeeeeWu commented 6 years ago

good

PiscesKnight commented 6 years ago

用的第二个方法,超感谢,折腾老半天

Vivomo commented 6 years ago

用的第二个方法, 感激涕零

LiuSuY commented 6 years ago

感谢楼主,帮助我很容易解决了这个问题

soarpatriot commented 6 years ago

🙏 感谢

LiuSuY commented 6 years ago

现在可以使用yarn安装也可以解决这个问题

nonlau commented 6 years ago

windows下,拆分命令后,还是不行 $ npm install node-sass

node-sass@4.7.2 install E:\workSpace\nonlau\hexo\node_modules\node-sass node scripts/install.js

'node' ▒▒▒▒▒ڲ▒▒▒▒ⲿ▒▒▒Ҳ▒▒▒ǿ▒▒▒▒еij▒▒▒ ▒▒▒▒▒▒▒▒▒ļ▒▒▒ npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\vents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fseven1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x6)

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.7.2 install: node scripts/install.js npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@4.7.2 install script.

LiuSuY commented 6 years ago

你可以试试yarn add node-sass

Yapril commented 6 years ago

用git终端 npm install node-sass 一次搞定~

EchoQ0Q commented 6 years ago

楼主请问下,如果我内网安装node-sass,并且把binding.node文件放在了C盘根目录下,应该如何设置才能找到这个文件呢?

LiuSuY commented 6 years ago

1 . node -p "[process.platform, process.arch, process.versions.modules].join('-')"

  1. set SASS_BINARY_PATH=D:/nodejs/.nodes/win32-x64-57_binding.node (win32-x64-57_binding.node文件百度下载)
  2. npm install node-sass
morenyang commented 6 years ago

使用yarn的话可以把下面这一行代码加入到.yarnrc里面 mac实测秒载 sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"

vv314 commented 6 years ago

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,和手动修改是等价的。

jerry9926 commented 6 years ago

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

JSoon commented 6 years ago

mark

taojason88 commented 6 years ago

感觉楼主的第一方法,弄了几个小时终于好了

yexk commented 6 years ago

gyp verb tmpdir == cwd automatically will remove dev files after to save disk space
gyp verb command install [ '10.1.0' ]
gyp verb install input version string "10.1.0"
gyp verb install installing version: 10.1.0
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 10.1.0
gyp verb ensuring nodedir is created /www/project/node_modules/node-sass/.node-gyp/10.1.0
gyp WARN EACCES user "root" does not have permission to access the dev dir "/www/project/node_modules/node-sass/.node-gyp/10.1.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/www/project/node_modules/node-sass/.node-gyp"

大佬。centos 7.2 ,node v10.1.0 npm 5.6.0 淘宝源。。这个错误怎么解决??

hucheng91 commented 6 years ago

发现其实在github下不到,是会去本地的 Users\user\AppData\Roaming\npm-cache\node-sass(比方,我电脑用户名叫 hc,一般在c:\Users\user\AppData\Roaming\npm-cache\node-sass)这个目录找,那解决办法就是,先去https://github.com/sass/node-sass/releases,将需要的*_binding.node下载下来,放到本地对应目录就好