NPM Install Reports an Error and Freezes, NPM Installation Guide

Question

When using npm install to install a npm dependency package, such as installing vue

npm install vue

I found that the progress bar has been stuck, or a package has been downloaded for a long time. I believe many of my friends have encountered it. The reason should be that the domestic network connection npm speed is relatively slow, and even many dependent packages cannot be downloaded directly. So how to solve this problem?

Option 1: Taobao mirror

The npm Taobao mirror source is a domestic mirror agent station produced by Taobao designed to accelerate the installation of npm. The official description is to synchronize every 10 minutes. Domestic agents also use Taobao to mirror the most, and their services are stable.

The principle of proxy is very simple. Foreign servers are far away from us, and of course it is slower to connect. There are more domestic Taobao servers, and they are closer. Of course, you will be faster.

There are three specific schemes for using Taobao’s proxy mirroring, which will be explained in detail below

  1. First, when you cannot install a single dependency, you can simply add --registry=https://registry.npm.taobao.org after the installation command to switch the npm source of this installation to Taobao's npm mirror station. For example, install vue
npm install vue --registry=https://registry.npm.taobao.org # One-time use of Taobao mirror

The advantage of this solution is that you can operate on individual dependent libraries without installing additional dependencies or modifying the configuration.

  1. The second is the way to switch npm source. Directly switch the source used for npm installation to Taobao npm.taobao.org, so there is no need to enter the suffix every time. Set up
npm config set registry https://registry.npm.taobao.org # Set the npm mirror source to Taobao mirror

After that, install dependencies or normal npm commands.

If you don’t want to use npm.taobao.org as the source later, you can switch back again

npm config set registry http://registry.npmjs.org # Set the npm mirror source to Taobao mirror

npm config get registry # View the current mirror source of npm
  1. The third is to use the cnpm installation method, which is essentially to switch the mirror source. Install cnpm first
npm install -g cnpm --registry=https://registry.npm.taobao.org # The purpose is to install the cnpm library, which is also a one-time use of Taobao mirroring

Then change npm to cnpm when installing the npm package, such as installing vue

cnpm install vue

The advantage of this method is that it supports both cnpm and npm to install npm packages, instead of adding a suffix or switching the source again like the previous two methods.

Note: The Taobao mirror source solution is not a one-time solution, because some friends reported that there is a problem with the dependency library after the Taobao mirror installation is used, but there is no problem with the npm installation. This situation rarely occurs, but once encountered When it arrives, it is a big pit, so friends should pay attention to it.

Option 2: yarn

Yarn is a new-generation alternative to npm to install dependent packages, and better handles the dependency tree. Therefore, for some npm installation failures, it may be just fine to replace it with yarn.

Here is a brief list of the installation methods under each system environment. If you have any questions, you can visit yarn to refer to the detailed tutorial

  1. Install the Yarn global binary to its latest version:
npm install -g yarn
  1. macOS recommends to install directly using brew
brew install yarn
  1. CentOS is also installed by command
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo

sudo yum install yarn

After installation, use the following command to test whether yarn is installed successfully

yarn --version

Then we can use yarn to install the npm package, here is a comparison of some commonly used commands.

npmyarn
npm installyarn add
npm install [package] --saveyarn add [package]
npm install [package] --save-devyarn add [package] --dev
npm install [package] --globalyarn global add [package]
npm update --globalyarn global upgrade
npm rebuildyarn add --force
npm uninstall [package]yarn remove [package]
npm cache cleanyarn cache clean [package]
rm -rf node_modules && npm installyarn upgrade
npm version majoryarn version --major
npm version minoryarn version --minor
npm version patchyarn version --patch

Similarly, as in solution 1, yarn may also need to be used in conjunction with switching the mirror source. You can refer to it as follows

yarn config get registry # View the current mirror source of yarn

yarn config set registry https://registry.npm.taobao.org/ # Set the yarn mirror source to Taobao mirror

In addition, some large third-party libraries are now recommending the use of yarn to install dependent libraries, such as vue, which can be used as an important reference. But it's not absolute, because some friends have reported that there is a problem with yarn, but it is normal to use npm.

Because everyone does different projects, the relationship between npm dependencies is intricate, and it often depends on luck, or you detect bugs that others have not encountered. After all, these low-level tools are also written in code, and it is possible to write code. If there are bugs, just find the most suitable tool for yourself.

Option Three: Dedicated Line Link

If the first two solutions are not very effective, the editor can only recommend one ultimate trick to completely solve various network problems such as npm stuck, slow npm download, npm installation error and other network problems-direct connection to the dedicated line. Why is it a big move, Xiaobian, are you fooling me?

There are two reasons,

  1. First, although we use Taobao mirror source, there are various third-party dependency packages. The strange thing is that some third-party libraries need to temporarily go to other websites to download their own dependencies when downloading. For our own reliance, Taobao does not have a proxy, or even if some URLs can be proxy, so many additional URLs of third-party libraries, you can not be able to help you download the proxy. The most typical dependent libraries are node-sass, node-gyp, etc. (node-sass and node-gyp have more than one pit, this will be a detailed tutorial someday)
  2. The second is that the editor has encountered such pits before. No matter if you use cnpm, change the mirror source, change the yarn, delete all kinds of dependencies and reinstall, it will not work. Finally, a more scientific method can be used to save valuable development time. . After all, all kinds of tools have to serve our work efficiency, otherwise they are not good tools. If we are in order to save money, but spend a lot of time researching the pits without results, wouldn't it be a waste of more money, time is more expensive than money.

Well, it’s not a matter of selling. Friends who understand the benefits of such tools will naturally understand the importance of time and good tools. Please go directly to the professional website recommended by the editor.

Professional Website

The most direct solution for direct connection with a dedicated line is to purchase services --Lu Xun (error)

If you have any questions about this tool, please refer to this interpretation to appreciate the bitter tears of the editor. . .

What weird thing did the editor do?

Reference

Comments