[Windows10 system] Introduction to npm (cnpm) + detailed installation tutorial for the latest version

1. npm and cnpm

1.1 Introduction to npm

The full name of npm in English is node package manager. As the name implies, it is the package manager in the Node.js environment. Third-party code resources such as jQuery, express, and Vue are called packages, and npm is mainly responsible for the installation, uninstallation, and dependency management of these packages.

Insert picture description here
The founder of npm is Isaac Z. Schlueter, who graduated from Southern Connecticut State University in 2002. The emergence of npm is groundbreaking, it provides a centralized and unified resource download and management method. Its working principle is simply divided into the following three steps:

  • The official server provided by npm serves as a registry, which contains all the code resources that need to be shared;
  • Authors of various code resources can submit the code to npm's code warehouse (registry);
  • Users can download and install the corresponding code resource module for local use through a simple npm installation command;

1.2 Reasons for recommending cnpm

Because the npm server is in a foreign country, using npm commands to install various packages will be slow and easily affected by network conditions. So our domestic Taobao team made a Chinese version of npm mirror-cnpm. We can use cnpm instead of npm, the installation speed and stability will be better.

2. Installation steps

1. Install Node.js environment

Just download the corresponding version from the official website and install it. Node.js official website download address

Insert picture description here
win10 x64The version I downloaded here is the installation path D:\nodejs. Here you can choose to install it according to your actual situation. (Next installation all the way, remember to change the installation path)

Now that the Node.jsenvironment is integrated npm, no additional installation is required. We can enter the following code in the cmd command prompt window (win + R and then enter cmd) to check the Node.jsresult of our installation just now , the installation is successful if the version number appears:

node -v
npm -v

Insert picture description here

2. Modify npm configuration (local cache address + Taobao mirror station)

Let's take a look at the default configuration at this time. The command to view the npm configuration is:

npm config list

Insert picture description here
You can see that the default warehouse address is the official warehouse address of npm abroad. https://registry.npmjs.org/We will install cnpm in a while, so we will modify the warehouse address to Taobao mirror address later.
The default file local storage address is C:\Users\dell\AppData\Roamingnow, now let's open this path:

Insert picture description here
Here you will see these two files, maybe you don't have the second file, it's because you haven't used it yet npm, it doesn't matter. What we have to do afterwards is to change the default local file storage address from here to Node.jsthe installation directory just now . It is convenient to view, and does not occupy C drive resources.

Then we directly Node.jscreate the following two folders under the installation directory:

Insert picture description here
Then open cmd and execute the following code in turn (the specific Node.jsinstallation path is subject to itself):

npm config set prefix "D:\nodejs\node_global"

npm config set cache "D:\nodejs\node_cache"

npm config set registry=http://registry.npm.taobao.org

Insert picture description here
Now let's enter the configuration for npm config listviewing npm:

Insert picture description here
If your npmconfiguration at this time is as shown in the figure, you have completed the basic npmconfiguration. You can check the Taobao mirror station again:

npm config get registry

If the situation shown in the figure below appears, the mirroring station is feasible.

Insert picture description here

2.3 Increase the system environment variable NODE_PATH

Since we have just changed the default module storage address to the node_globaldirectory, running the npm installinstallation command directly will report an error. So we also need node_globalto create a new node_modulefolder in the directory :

Insert picture description here
Then add the path to the system environment variables, right-click This Computer icon to open the Properties panel, then click in the following order:

Insert picture description here
Add path information as shown:

Insert picture description here

2.4 Test and install express module

If you have no problem with the above steps, you can use the npm installinstallation command to install the module at this time , as expressan example:

npm install express -g

As shown in the figure below, the installation is successful, and you will see the installed folder in the node_globalfolder under the directory .node_moduleexpress

Insert picture description here
Insert picture description here

3. How to update the version of npm

Updating the npm package is also very simple, enter the following command and wait for the execution to complete:

npm install npm -g

Insert picture description here
Let's check the version of npm again, you can compare the previous pictures, the version is indeed updated~

npm -v

Insert picture description here

4. Install cnpm

Enter the following command:

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

Because it cnpmwill be installed D:\nodejs\node_globalunder, and the system variable pathdoes not include the path. The cnpmcommand used at this time will prompt us:
'cnpm' is not an internal or external command, nor is it an executable program or batch file.

Insert picture description here
We can use it normally by adding the path under the system variable path cnpm:

Insert picture description here
Insert picture description here
Enter the cnpm -vcommand again , if the result shown in the figure below appears, it means you are successful! ! !

Insert picture description here


The next section will demonstrate the use of cnpmcommands to install Vue scaffolding (Vue-cli) and initially build our first Vue componentized project. ❤❤❤

Guess you like

Origin blog.csdn.net/JZevin/article/details/108350411