Tutorial

How to Install Node.js

2024-12-12 #Linux#macOS#Nodejs#Windows

This article will introduce how to install the Node.js runtime environment on your computer, enabling you to use a wide variety of Node.js applications and tools.

Windows

Testing environment is Windows 11.

Downloading Node.js

Visit the Download Page on the official Node.js website to download the installer.

Choose Prebuilt Installer, select your operating system and CPU architecture (for Windows, usually x64), and then select the Node.js version.

It is recommended to choose an even-numbered LTS (Long Term Support) version.

nodejs-home-page

Installing Node.js

Open the installer node-v22.12.0-x64.msi. The installation process is straightforward: after accepting the license agreement, click Next all the way to complete it.

nodejs-setup

After the installation is complete, open the terminal program:

  • Right-click the Start menu and select Terminal (or Windows Terminal).
  • Or press Windows + R shortcut to open the Run window, type cmd, and press Enter to open it.
  • Or type cmd in the address bar of File Explorer and press Enter (the terminal's working directory will be the current folder path in File Explorer).

Run the following commands to test the installation:

1
node -v
1
npm -v

If the version numbers are successfully printed, the installation was successful.

nodejs-install-done

Common npm Commands

npm is the default package manager that comes with Node.js. It allows you to easily install Node.js applications. Here are some commonly used commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Initialize a project and create a package.json file
npm init

# View version
npm -v

# List current npm configuration
npm config list

# Update npm to the latest version
npm update -g npm

# Install all dependencies for the project (based on package.json)
npm i
npm install

# Install a specific package and add it to project dependencies
npm install <package>

# Install a specific version of a package, e.g., npm i [email protected]
npm install <package>@<version>

# Install a package globally (usually CLI tools)
npm install <package> -g

# Uninstall a specific package
npm uninstall <package>

# Update a specific package to the latest version
npm update <package>

# List installed dependencies in the current project
npm list

# List globally installed dependency packages
npm list -g

# Remove extraneous packages not declared in package.json
npm prune

Using pnpm

If you use Node.js applications frequently, installing the pnpm package manager is highly recommended. It offers better performance, faster installation speeds, and runs in strict mode by default. This ensures consistent dependency versions in your project, avoids hidden version conflicts, and maintains full compatibility with npm.

The lockfile for pnpm is pnpm-lock.yaml.

Installing pnpm

1
npm install -g pnpm

Common Commands

The commands for pnpm are similar to those of npm, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Install dependencies based on pnpm-lock.yaml
pnpm i
pnpm install

# View current pnpm version
pnpm -v

# Install a single dependency (pnpm install also works)
pnpm add <package>

# Install globally
pnpm add -g <package>

# Add specific version
pnpm add <package>

# Remove a package
pnpm remove <package>

# Update pnpm itself
pnpm self-update
pnpm add -g pnpm

Other Operations

Changing Mirror Sources

Mainland China users can use the following command to switch to the Taobao mirror source to speed up downloads:

1
npm config set registry https://registry.npm.taobao.org

View the current mirror source:

1
npm config get registry
Comments
Share

Comments