Node.js Development

Installing Node.js and NPM on Your Local Machine

Published · Updated 5 min read
Knowledge base article
Contents (24 sections)

Node.js and NPM (Node Package Manager) are essential tools for modern JavaScript development. Node.js enables server-side scripting in JavaScript, while NPM helps manage your project's dependencies. This guide will walk you through installing, updating, and uninstalling Node.js and NPM on Windows, macOS, and Linux.


What Are Node.js and NPM

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript code outside of the browser, making it popular for building scalable server-side applications.

NPM (Node Package Manager) is the default package manager that comes with Node.js, providing: A command-line client for installing and managing packages.
A large repository of public and private JavaScript packages (npm registry).


Prerequisites

Basic command-line interface (CLI) knowledge is helpful.
Familiarity with JavaScript (optional but useful).


Installing Node.js and NPM

Windows Installation

Option A: Direct Download

1 Go to the official Node.js download page.
2 Select either the 64-bit or 32-bit Windows Installer (.msi) based on your system.
3 Run the downloaded file and follow the prompts in the Node.js Setup Wizard.

Option B: Using a Package Manager

Chocolatey (For Admin users)

code
choco install nodejs

Scoop (For Non-Admin users)

code
scoop install nodejs

macOS Installation

Option A: Direct Download

1 Visit the official Node.js download page.
2 Download the macOS Installer (.pkg).
3 Run the .pkg file and follow the installation wizard.

Option B: Using Homebrew

code
brew install node

Linux Installation

Option A: Using Distribution's Package Manager

Debian/Ubuntu

code
sudo apt update && sudo apt install nodejs npm

Fedora/RHEL/CentOS

code
sudo dnf update && sudo dnf install nodejs npm

Option B: Using NodeSource (For Latest Versions)

code
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

(Replace setup_18.x with setup_16.x or setup_20.x as needed.)


Verifying Installation

Check if Node.js and NPM are installed correctly:

code
node -v # Outputs Node.js version\ nnpm -v # Outputs NPM version

Managing Multiple Node.js Versions

Use NVM (Node Version Manager) for macOS/Linux or nvm-windows to manage multiple Node.js versions.

code
nvm install 18 # Installs Node.js v18
nvm use 18 # Switch to Node.js v18

Updating Node.js and NPM

Windows

Node.js - Download and install the latest version from Node.js official site.
NPM - Run:

code
npm install npm@latest -g

macOS

Node.js (via Homebrew):

code
brew update && brew upgrade node

NPM:

code
npm install npm@latest -g

Linux

Debian/Ubuntu:

code
sudo apt update && sudo apt upgrade nodejs
npm install npm@latest -g

Fedora/RHEL/CentOS:

code
sudo dnf update && sudo dnf upgrade nodejs
npm install npm@latest -g

Uninstalling Node.js and NPM

Windows

1 Open Add or Remove Programs in the Control Panel.
2 Locate Node.js and click Uninstall.

macOS

If installed via .pkg file:

code
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*

If installed via Homebrew:

code
brew uninstall node

Linux

Debian/Ubuntu

code
sudo apt remove nodejs

Fedora/RHEL/CentOS

code
sudo dnf remove nodejs

NPM (All distros):

code
npm uninstall npm -g

Advanced NPM (Node Package Manager) Features & Usage

NPM is more than just a package manager--it provides powerful tools for handling dependencies, managing scripts, and optimizing workflows for advanced JavaScript development.

1. Managing Dependencies Efficiently

Semantic Versioning (SemVer): NPM follows SemVer to specify package versions, ensuring compatibility and stability.

  • ^1.2.3 Updates within the same major version.

  • ~1.2.3 Updates within the same minor version.

  • 1.2.3 Installs the exact version.

Installing Development vs. Production Dependencies:

code
npm install lodash --save # Adds to dependencies (default)
npm install eslint --save-dev # Adds to devDependencies
  • dependencies are required for production.

  • devDependencies are only needed for development.

Locking Dependencies with package-lock.json: Ensures consistent installs across environments.

code
npm ci # Installs exact versions from package-lock.json

2. Automating Tasks with NPM Scripts

NPM allows you to define and run custom scripts in package.json:

code
"scripts": {
 "start": "node index.js",
 "dev": "nodemon index.js",
 "build": "webpack --mode production",
 "test": "jest"
}

Run a script with:

code
npm run dev

For predefined scripts (start, test), simply use:

code
npm start
npm test

3. Global vs. Local Packages

Installing Global Packages: Use -g to install tools globally, accessible system-wide:

code
npm install -g nodemon

Check globally installed packages:

code
npm list -g --depth=0

Using npx for Temporary Global Packages: npx runs CLI tools without a global install:

code
npx create-react-app my-app

4. Publishing and Managing Private Packages

Publishing a Package to npm Registry:

code
npm login # Authenticate to npm
npm publish # Upload package

Managing Scoped Packages for Private Projects:

code
npm install @my-org/my-package --save

Private packages require an npm organization or a paid plan.


5. Performance Optimization & Troubleshooting

Speeding Up Installs: Use cache for faster package retrieval:

code
npm cache clean --force
npm install --prefer-offline

Debugging & Diagnosing Issues:

code
npm doctor # Checks system for known issues
npm audit # Scans dependencies for security vulnerabilities
npm outdated # Lists outdated dependencies

Going Beyond: MERN Stack & MongoDB

Once Node.js and NPM are installed, you can explore: MongoDB for database management.
Express.js & React for the MERN (MongoDB, Express, React, Node.js) stack.
Building full-stack applications with modern JavaScript frameworks.

Read more: Advanced MERN Stack Manual


Conclusion

Congratulations! You've successfully installed, updated, and uninstalled Node.js and NPM. Now, you can start building robust JavaScript applications, manage dependencies efficiently, and stay ahead in modern web development.

Tip: Stay updated by checking the official Node.js documentation for more insights and troubleshooting tips.

Happy Coding!

Note: CentOS has reached end-of-life. If you are setting up a new server, we recommend using AlmaLinux or Rocky Linux as a drop-in replacement. The commands and procedures in this article apply equally to AlmaLinux and Rocky Linux.

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
Installing Node.js and NPM on Your Local Machine - Knowledge Base