Node.js
Install and manage Node.js versions using nvm (Node Version Manager) for JavaScript development
What is Node.js?
Node.js is an asynchronous event-driven JavaScript runtime designed to build scalable network applications.
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications, in contrast to today’s more common concurrency model, in which OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Node.js is free from worries of dead-locking the process, since there are no locks.
Node.js is required for many development tools, including Appium testing for mobile automation testing.
Prerequisites
Install nvm (Node Version Manager)
nvm is a version manager for Node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash) on unix, macOS, and Windows Subsystem for Linux.
Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash
Close your terminal session.
The installation script should automatically add nvm to your ~/.zshrc or ~/.bashrc
config file. If not, add manually:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Verify Your Installation
Open a new terminal session:
nvm -v
# or
nvm --version
Install Node.js
Install the latest LTS version:
nvm install --lts
Install a specific version:
nvm install <version>
Install the latest version:
nvm install node
Install the latest npm release only:
nvm install-latest-npm
Managing Node.js Versions
List installed versions:
nvm ls
List available versions:
nvm ls-remote
Use a specific version:
nvm use <version>
Switch to the latest LTS version:
nvm use --lts
Set default version:
nvm alias default <version>
Create a custom alias:
nvm alias <alias_name> <version>
Delete an alias:
nvm unalias <alias_name>
Find the path to a Node executable:
nvm which <installed_node_version>
Check current Node version:
node -v
# or
node --version
Check current npm version:
npm -v
# or
npm --version
Uninstalling
Determine How Node.js Was Installed
which node
| Output | Installed Via |
|---|---|
~/.nvm/versions/node/... | nvm |
~/.local/share/fnm/... | fnm |
/opt/homebrew/bin/node | Homebrew (Apple Silicon) |
/usr/local/bin/node | Homebrew (Intel) or direct install |
Uninstall Node.js Versions (nvm)
If you get an error:
nvm: Cannot uninstall currently-active node versionwhen uninstalling any currently-active version, runnvm deactivatefirst.
# list installed versions
nvm ls
# uninstall a specific version
nvm uninstall <version>
# uninstall the latest LTS version
nvm uninstall --lts
Uninstall nvm
Remove nvm and all Node.js versions it manages:
rm -rf ~/.nvm
Remove the nvm initialization lines from your shell config file (~/.zshrc,
~/.bashrc, ~/.bash_profile, ~/.profile, or ~/.config/fish/config.fish):
# remove these lines
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Restart your shell or source the edited file for changes to take effect.
Uninstall fnm
fnm stores versions in $FNM_DIR if set, otherwise
$XDG_DATA_HOME/fnm (defaults to ~/.local/share/fnm). Older installs may
use ~/.fnm.
# remove fnm and all managed versions (use your actual path)
rm -rf "${FNM_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/fnm}"
rm -rf ~/.fnm # if it exists (older installs)
# if installed via Homebrew
brew uninstall fnm
Remove fnm initialization from your shell config file (~/.zshrc, ~/.bashrc,
~/.bash_profile, ~/.profile, or ~/.config/fish/config.fish):
# remove this line
eval "$(fnm env)"
Uninstall Homebrew-installed Node.js
brew uninstall node
brew cleanup --prune-prefix
If
brew uninstall nodefails due to dependency errors, usebrew uninstall --ignore-dependencies nodeinstead.
Uninstall Official Installer (.pkg)
If Node.js was installed from the official .pkg installer from nodejs.org:
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/bin/npx
sudo rm -rf /usr/local/include/node
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf /usr/local/lib/dtrace/node.d
sudo rm -rf /usr/local/share/doc/node
sudo rm -rf /usr/local/share/man/man1/node*
sudo rm -rf /usr/local/share/man/man1/npm*
sudo rm -rf /usr/local/share/systemtap/tapset/node.stp
Remove npm and Global Packages
rm -rf ~/.npm
rm -rf ~/.node-gyp
rm -rf ~/.npmrc
rm -rf ~/.node_repl_history
Remove Yarn or pnpm (If Installed)
# yarn
rm -rf ~/.yarn
rm -rf ~/.yarnrc
rm -rf ~/.yarnrc.yml
# pnpm
rm -rf ~/.local/share/pnpm
rm -rf ~/.config/pnpm
Verify Removal
# all should return "command not found"
which node
which npm
which npx
# nvm is a shell function, not a binary
command -v nvm
Good to Know
Alternative: fnm (Fast Node Manager)
fnm is a faster alternative to nvm, written in
Rust. It supports .nvmrc files and is compatible with nvm’s version syntax:
# Install via Homebrew
brew install fnm
# Add to ~/.zshrc
eval "$(fnm env)"
# Usage is similar to nvm
fnm install --lts
fnm use <version>
fnm has significantly faster shell startup times compared to nvm. Consider it if nvm is slowing down your terminal.
Troubleshooting
”nvm: command not found”
-
Verify nvm is installed:
ls ~/.nvm -
Check your shell config has nvm initialization:
grep -A 3 'NVM_DIR' ~/.zshrc -
Re-source your shell:
source ~/.zshrc
“node: command not found” After Installing
Set a default Node version:
nvm alias default <version>
# or
nvm alias default lts/*
Permission Errors with npm
Never use sudo with npm. Fix permissions:
# Create a directory for global packages
mkdir ~/.npm-global
npm config set prefix "$HOME/.npm-global"
# Add to PATH in ~/.zshrc
export PATH="$HOME/.npm-global/bin:$PATH"
nvm Slowing Down Shell Startup
nvm can slow down shell initialization. For faster startup, use lazy loading in
~/.zshrc:
# Lazy load nvm
nvm() {
unset -f nvm node npm npx
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm "$@"
}
Different Node Version in Different Terminals
Use .nvmrc file in your project:
echo "18" > .nvmrc
nvm use
Add to shell config for automatic switching:
# Add to ~/.zshrc
autoload -U add-zsh-hook
load-nvmrc() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "$nvmrc_path")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use --silent
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] \
&& [ "$(nvm version)" != "$(nvm version default)" ]; then
nvm use default --silent
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Resources
Official introduction to Node.js and its architecture
Node Version Manager - manage multiple Node.js versions