Node Js

Node.js lets you run JavaScript outside the browser, primarily for building back-end services, tools, or full-stack applications.

Node Installation

Check version

node -v

npm -v

nvm -v

Option 1: Install from Official Website

  • Go to: https://nodejs.org
  • You’ll see two options:
    • LTS (Long-Term Support) – Stable, recommended for most users
    • Current – Latest features, but may be less stable.
  • Download and install the LTS version.

This installs both Node.js and npm (Node Package Manager).

NVM allows you to install and manage multiple versions of Node.js.

This will install nvm in your home directory and add it to your shell startup files (.bashrc, .zshrc, etc.)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

# Now reload your shell (for bash):
source ~/.bashrc

# Check if nvm installed correctly:
nvm --version

NVM Commands

Common NVM Commands (for both macOS/Linux and Windows) The commands are largely the same for both NVM implementations.

# Installs a specific version of Node.js.
nvm install <version>

# install a specific patch version
nvm install 20.12.2

# install the latest patch of version 18
nvm install 18

# install the latest stable version of Node.js
nvm install node

# install the latest LTS - Long Term Support - version of Node.js
nvm install --lts

# install the latest version of a specific LTS line, e.g., Node.js 18.x
nvm install lts/hydrogen
# Switches to a specific installed Node.js version.
nvm use <version>

# switches to the latest installed 18.x version
nvm use 20.12.2
nvm use 18

# switches to the latest installed stable version
nvm use node 

# switches to the latest installed LTS version
nvm use --lts

# switches to the Node.js installed by your system, outside of NVM
nvm use system
# Lists all installed Node.js versions and indicates which one is 
nvm ls
nvm list

npm vs npx

Node Package Manager(npm)

npm is the default package manager for Node.js. It installs packages (also called modules/libraries) from the npm registry.

  • Npm is a tool that use to install packages.
  • It is the default package manager for Node.js
  • It gets installed into the system with the installation of node.js.
* Installs packages locally or globally
* Manages dependencies in package.json
* Runs scripts defined in package.json
* Publishes packages to the npm registry
# gives package.json file
npm init -y

# Installs redux and creates node_modules dir
npm install redux

Node Package Execute(npx)

npx is a tool that comes with npm (v5.2.0 and above). It allows you to run Node packages without installing them globally or permanently.

  • Npx is a tool that use to execute packages.
* Run one-time commands from npm packages
* Avoid global installs (which can cause version conflicts)
* Great for CLI tools like create-react-app, nodemon, eslint, etc.

Run a local project tool (in node_modules/.bin):

npx create-react-app my-app

npx eslint index.js

Visual Studio – Extension

  • Prettier
    • format
  • Bracket pair
    • Coloured bracket
  • Matching Tag
    • Show Start and End tag
  • Material icon theme
    • Optional