Original link
author Ping Hai Li
Node sass is a very common dependency package in our development, and it is also the most common dependency for long installation time and error reporting. There are many reasons. Before we talk about the error reporting reasons, let's analyze the installation process of node sass (the following node version is v10.15.3):
PS D:\demo> npm i node-sass > node-sass@4.13.0 install D:\demo\node_modules\node-sass > node scripts/install.js Downloading binary from https://github.com/sass/node-sass/releases/download/v4.13.0/win32-x64-64_binding.node Download 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 > node-sass@4.13.0 postinstall D:\demo\node_modules\node-sass > node scripts/build.js Binary 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 demo@1.0.0 No description npm WARN demo@1.0.0 No repository field. + node-sass@4.13.0 added 174 packages from 138 contributors and audited 529 packages in 24.379s found 0 vulnerabilities
As we can see, there are several steps to install node sass:
- Verify whether node sass is installed in local node modules and whether the versions are consistent;
- If not installed or the version is incorrect, install the node sass ontology from the npm source;
- Check whether there is binding.node in the global cache and local cache, and skip the installation if there is;
- Without binding.node, download the binary file from github and cache it to the global;
- If the downloading of binding.node fails, try to compile the file locally;
- Write the version information to package-lock.json;
It can be seen that node sass actually relies on a binary file, binding.node, which will be downloaded from github after the ontology is installed from the npm source.
Therefore, there are several reasons for the failure of node sass installation:
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 in China. npm source can be set as domestic image source (such as Taobao npm):
npm config set registry https://registry.npm.taobao.org
Or through the. npmrc file:
// .npmrc registry=https://registry.npm.taobao.org/
Reason 2: the binding.node source cannot be accessed or is slow
Node sass can download the binary file binding.node in addition to the part of npm code. The default source is github, which is slow to access in China and even cannot be accessed in special periods. We can also change it to a domestic source:
// linux, mac SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass // Under window set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sass
Or through the. npmrc file:
// .npmrc sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
There are also common dependency packages such as chrome driver, phantom JS, and electron. 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: incompatible node version and node sass version
The compatibility of node sass version is not good. The old project depends on node sass which may not be compatible with the new node. The corresponding version is as follows (or refer to Official warehouse):
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 |
In the example at the beginning of this article, the version of binding.node is v4.13.0/win32-x64-64_binding.node. You can see that 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. Its version number 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', napi: '3', openssl: '1.1.0j', icu: '62.1', unicode: '11.0', cldr: '33.1', tz: '2018e' } undefined >
As shown above, the module version of node10.15.3 is 64.
Therefore, if the version of node sass is not compatible with that of node, the corresponding binding.node will not be found and an error will be reported. At this time, the version of node sass or node can be changed.
Reason 4: the binding.node version in the cache is inconsistent
If the local node version changes, or runs on different machines, and the node versions are inconsistent, a similar error will be reported:
Found bindings for the following environments: - Windows 64-bit with Node.js 6.x
This is because the original binding.node cache is not consistent with the current node version. Follow the prompts NPM rebuild node sass or clear the cache and reinstall.
Reason 5: reinstall after installation failure
If the installation fails, reinstall it. You may not have permission to delete the installed content. At this time, NPM uninstall node sass or manually delete the original directory before installing.
Reason 6: prompt that python is not installed, build fails, etc
If the pull binding.node fails, node sass will try to compile the binding.node locally, which requires python. If you have solved the above situations, it will not happen in the local build. Let's not talk about the failure in this kind of failure.