Analyzing the node-sass installation process

 First understand the process of installing node-sass :


  1. npm Pull down the node-sass package;


  2. Pull the corresponding binding.node compiler based on the node version and the node-sass version, due to the fact that sass has a special compilation language and needs to download the corresponding version of the compiler in order to compile; ( node scripts/install.js phase)

  3.  If you can pull down binding.node then [Installation Successful];


    If the corresponding binding.node package is not found, it fails, and then a local compilation is attempted.

 Reasons for possible failure

 I. Network instability


The first thing to know is that when installing node-sass , a .node file is downloaded from github.com at the node scripts/install.js stage. Most unsuccessful installations originate from here because the files in GitHub Releases are hosted at s3.amazonaws.com , which is always “unstable” in China, so we need to download the file from a third-party server.

 method settle an issue

 1. Using Taobao mirrors
npm set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass

 Then re-execute npm i node-sass to complete the installation.

 2. Use of ladders


Assuming your ladder has a third-party server 127.0.0.1:1080 turned on on your local machine, simply configure it as follows to install node-sass properly (if you have PAC mode turned on instead of global mode, you’ll also need to add s3.amazonaws.com to the PAC list):

npm config set proxy http://127.0.0.1:1080
npm i node-sass

npm config delete proxy
 3. Locally specified .node files


For example, when we install node-sass we can see that it requires the download of the version-specific .node file:

$ npm install --save-dev node-sass
> [email protected] install D:\WorkSpace\node-sass-test\node_modules\gulp-sass\node_modules\node-sass
> node scripts/install.js

Downloading binary from http://npm.taobao.org/mirrors/node-sass/v5.0.0/win32-x64-72_binding.node


If your office environment does not have access to an external network, then you can download the .node file (download the version corresponding to node-sass as well as the corresponding operating system) from a computer with Internet access, and then transfer it to an offline computer to specify the binary path to install it, execute the following command to complete the installation:

npm i -D [email protected] --sass_binary_path=D:\files\win32-x64-72_binding.node

 II. Local compilation without Python environment

 Method 1: via NPM package


Network under normal circumstances to install node-sass is not required Python environment, if you can not pull down the corresponding binding.node will enter the try [local compilation], and then will check whether the conditions: need python environment, the reported error will generally prompt the python is not installed (or Mac , comes with python environment does not have these troubles), the installation of the following two packages can be quickly resolved:


Note though: the way to pull the package requires cmd to be opened in administrator mode! ! !

npm install -g node-gyp
npm install --global --production windows-build-tools


If the local network is good, these two packages if installed successfully, node-sass basically can be installed successfully, but if your office environment to limit the network may also be more pitiful, reference: offline installation node-gyp.

 Method 2: Build a Python environment (2.7 recommended)


  • 1. Go to the official website and download www.python.org/download/re… ;

  • 2. After successful installation, add the installation path of python to the system variables in the environment variables;
  •  3. Execute npm rebuild node-sass to refactor it;
  •  4. Re-implementation npm install node-sass


Third, the node-sass version does not match the current Node version


binding.node can’t be pulled down and compiled due to a mismatch between the node version and the node-sass version.


node-sass Version compatibility is not good, the old project depends on node-sass is probably not compatible with the new node version, the corresponding version compatibility reference is as follows (or official repository):

NodeJS Supported node-sass version Node Module
Node 15 5.0+ 88
Node 14 4.14+ 83
Node 13  4.13+, <5.0 79
Node 12 4.12+ 72
Node 11  4.10+, <5.0 67
Node 10 4.9+ 64
Node 8  4.5.3+, <5.0 57
 Node <8  <5.0 <57


For example, if I install [email protected] under the environment of Node 8 , the installation will report an error that the corresponding binding.node cannot be found, as follows:

$ npm install --save-dev node-sass
> [email protected] install D:\WorkSpace\mumble-next-ng\node_modules\gulp-sass\node_modules\node-sass
> node scripts/install.js

Downloading binary from http://npm.taobao.org/mirrors/node-sass/v3.13.1/win32-x64-57_binding.node
Cannot download "http://npm.taobao.org/mirrors/node-sass/v3.13.1/win32-x64-57_binding.node":

HTTP error 404 OK


If you get a 404, you can change the version of node-sass or node .

 Specific reference programs:

npm i [email protected]


Because Node 8 can’t install node-sass 3.x that’s for sure ( Node 7 is for 3.x ), so you have to install node-sass 4.x .


If there are still packages like gulp-sass in the project, and node-sass 3.x is used inside gulp-sass 2.x , there will still be a pitfall, so gulp-sass will have to be replaced with 3.x , and it is recommended to install [email protected] .

 Fourth, did not clear the cache


Remember to npm uninstall xxx clear the cache left by the packet loading error after each failed packet loading error report.


For example, if npm i -D node-sass reports an error, you have to run npm uninstall node-sass again and then reinstall it.

 Common Mirror Source Settings


Due to the domestic network environment or office network environment limitations, load npm package will always encounter a variety of pitfalls, here to share some other common NPM mirror address settings:

$ npm set registry https://registry.npm.taobao.org  
$ npm set disturl https://npm.taobao.org/dist     


$ npm config set proxy http://example.com:8080  
$ npm set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass # node-sass
$ npm i -D [email protected] --sass_binary_path=C:\Users\<username>\Downloads\win32-x64-57_binding.node 

$ npm set chromedriver_cdnurl http://cdn.npm.taobao.org/dist/chromedriver # chromedriver 
$ npm set operadriver_cdnurl http://cdn.npm.taobao.org/dist/operadriver # operadriver 
$ npm set phantomjs_cdnurl http://cdn.npm.taobao.org/dist/phantomjs # phantomjs
$ npm set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents # fsevents 
$ npm set electron_mirror http://cdn.npm.taobao.org/dist/electron/ # electron 

 

By lzz

Leave a Reply

Your email address will not be published. Required fields are marked *