Preparation#
Go to the "official download page" to get the latest LTS version. The current version is Latest LTS Version: 10.14.1
. The official website provides installation files for different platforms. Choose the "Windows Binary (.zip) 64-bit" option and click to download the compressed binary file.
Installation#
After downloading, unzip the file. For convenience, I created a new directory 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 an "environment variable" 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, you can use the
npm
command in global commands without additional configuration. 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
to view 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, where g 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, mine 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 command
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
.npmrc
file located atC:\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 to access in 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 you cannot use cnpm, it may be because you have specified 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 require setting the
global module directory
andcache directory
as in the "NPM Configuration" section. Custom configurations will be saved in the.cnpmrc
file located atC:\Users\%USERNAME%\.cnpmrc
.