preface

I don’t know how you can quickly understand the business of a new project, but my approach is to get it running first. After the operation, there are things to talk about.

Start the first step of the project

npm install
Copy the code

But today, it failed, and I thought I’d just do a quick Google search and be done with it. Instead, I was in a deep, bottomless pit.

Just today, the console reported this error when I was trying to accurately download the dependencies after pulling the company projectThen I reported the error again after switching sources

Then I went to Baidu Google with the error information and found the same number of victims as I did. They also gave many answers, but most of the answers were superficial, neither talking about the cause of the error, nor the principle of the solution.

After reading a lot of articles, I finally found a relatively convincing article on the system to solve the problem of failure to install Node-SAa. I’ll put the link at the end of the article.

I. Is it caused by no switching source?

As we all know, due to the domestic network environment, when we use NPM to download some packages, there will often be slow download or even download failure. Therefore, some experienced programmers will switch the NPM image immediately after installing Node, such as switching to Taobao source.

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

This way we can use NPM to download packages at a much faster speed and avoid download failures.

This is why I initially thought the download failed, so I immediately switched to Taobao source, but it’s not that simple. After switching to Taobao source, the previous error disappeared, but there are new errors.

Switch source yes, wrong is the dependency package

Obviously, the download speed has improved after switching sources, but installation of modules that rely on c++ modules will implicitly install node-gyp, which is used to compile modules that rely on c++ modules.

This doesn’t matter, but node-gyp relies on node’s source code for the first build, so it quietly downloads Node. Although in the front has set up taobao mirror, but there is no egg here. In this case, due to the domestic network environment, the installation was slow or failed again.

Cb? It’s too mind-bending.

See you again in NPM Config. See you in NPM config. See you in NPM config. This makes it easy to install modules that rely on C++ modules.

npm config set disturl https://npm.taobao.org/mirrors/node/
Copy the code

Node-sass is the goblin who grinds people

The first two operations are fine for normal module installations, but there are some modules that, alas, just don’t want to make you feel bad, and one of them is Node-sass, which can’t be solved by the first two operations.

You are most likely to make this mistake if you force the installation

The reason?

When installing Node-sass, a file called binding.node is downloaded from Github during the install phase, and the files in Github Releases are hosted on s3.Amazonaws.com, which is blocked so it can’t be installed.

Is there no way to solve that? Of course not. Why else would I be writing this article?

Sass_binary_site sets the address of the SASS image, which will no doubt point to the domestic Taobao image. This will allow you to install Node-sass again.

npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
Copy the code

Hee hee, the problem is almost solved here.

Why do I say almost?

😭

One of the problems I encountered was that the Node version was not compatible with the Node-sass version

mmp

My final solution was to find the node-sass folder on my computer, General is C: \ Users \ AppData \ Roaming, NPM – cache, node – sass here, and then download the win32 – x64-46 _binding. Node files directly on the problem solved in this folder. But clearly this is not a good solution.

The correct solution is to switch to the correct Node version and re-download the corresponding Node-sass module. The corresponding relationship is shown in the figure

The ultimate solution for lazy people

If you feel that the previous knowledge you do not want to know, also feel unimportant. Ok, it doesn’t matter, you can just do it.

Step 1: Check whether your Node version corresponds to the Node-sass version of package.json in your project.

If yes, go to step 2. If no, switch the node or Node-sass version.

Step 2: Switch the mirror

Use the following command to switch the mirror, I am using taobao mirror

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

You can also use NRM to switch images, see Easter eggs for details

Step 3: Set the ADDRESS of the Sass mirror

npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
Copy the code

This can be done for other packages with the same problem.

npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ npm config set sharp_dist_base_url https://npm.taobao.org/mirrors/sharp-libvips/ npm config set electron_mirror https://npm.taobao.org/mirrors/electron/ npm config set puppeteer_download_host https://npm.taobao.org/mirrors/ npm config set phantomjs_cdnurl https://npm.taobao.org/mirrors/phantomjs/ npm config set sentrycli_cdnurl https://npm.taobao.org/mirrors/sentry-cli/ npm  config set sqlite3_binary_site https://npm.taobao.org/mirrors/sqlite3/ npm config set python_mirror https://npm.taobao.org/mirrors/python/Copy the code

Step 4: Violence

Download win32-x64-46_binding.node directly from the web, then copy and paste the win32-x64-46_binding.node into the folder of Node-sass. Of course, don’t do this unless you have to.

NPM Mirror Management NRM

NPM package has many mirror sources, some sources sometimes fail to access, some sources may not have the latest package, etc., so sometimes it is necessary to switch the source of NPM, NRM package is to solve the problem of fast switching. NRM helps you switch easily and quickly between different NPM source addresses.

NRM has the following built-in sources:

Taobao image or CNPM is recommended

1. Install

npm i -g nrm
Copy the code

2. Check whether the installation is successful.nrm --version

~ nrm --version

1.0.2
Copy the code

3. List the available sources as NRM LS

~ nrm ls

  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
* taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
Copy the code

Note: An asterisk preceded by an asterisk indicates the source being used

4. Switch the source: NRM use NPM

nrm use npm

~ nrm use npm
                        
   Registry has been set to: https://registry.npmjs.org/
Copy the code

5. Add a source: NRM add Registry URL

If you want to add a source, the terminal executes the command NRM add


[home], where reigstry is the source name, URL is the source path, and home is the home page of the source (optional).

~ nrm add company http://npm.company.com/   

    add registry company success
Copy the code

Note:

  1. The/at the end of the URL can be omitted, either of the following urls is acceptable:http://npm.company.com/ http://npm.company.com
  2. The [home] parameter is usednrm homeCommand to view the home page of the source.

6. Delete a source: NRM del Registry

To delete a source, the terminal executes the command NRM del

, where reigstry is the source name.

~ nrm del company

    delete registry company success
Copy the code

Note: The NRM del command cannot delete NRM’s own built-in sources.

7. Test source speed: NRM test

Test the response time of a source: NRM test NPM

~ nrm test npm

* npm ---- 833ms
Copy the code

Test the speed of all sources: NRM test

~ nrm test

* npm ---- 807ms
  cnpm --- 374ms
  taobao - 209ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1056ms
  edunpm - Fetch Error
Copy the code

8. Do not use NRM to switch sources

It is possible to switch sources without NRM, but it is more troublesome

View the source currently in use

npm config get registry

~ npm config get registry
https://registry.npmjs.org/
Copy the code

Setting up a source

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

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

There is no output on the terminal after the configuration is successful.

  • Installation packages use specific sources

    Install all using a specific source:

    npm install --registry=https://registry.npm.taobao.org

    Install a package using a specific source:

    npm i logo --registry=https://registry.npm.taobao.org

NVM Management Node

Use NVM to manage different versions of Node and NPM

Refer to the link

Talk about the dangerous pits of NPM mirroring

NPM Source manager NRM Tutorial