Solution for node-sass installation failure

There are several steps to install node-sass:

  1. Check whether node-sass has been installed in the local node_modules and whether the version is the same;
  2. If it is not installed or the version does not match, install the node-sass ontology from the npm source;
  3. Detect whether there is binding.node in the global cache and local, if so, skip the installation;
  4. Without binding.node download the binary from github and cache it globally;
  5. If the binding.node download fails, try to compile the file locally;
  6. Write version information to package-lock.json;

It can be seen from this that node-sass actually depends on a binary file binding.node. After installing the ontology from the npm source, it will download binding.node from github.

Therefore, the failure reasons related to installing node-sass are as follows:

Reason 1: npm source is slow

Due to the well-known domestic network environment, it will be very slow to install the dependency package from the official source from China. You can set the npm source to a domestic mirror source (such as Taobao npm):

npm config set registry https://registry.npm.taobao.org

Or set via .npmrc file:

// .npmrc
registry=https://registry.npm.taobao.org/

Reason 2: The binding.node source is inaccessible or slow

In addition to the npm part of the code, node-sass also downloads the binary file binding.node, the default source is github, domestic access is slow, and it is even inaccessible during special periods. We can also change it to a domestic source:

// under linux, mac
SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass

// under the window
set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sass

Or set via .npmrc file:

// .npmrc
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

There are also common dependencies such as chromedriver,phantomjs,electron, etc. that have similar problems, we can write them together in .npmrc:

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

Reason 3: The node version is not compatible with the node-sass version

The node-sass version compatibility is not good. The node-sass that the old project depends on is probably not compatible with the new node. The corresponding version is compatible as follows (or refer to Official warehouse):

NodeJSMinimum node-sass versionNode Module
Node 134.13+79
Node 124.12+72
Node 114.10+67
Node 104.9+64
Node 84.5.3+57

In the installation example at the beginning of this article, the version of binding.node is v4.13.0/win32-x64-64_binding.node. As you can see, it includes node-sass version number v4.13.0, platform win32, architecture x64, and Node Module. Version 64. Node Module is a module of node, and its version number can be found in the process process.versions:

PS D:\demo> node
> console.log(process.versions);
{ http_parser: '2.8.0',
  node: '10.15.3',
  v8: '6.8.275.32-node.51',
  uv: '1.23.2',
  zlib: '1.2.11',
  ares: '1.15.0',
  modules: '64',
  nghttp2: '1.34.0',
  napi: '3',
  openssl: '1.1.0j',
  icu: '62.1',
  unicode: '11.0',
  cldr: '33.1',
  tz: '2018e' }
undefined
>

As shown above, the module version corresponding to node10.15.3 is 64. If node-sass is not compatible with the version of node, the corresponding binding.node will not be found and an error will be reported. For example, if your node is 10.15.3 and node-sass4.6.1 is installed, it will try to install v4.6.1/win32- x64-64_binding.node, but this version of binding.node does not exist. At this point, you can change the version of node-sass or node.

Tags: Vue.js Front-end Webpack npm

Posted by delmardata on Sun, 13 Nov 2022 16:12:54 +0530