Installing Node.js and NPM on Your Local Machine Print

  • 0

๐Ÿš€ 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)

choco install nodejs

๐Ÿฅ„ Scoop (For Non-Admin users)

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

brew install node

๐Ÿง Linux Installation

Option A: Using Distributionโ€™s Package Manager

๐ŸŸข Debian/Ubuntu

sudo apt update && sudo apt install nodejs npm

๐Ÿ”ด Fedora/RHEL/CentOS

sudo dnf update && sudo dnf install nodejs npm

Option B: Using NodeSource (For Latest Versions)

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:

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.

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:

npm install npm@latest -g

๐Ÿ macOS

Node.js (via Homebrew):

brew update && brew upgrade node

NPM:

npm install npm@latest -g

๐Ÿง Linux

Debian/Ubuntu:

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

Fedora/RHEL/CentOS:

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:

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

๐Ÿ“Œ If installed via Homebrew:

brew uninstall node

๐Ÿง Linux

๐ŸŸข Debian/Ubuntu

sudo apt remove nodejs

๐Ÿ”ด Fedora/RHEL/CentOS

sudo dnf remove nodejs

๐Ÿ“Œ NPM (All distros):

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:

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.

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:

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

Run a script with:

npm run dev

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

npm start
npm test

๐Ÿ“ฆ 3. Global vs. Local Packages

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

npm install -g nodemon

Check globally installed packages:

npm list -g --depth=0

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

npx create-react-app my-app

๐ŸŒ 4. Publishing and Managing Private Packages

โœ… Publishing a Package to npm Registry:

npm login   # Authenticate to npm
npm publish # Upload package

โœ… Managing Scoped Packages for Private Projects:

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:

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

โœ… Debugging & Diagnosing Issues:

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! ๐ŸŽฏ


Was this answer helpful?

« Back