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 it is for our own projects or installing dependencies for other projects, running
npm install
always causes various issues.
Solution#
When encountering problems, the first thing we do is search on search engines. Searching for node-sass usually brings up keywords like "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 to download, which can improve the situation.
cnpm install
Alternatively, changing the source settings can be more perfect and prevent some hidden bugs from appearing.
npm config set registry https://registry.npm.taobao.org
Or, you can change the download source of node-sass to the Taobao mirror only.
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_VPN#
npm install node-sass
# After the download is complete, remove the http proxy
npm config delete proxy
Special method#
If the failure is due to download issues, we can download it locally 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.