Preparation#
Go to the "official download page" to get the latest LTS version, which is currently "Latest LTS Version: 10.14.1". The official website provides installation files for different platforms. Choose "Windows Binary (.zip) 64-bit" and click to download the compressed binary file.
Installation#
After downloading, unzip the file. For convenience, I created a folder "F:\Node.js\dev_tools\node\v10.14.1" to save the extracted Node.js program, and npm is installed by default.
To use Node.js commands in CMD, we need to add "environment variables" next:
# Create an environment variable for the Node.js installation path
NODE_HOME=F:\Node.js\dev_tools\node\v10.14.1
# Add it to the Path
Path={...};%NODE_HOME%;
Since npm is installed by default in Node.js, there is no need for additional configuration to use the "npm" command globally. If you want to use your own installed npm, such as cnpm, you need to add the corresponding environment variable as shown above.
Testing#
In PowerShell, enter node -v
and npm -v
:
PS C:\> node -v
v10.14.1
PS C:\> npm -v
6.4.1
You can see that the current installation versions of node and npm are v10.14.1
and 6.4.1
, respectively.
NPM Configuration#
View current configuration#
Use npm config list
to view the current configuration, or use npm config ls -l
for all configuration information.
Global module directory and cache directory#
Configure the "global module directory" and "cache directory" for npm installation.
Why do we need to configure these two directories?
When executing a global installation command, such as:
npm install express -g
-g
: Optional parameter -g, which stands for global installation.
If you are using the extracted version, the "express" module will be installed in the {extraction directory}\node_modules
directory by default. For example, in my case, it is: F:\Node.js\dev_tools\node\v10.14.1\node_modules
. The npm cache files will be saved in the C:\Users\%USERNAME%\AppData\Roaming\npm-cache
directory. If you are using the installation file directly, both of these folders will be located in the C drive by default, which will take up space on your C drive.
Can we customize these two folders?
Next, let's configure these two directories and specify the "installation directory for global modules" and "cache directory":
# Configure the installation directory for global modules, files will be saved in the node_modules folder
npm config set prefix "F:\Node.js\dev_tools\node\v10.14.1"
# Configure the cache directory
npm config set cache "F:\Node.js\dev_tools\node\v10.14.1\npm-cache"
# Verify the configuration by using the following commands
npm config list
# or
npm config ls -l
Now, if we perform a global installation of the "express" module again, we can see that it is installed in the specified directory.
Our custom configuration will be saved in the file
C:\Users\%USERNAME%\.npmrc
.
Configure NPM registry#
We can specify a mirror source for npm to achieve network acceleration. The default source is https://registry.npmjs.org
, which is slow for accessing from China.
At this point, we can use some excellent npm mirror sources in China, such as:
- CNPM:
https://r.cnpmjs.org/
- Taobao NPM Mirror:
https://registry.npm.taobao.org/
Temporary usage
npm --registry https://registry.npm.taobao.org install express -g
Permanent usage
npm config set registry https://registry.npm.taobao.org
# Verify the configuration by using the following command
npm config get registry
# or
npm info express
Using
cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
# Usage
cnpm install express -g
# If cnpm cannot be used, it may be due to specifying the global module directory for npm, which requires configuring the corresponding system environment. Please refer to the "Installation" section above for more information.
- Note: cnpm also has default configurations that need to be set for the "Global module directory" and "Cache directory" as shown in the "NPM Configuration" section. The custom configurations will be saved in the file
C:\Users\%USERNAME%\.cnpmrc
.