Node.js Introduction

Node.js · Lesson 1 of 30 8 min read Updated

For many years JavaScript lived only inside web browsers. Node.js changed that. It lets you run JavaScript on your own computer or on a server, so the language you use for buttons and forms can also read files, talk to databases and answer requests from thousands of users.

In this lesson you will learn what Node.js is, what people build with it, how it differs from JavaScript in the browser, and how its event loop works. The event loop is the single most important idea in Node.js, and every later lesson builds on it.

What is Node.js?

Node.js is a JavaScript runtime. A runtime is a program that runs code written in a language. Node.js is made of three main parts:

  • V8, the JavaScript engine from Google Chrome. It turns your JavaScript into fast machine code.
  • libuv, a C library that talks to the operating system for files, network sockets, timers and other slow work.
  • Built-in modules such as node:fs (files), node:http (web servers), node:path (file paths) and node:crypto (hashing and encryption).

Ryan Dahl released Node.js in 2009; today it is free, open source and looked after by the OpenJS Foundation. You write normal JavaScript, and Node.js adds powers that a browser does not allow.

Here is the smallest Node.js program. Save it as hello.js and run it with node hello.js (the next lesson shows how to install Node.js):

JavaScript
const name = "Priya";
const city = "Pune";

console.log(`Hello ${name} from ${city}!`);
console.log(`Node.js version: ${process.version}`);
Output
Hello Priya from Pune!
Node.js version: v26.13.0

The process object only exists in Node.js. It describes the running program: its version, its command-line arguments, its environment variables and more. Your version number may be different.

What can you build with Node.js?

  • Web servers and REST APIs. This is the main use, and most of this course. A REST API is a server that sends and receives data (usually JSON) instead of web pages.
  • Real-time apps such as chat, live cricket scores and delivery tracking, using WebSockets.
  • Command-line tools. Many tools you will use, such as npm itself, are written in JavaScript and run on Node.js.
  • Build tools for front-end code. Vite, ESLint and the TypeScript tooling all run on Node.js.
  • Scripts and automation, such as resizing photos or sending a daily report email.

Node.js compared with browser JavaScript

The language is the same, but the environment is different.

FeatureBrowserNode.js
Page and DOM (document, window)YesNo
Read and write files on diskNo (only with user permission)Yes, with node:fs
Start a web serverNoYes, with node:http or Express
fetch(), setTimeout(), URL, consoleYesYes
Global objectwindow (or globalThis)globalThis
Who chooses the version?Each visitor's browserYou, the developer

The last row matters: on a server you choose the Node.js version, so you can safely use the newest JavaScript features.

The event loop: how one thread serves many users

JavaScript in Node.js runs on a single thread. A thread is one line of work that the processor follows step by step. So how can one thread serve thousands of users at once?

Think of a busy dhaba with one waiter. The waiter takes an order from table 1, passes it to the kitchen and goes straight to table 2. The waiter never stands in the kitchen waiting for the dal to cook. When a dish is ready, the kitchen rings a bell and the waiter carries it out.

In Node.js, your JavaScript is the waiter. Slow work such as reading a file, querying a database or waiting for a network reply is the cooking. Node.js hands that work to the operating system or to a small pool of helper threads inside libuv, and your code keeps running. When the work finishes, a callback (a function to run later) is put in a queue. The event loop is the part of Node.js that keeps checking the queues and running those callbacks one by one whenever your main code is free.

Read this program and guess the order of the lines before you look at the output:

JavaScript
import { readFile } from "node:fs/promises";

console.log("1. Start");

setTimeout(() => console.log("4. Timer finished"), 0);

readFile(import.meta.filename, "utf8").then(() => {
  console.log("5. File has been read");
});

Promise.resolve().then(() => console.log("3. Promise callback"));

console.log("2. End of main code");
Output
1. Start
2. End of main code
3. Promise callback
4. Timer finished
5. File has been read

Here is what happened:

  1. The main code ran from top to bottom first, printing lines 1 and 2. Nothing waits in the middle.
  2. When the main code finished, Node.js ran the promise callbacks (called microtasks). They always run before the event loop moves on.
  3. The event loop then ran the timer, even though it was set to 0 milliseconds. A timer means "not before this time", never "exactly now".
  4. Reading the file from disk took a little longer, so its callback ran last.

This style is called non-blocking I/O. I/O means input and output: disk, network and databases. Because Node.js never sits idle waiting for I/O, a small server can handle a large number of connections.

Blocking the event loop

The waiter idea has a weak spot. If the waiter stops to count every grain of rice, nobody else gets served. In the same way, long calculations in your JavaScript block the single thread, and every other user waits.

JavaScript
function slowSum(limit) {
  let total = 0;
  for (let i = 0; i < limit; i++) total += i;
  return total;
}

setTimeout(() => console.log("Timer wanted to run after 10 ms"), 10);

const started = Date.now();
slowSum(500_000_000);
console.log(`Loop took ${Date.now() - started} ms`);
Output
Loop took 2034 ms
Timer wanted to run after 10 ms

The timer asked for 10 ms, but it had to wait about two seconds (your number will differ) because the loop held the thread. A real server would have frozen for every visitor during that time.

Node.js versions

A new major version of Node.js arrives regularly. Even-numbered versions become LTS (Long Term Support), which means they get bug and security fixes for about two and a half years. Companies run LTS versions in production.

As of late 2026, Node.js 26 (codename "Lithium") is the Active LTS version. It became LTS on 28 October 2026. Node.js 24 ("Krypton") is the previous LTS and still receives fixes. This course teaches Node.js 26 and points out when a feature needs a newer version.

Common mistakes

  • Using document or window in Node.js. They do not exist, so you get ReferenceError: document is not defined. Node.js has no web page.
  • Thinking setTimeout(fn, 0) runs at once. It runs only after the current code and all promise callbacks have finished.
  • Doing heavy calculations on the main thread. They block every user. Keep request handlers quick and move CPU-heavy jobs to worker threads.

Key points

  • Node.js is a JavaScript runtime built on Chrome's V8 engine and the libuv library.
  • It adds server powers the browser does not have: files, networking, processes and more.
  • Your JavaScript runs on one thread; slow I/O is handed off and finished work comes back as callbacks.
  • The event loop runs queued callbacks when the main code is free; promise callbacks run before timers.
  • Long CPU work blocks the event loop, so keep it short or move it to a worker thread.
  • Node.js 26 "Lithium" is the current LTS; Node.js 24 is the previous LTS.

Exercise

Without running it, write down the order in which this program prints its lines. Then explain in one sentence why "A" is printed last even though its line comes first.

JavaScript
setTimeout(() => console.log("A"), 0);
console.log("B");
Promise.resolve().then(() => console.log("C"));
console.log("D");
Show solution
Output
B
D
C
A

The main code runs completely first (B then D), then the promise callback C runs as a microtask, and only then does the event loop run the timer A. Scheduling a timer does not run it; the callback waits in a queue until the thread is free.

Quick Quiz

1. Which JavaScript engine does Node.js use?

2. Which of these is available in Node.js but not in a normal web page?

3. What happens when a request handler runs a loop that takes three seconds?

4. Which Node.js version is the Active LTS release in late 2026?

Learn Node.js with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call