Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. Built on Chrome's V8 JavaScript engine, Node.js enables developers to use JavaScript for server-side scripting, creating dynamic web page content before the page is sent to the user's browser. This allows for a unified JavaScript development stack for both client-side and server-side applications.
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. It's widely used for building web servers, REST APIs, real-time chat applications, microservices, and command-line tools.
Before installing Node.js, ensure you have:
Windows users can install Node.js using the official installer from nodejs.org. The installer includes both Node.js and npm (Node Package Manager).
Visit nodejs.org and download the LTS (Long Term Support) version for Windows. The LTS version is recommended for most users as it receives long-term support and is more stable.
Double-click the downloaded .msi file and follow the installation wizard. Accept the license agreement, choose the installation directory (default is C:\Program Files\nodejs\), and ensure "Add to PATH" is checked.
# Check Node.js version
node --version
# Output: v20.11.0 (or your installed version)
# Check npm version
npm --version
# Output: 10.2.4 (or your installed version)
# Test Node.js REPL (interactive shell)
node
> console.log('Hello, Node.js!')
Hello, Node.js!
> .exit
macOS users have multiple installation options: the official installer, Homebrew, or nvm (Node Version Manager).
Download the .pkg installer from nodejs.org and run it. Follow the installation wizard similar to Windows.
# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Node.js
brew install node
# Verify installation
node --version
npm --version
Linux users can install Node.js using package managers (apt, yum, dnf) or by downloading the binary package. Below are instructions for Ubuntu/Debian and manual binary installation.
Download the latest version of Node.js Linux Binary (x86/x64) from the official website of Node.js.
# Update package index
sudo apt update
# Install Node.js and npm
sudo apt install nodejs npm
# Verify installation
node --version
npm --version
# For latest LTS version, use NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
# Step 1: Download Node.js binary (replace version as needed)
wget https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.xz
# Step 2: Extract the tar file
tar xf node-v20.11.0-linux-x64.tar.xz
# Step 3: Create directory and move Node.js binary
sudo mkdir -p /usr/local/nodejs
sudo mv node-v20.11.0-linux-x64/* /usr/local/nodejs/
# Step 4: Add Node.js to PATH (temporary)
export PATH="$PATH:/usr/local/nodejs/bin"
# Step 5: Make PATH permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export PATH="$PATH:/usr/local/nodejs/bin"' >> ~/.bashrc
source ~/.bashrc
# Verify installation
node --version
npm --version
nvm allows you to install and switch between multiple Node.js versions easily. This is especially useful when working on different projects that require different Node.js versions.
# Install nvm (macOS/Linux)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Reload shell configuration
source ~/.bashrc # or ~/.zshrc for Zsh
# Install latest LTS version
nvm install --lts
# Install specific version
nvm install 20.11.0
# List installed versions
nvm list
# Switch to a specific version
nvm use 20.11.0
# Set default version
nvm alias default 20.11.0
# Check current version
nvm current
Once Node.js is installed, you can create your first program. Node.js can execute JavaScript files and also provides a REPL (Read-Eval-Print Loop) for interactive coding.
// app.js - Your first Node.js program
console.log('Hello, Node.js!');
console.log('Node.js version:', process.version);
console.log('Platform:', process.platform);
// Run this file with: node app.js
# Run the JavaScript file
node app.js
# Output:
# Hello, Node.js!
# Node.js version: v20.11.0
# Platform: linux
One of the most common uses of Node.js is creating web servers. Here's a simple HTTP server that responds to all requests with "Hello World".
// server.js - Simple HTTP server
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World from Node.js!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
// Run with: node server.js
// Visit: http://localhost:3000
A proper development environment makes Node.js development more efficient and enjoyable. Here are the recommended tools and setup.
VS Code is the most popular editor for Node.js development. Download it from code.visualstudio.com.
# Create a new project directory
mkdir my-node-project
cd my-node-project
# Initialize package.json (interactive)
npm init
# Or use defaults (skip questions)
npm init -y
# Install a package (example: express)
npm install express
# Install dev dependency
npm install --save-dev nodemon
# Run scripts defined in package.json
npm run start
The package.json file is the heart of any Node.js project. It contains metadata about your project and manages dependencies.
{
"name": "my-node-project",
"version": "1.0.0",
"description": "My first Node.js project",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["nodejs", "tutorial"],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
| Command | Description |
|---|---|
| node file.js | Execute a JavaScript file |
| node | Start Node.js REPL (interactive shell) |
| node --version | Check Node.js version |
| npm init | Initialize a new Node.js project |
| npm install package | Install a package |
| npm install | Install all dependencies from package.json |
| npm uninstall package | Remove a package |
| npm update | Update all packages |
| npm run script | Run a script defined in package.json |
| npm list | List installed packages |
Explore 500+ free tutorials across 20+ languages and frameworks.