Article Directory
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.
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
win10 x64
The 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.js
environment 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.js
result of our installation just now , the installation is successful if the version number appears:
node -v
npm -v
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
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\Roaming
now, now let's open this path:
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.js
the installation directory just now . It is convenient to view, and does not occupy C drive resources.
Then we directly Node.js
create the following two folders under the installation directory:
Then open cmd and execute the following code in turn (the specific Node.js
installation 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
Now let's enter the configuration for npm config list
viewing npm
:
If your npm
configuration at this time is as shown in the figure, you have completed the basic npm
configuration. 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.
2.3 Increase the system environment variable NODE_PATH
Since we have just changed the default module storage address to the node_global
directory, running the npm install
installation command directly will report an error. So we also need node_global
to create a new node_module
folder in the directory :
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:
Add path information as shown:
2.4 Test and install express module
If you have no problem with the above steps, you can use the npm install
installation command to install the module at this time , as express
an 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_global
folder under the directory .node_module
express
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
Let's check the version of npm again, you can compare the previous pictures, the version is indeed updated~
npm -v
4. Install cnpm
Enter the following command:
npm install -g cnpm --registry=https://registry.npm.taobao.org
Because it cnpm
will be installed D:\nodejs\node_global
under, and the system variable path
does not include the path. The cnpm
command used at this time will prompt us:
'cnpm' is not an internal or external command, nor is it an executable program or batch file.
We can use it normally by adding the path under the system variable path cnpm
:
Enter the cnpm -v
command again , if the result shown in the figure below appears, it means you are successful! ! !
The next section will demonstrate the use of cnpm
commands to install Vue scaffolding (Vue-cli) and initially build our first Vue componentized project. ❤❤❤