Thursday, February 20, 2020

How to solve npm install issue for installing node-sass on Windows.


When we run npm install on a project with a dependency on  node-sass, we often get an error like
"Unable to find a sass binary for your platform", and thus a compiling process is triggered to compile the sass binary from source code. Which requires Python, Visual Studio and Dot net Framework are installed on local.

Option 1. Install required build tools, such as Python, Visual Studio and dot net framework. But basically we don't need to manually install them. An npm tool is out there can help us to install all of them. windows-build-tools.
One of a disadvantage of this option is when we install windows-build-tools, it requires administrator privilege to perform installation. And per my company's policy, we are not allowed to acquire administrator privilege. And I have to find another way.

I was thinking, do we have to compile sass binary from source code? Windows is a common platform, it is impossible that there's no existing binary for this platform. The reason of I got the error of "Unable to find a sass binary for your platform" maybe just there's no sass binary for my platform on the registry I was using.
Option 2. After some research, I notice there is an argument for installing sass-binary to indicate the source it uses to fetch the binary. And I saw some said the sass binary for windows platform exists in taobao registry. So with this argument we can direct node-sass download sass binary from taobao.
So run npm install like this:
npm install --sass_binary_site=https://npm.taobao.org/mirrors/node-sass

Or this argument can also be configured in .npmrc file
sass_binary_site=https://npm.taobao.org/mirrors/node-sass

Then node-sass can automatically download sass binary from taobao registry without compiling it.

Reference
node-sass Binary configuration parameters
整理 node-sass 安装失败的原因及解决办法