Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
All Practice Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Install Node.js Create Your First App: Tutorial, Examples, FAQs & Interview Tips

What is Node.js?

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.

Prerequisites

Before installing Node.js, ensure you have:

  • Administrator/sudo privileges on your system
  • Basic command-line knowledge for running terminal commands
  • A code editor - VS Code, Sublime Text, or Atom recommended
  • Basic JavaScript knowledge - understanding of variables, functions, and objects

Installing Node.js on Windows

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.

Verify Windows Installation

Verify Windows Installation
# 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

Installing Node.js on macOS

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 via Homebrew

Install via Homebrew
# 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

Installing Node.js on Linux

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.

Install via apt

Install via apt
# 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

Manual Binary Installation Steps

Manual Binary Installation Steps
# 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

Using nvm (Node Version Manager) - Recommended for Developers

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 and Use nvm

Install and Use nvm
# 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

Your First Node.js Program

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.

Hello World Program

Hello World Program
// 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 Your Program

Run Your Program
# Run the JavaScript file
node app.js

# Output:
# Hello, Node.js!
# Node.js version: v20.11.0
# Platform: linux

Creating a Simple HTTP Server

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".

HTTP Server Example

HTTP Server Example
// 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

Setting Up Your Development Environment

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.

  • Node.js Extension Pack - Collection of essential Node.js extensions
  • ESLint - JavaScript linting and code quality
  • Prettier - Code formatter
  • npm Intellisense - Autocomplete npm modules
  • Path Intellisense - Autocomplete file paths
  • GitLens - Enhanced Git integration

Create package.json

Create package.json
# 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

Understanding package.json

The package.json file is the heart of any Node.js project. It contains metadata about your project and manages dependencies.

Sample package.json

Sample package.json
{
  "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"
  }
}

Common Node.js Commands

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

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.