Original text link author li Pinghai

Node-sass is one of the most common dependencies in our development, as well as the one that takes a long time to install and is the most bug-prone. There are several reasons for this error. Before we say why, we should analyze the installation process of Node-sass (v10.15.3) :

PS D:\demo> npm i node-sass

>[email protected] install D: \ demo \ node_modules \ node - sass
> node scripts/install.jsDownloading binary Download from https://github.com/sass/node-sass/releases/download/v4.13.0/win32-x64-64_binding.node complete .] - : Binary saved to D:\demo\node_modules\node-sass\vendor\win32-x64-64\binding.node Caching binary to C: \ Users \ leepi \ AppData \ Roaming, NPM - cache, node - sass \ 4.13.0 \ win32 - x64-64 _binding node
>[email protected] postinstall D: \ demo \ node_modules \ node - sass
> node scripts/build.jsBinary found at D:\demo\node_modules\node-sass\vendor\win32-x64-64\binding.node Testing binary Binary is fine npm notice Created a lockfile as package-lock.json. You should commit this file. NPM WARN [email protected] No description NPM WARN [email protected] No repository field. + [email protected] added 174 packages from 138 ficol3 and audited 529 packages in 24.379 s found vulnerabilitiesCopy the code

As you can see, there are several steps to installing Node-sass:

  1. Verify whether node-sass is installed in the local node_modules and whether the version is the same.
  2. If not installed or the version is incorrect, install the Node-Sass ontology from the NPM source.
  3. Check whether global cache and local cache existbinding.node, if yes, skip the installation;
  4. There is nobinding.nodeDownload the binary from Github and cache it globally;
  5. ifbinding.nodeIf the download fails, try to compile the file locally.
  6. Write version information topackage-lock.json;

Node-sass relies on a binary file called binding.node, which will be downloaded from Github after installing the ontology from NPM source.

Therefore, there are several reasons for node-sass installation failures:

Cause one: The NPM source speed is slow

Due to the well-known domestic network environment, installing dependency packages from official sources in the country can be slow. You can set the NPM source as a domestic mirror source (such as Taobao NPM) :

npm config set registry https://registry.npm.taobao.org
Copy the code

Or via the.npmrc file:

// .npmrc
registry=https://registry.npm.taobao.org/
Copy the code

Cause two: The binding.node source is inaccessible or slow

In addition to NPM code, node-sass will also download binary file binding.node. The default source is Github. 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 / / set under the window SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sassCopy the code

Or via the.npmrc file:

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

Had similar problems and chromedriver phantomjs, electron and other common depend on the package, we can together wrote. 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
Copy the code

Cause three: The Node version is incompatible with node-sass

The node-sass version is not compatible. The node-sass version relied on in the old project may not be compatible with the new Node. The version is as follows (or refer to the official repository) :

NodeJS Minimum node-sass version Node Module
Node 13 4.13 + 79
Node 12 4.12 + 72
Node 11 4.10 + 67
Node 10 4.9 + 64
Node 8 4.5.3 + 57

Binding. node is v4.13.0/win32-x64-64_binding.node. This includes Node-Sass v4.13.0, Platform Win32, architecture X64, and Node Module version 64. Node Module is a Module of Node whose version can be found in 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' for a: '3', openssl: '1.1.0 j, icu:' 62.1 ', unicode: '11.0', CLDR: '33.1', tz: '2018e'} undefined>
Copy the code

As shown above, the module version for Node10.15.3 is 64. If node-sass is not compatible with Node, the corresponding binding. Node will not be found. If node-sass is incompatible with Node, the corresponding binding.

Cause four: The versions of binding.node in the cache are inconsistent

If the local node version is changed, or the node version is not consistent on different machines, the same error will be reported:

Found bindings for the following environments:
  - Windows 64-bit with Node.js 6.x
Copy the code

This is because the old binding.node cache does not match the current node version. Install NPM Rebuild node-sass or clear the cache as prompted.

Cause five: The installation fails and you need to install it again

In this case, NPM uninstall node-sass or manually delete the original directory and install the node again.

Cause six: A message is displayed indicating that python is not installed or build fails

If the binding.node pull fails, Node-sass will try to compile binding.node locally, requiring Python. If you run into some of the previous cases, you don’t actually have to build locally, so let’s not talk about failure within failure.