node-sass has been deprecated, click here for more information. Dart-sass is the recommended alternative and does not encounter the issues mentioned in this article. As of now, when installing sass from npm, dart-sass is the default option. - 2022-3-29
node-sass#
node-sass is a commonly used dependency package in our development process. It is also a dependency that takes a long time to install and often encounters errors. Whether you want to use it in your own project or install it as a dependency in someone else's project, running
npm install
always causes various problems.
Solution#
When encountering problems, the first thing to do is to search the search engine. Searching for "node-sass" will usually bring up keywords such as "failure" and "fail". This indicates that this problem is indeed very common. Then, I will briefly record various methods and my own method.
Here are some experiences from others:#
Download failure due to slow npm source#
Usually, it is recommended to directly use cnpm for downloading, which can improve the speed.
cnpm install
Alternatively, changing the source settings can also be a more perfect solution, without any hidden bugs.
npm config set registry https://registry.npm.taobao.org
Or, you can change the download source of node-sass to the Taobao mirror.
npm config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
Slow or inaccessible binary file source#
In addition to the npm part of node-sass, it also downloads binary files. However, the default source is GitHub, which is known to be slow to access in China and sometimes even inaccessible. We can also change it to a domestic source by simply adding an environment variable:
set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sass
Alternatively, you can add a .npmrc
file in the project and add the following line:
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
This way, when using npm to install node-sass, it will download the binary files from the Taobao mirror.
Final solution#
The final solution! Use a VPN and set up a proxy.
npm config set proxy http://127.0.0.1:#your_local_port_for_the_VPN#
npm install node-sass
# After the download is complete, remove the http proxy
npm config delete proxy
Special method#
If the download fails, we can download it to the local machine in advance. First, check the system version to determine which version of the binary file is suitable.
Use the following command to check:
node -p "[process.platform, process.arch, process.versions.modules].join('-')"
It will display the system version in the form of win32-x64-83
. Then, choose one of the following two addresses to download the node-sass file with the corresponding system version and the .node
extension:
Next, we need to manually specify the download source of the node-sass binary file as the downloaded file.
npm config set sass-binary-path path_to_the_directory_where_you_saved_the_binary_file
// For example: npm config set sass-binary-path e:/web/win32-x64-48_binding.node
After that, running npm i
should do the trick. However, this method prevents updating the version of node-sass. It is recommended to try this method only if the previous methods do not work.