Installing Node.js and NPM on Ubuntu
To use NPM, you first need to install Node.js (which includes NPM by default). There are two common methods: using Ubuntu’s official repositories or the NodeSource repository (recommended for the latest stable version).
Method 1: Install from Ubuntu’s Official Repositories
Run the following commands to update your package list and install Node.js with NPM:
sudo apt update
sudo apt install nodejs npm
Method 2: Install from NodeSource Repository (Recommended)
For the latest stable version of Node.js, add the NodeSource repository and install:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - # Replace "16.x" with your desired version (e.g., 18.x)
sudo apt install -y nodejs
After installation, verify success by checking the versions:
node -v # Displays Node.js version (e.g., v16.14.0)
npm -v # Displays NPM version (e.g., 8.5.0)
Basic NPM Commands for Package Management
NPM allows you to install, update, and manage dependencies for your Node.js projects. Below are essential commands:
Initialize a New Project
Create a package.json
file (the manifest for your project) in your project directory:
npm init -y # Uses default values; omit "-y" to customize interactively
Install Local Packages
Install a package (e.g., express
) into your project’s node_modules
folder and add it to package.json
as a dependency:
npm install express # Installs the latest version
npm install express@4.18.2 # Installs a specific version
Use --save-dev
to add the package as a development dependency (e.g., testing tools like jest
):
npm install jest --save-dev
Install Global Packages
Install packages globally (accessible from any directory) for command-line tools (e.g., nodemon
):
sudo npm install -g nodemon # Requires sudo for global installations
Update Packages
Update all installed packages to their latest versions:
npm update
Update a specific package (e.g., express
):
npm update express
Uninstall Packages
Remove a local package from your project and package.json
:
npm uninstall express
Remove a global package:
sudo npm uninstall -g nodemon
Run Scripts
Execute scripts defined in your package.json
(e.g., a start
script to launch your app):
npm run start
Configuring NPM for Better Workflow
Customize NPM’s behavior to suit your needs:
Set a Mirror Registry (Accelerate Downloads)
Switch to a faster mirror (e.g., Taobao) to avoid slow downloads from the official registry:
npm config set registry https://registry.npmmirror.com
Verify the change:
npm config get registry
Upgrade NPM to the Latest Version
Keep NPM up-to-date to access new features and security patches:
npm install -g npm@latest
Fix Global Installation Permissions (Avoid sudo
)
By default, global installations require sudo
, which can cause permission issues. To fix this, change the ownership of the global node_modules
folder:
sudo chown -R $USER /usr/local/lib/node_modules
After running this command, you can install global packages without sudo
.
Optional: Use nvm to Manage Multiple Node.js Versions
If you need to switch between different Node.js versions (e.g., for testing compatibility), use nvm
(Node Version Manager):
Install nvm
Run the following command to install the latest version of nvm
:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc # Load nvm into your current shell session
Install and Switch Node.js Versions
Install a specific version of Node.js (e.g., 14.17.0):
nvm install 14.17.0
Switch to the installed version:
nvm use 14.17.0
Set a default version (used when opening new terminals):
nvm alias default 14.17.0