Installing Node.js and NPM on UbuntuTo 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 RepositoriesRun the following commands to update your package list and install Node.js with NPM:
sudo apt updatesudo apt install nodejs npmMethod 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 nodejsAfter 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 ManagementNPM allows you to install, update, and manage dependencies for your Node.js projects. Below are essential commands:
Initialize a New ProjectCreate a package.json file (the manifest for your project) in your project directory:
npm init -y# Uses default values; omit "-y" to customize interactivelyInstall Local PackagesInstall 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 versionnpm install [email protected] # Installs a specific versionUse --save-dev to add the package as a development dependency (e.g., testing tools like jest):
npm install jest --save-devInstall Global PackagesInstall packages globally (accessible from any directory) for command-line tools (e.g., nodemon):
sudo npm install -g nodemon# Requires sudo for global installationsUpdate PackagesUpdate all installed packages to their latest versions:
npm updateUpdate a specific package (e.g., express):
npm update expressUninstall PackagesRemove a local package from your project and package.json:
npm uninstall expressRemove a global package:
sudo npm uninstall -g nodemonRun ScriptsExecute scripts defined in your package.json (e.g., a start script to launch your app):
npm run startConfiguring NPM for Better WorkflowCustomize 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.comVerify the change:
npm config get registryUpgrade NPM to the Latest VersionKeep NPM up-to-date to access new features and security patches:
npm install -g npm@latestFix 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_modulesAfter running this command, you can install global packages without sudo.
Optional: Use nvm to Manage Multiple Node.js VersionsIf you need to switch between different Node.js versions (e.g., for testing compatibility), use nvm (Node Version Manager):
Install nvmRun the following command to install the latest version of nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashsource ~/.bashrc# Load nvm into your current shell sessionInstall and Switch Node.js VersionsInstall a specific version of Node.js (e.g., 14.17.0):
nvm install 14.17.0Switch to the installed version:
nvm use 14.17.0Set a default version (used when opening new terminals):
nvm alias default 14.17.0