NodeJS installation tutorial
Since we often encounter projects developed based on different NodeJS versions in our daily development, some projects require an older version of NodeJS, while others need a newer version. Therefore, we need to install a NodeJS version management tool.
Below are some recommended tools for reference:
nvm
安装
1.Open the terminal and enter the following command:
macos
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
windows
https://github.com/coreybutler/nvm-windows/releases/download/1.2.2/nvm-setup.exe
2.After installation, close the terminal, reopen it, and enter the following command:
nvm --version
3.If a version number appears, the installation was successful.
Installing NodeJS
- Enter the following command to list all available NodeJS versions:
nvm ls-remote
- Select a version to install, for example, to install NodeJS 18.20.5:
nvm install 18.20.5
- After installation, enter the following command to switch to that version:
nvm use 18.20.5
- Enter the following command to check the current NodeJS version:
node -v
- Enter the following command to view all installed NodeJS versions:
nvm ls
fnm
windows
Install Chocolatey
Open Windows Terminal in administrator mode.
Run the following command:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
- Enter choco -v to test whether the installation was successful.
When using Chocolatey to install software, always open Windows Terminal in administrator mode.
Install fnm
Open Terminal in administrator mode.
Run the following command:
choco install fnm
Test fnm command
Open PowerShell.
Enter fnm -h to test whether the command works properly.
fnm -h
Environment Variable Configuration
PowerShell
- Create a profile.ps1 file in the following directory:
%USERPROFILE%\Documents\WindowsPowerShell\profile.ps1
%USERPROFILE% represents the user directory. You can enter %USERPROFILE% in the file explorer’s address bar and press Enter.
If the WindowsPowerShell folder does not exist, create it. If the command is still not recognized after installing NodeJS, rename the folder to PowerShell.
- Add the following code to the configuration file:
fnm env --use-on-cd | Out-String | Invoke-Expression
cmd
Search for CMD.
Open the file location.
Right-click on “Command Prompt” and select “Properties.”
Modify the target to the following value:
other%windir%\system32\cmd.exe /k %USERPROFILE%\bashrc.cmd
Go to the user directory and create a file named bashrc.cmd.
Add the following code to the configuration file:
@echo off
FOR /f "tokens=\*" %%z IN ('fnm env --use-on-cd') DO CALL %%z
git bash
Go to the user directory and add the following code to the .bash_profile configuration file for Git Bash:
eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH
if [[-f .node-version || -f .nvmrc]]; then
fnm use
fi
VSCode Built-in CMD
Add the following configuration to settings.json:
"terminal.integrated.defaultProfile.windows": "Default Cmd",
"terminal.integrated.profiles.windows": {
"Default Cmd": {
"path": "C:\\Windows\\System32\\cmd.exe",
"args": ["/k", "%USERPROFILE%\\bashrc.cmd"]
}
}
macOS
Install fnm
curl -fsSL https://fnm.vercel.app/install | bash
Set up fnm environment
- Add the following code to .zshrc:
eval "$(fnm env --use-on-cd)"
- Refresh .zshrc:
source ~/.zshrc
Using fnm
Install NodeJS
fnm install 16
fnm install 14
fnm install 12
Use NodeJS
fnm use 16
fnm use 14
fnm use 12
Test NodeJS command
node -v
Set default NodeJS version
fnm default 14 # Sets NodeJS 14 as the default version when opening a terminal.
More fnm commands
fnm -h
fnm -h or visit https://github.com/Schniz/fnm