Node.js Introduction
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) andnode: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):
const name = "Priya";
const city = "Pune";
console.log(`Hello ${name} from ${city}!`);
console.log(`Node.js version: ${process.version}`);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
npmitself, 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.
| Feature | Browser | Node.js |
|---|---|---|
Page and DOM (document, window) | Yes | No |
| Read and write files on disk | No (only with user permission) | Yes, with node:fs |
| Start a web server | No | Yes, with node:http or Express |
fetch(), setTimeout(), URL, console | Yes | Yes |
| Global object | window (or globalThis) | globalThis |
| Who chooses the version? | Each visitor's browser | You, 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:
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");1. Start 2. End of main code 3. Promise callback 4. Timer finished 5. File has been read
Here is what happened:
- The main code ran from top to bottom first, printing lines 1 and 2. Nothing waits in the middle.
- When the main code finished, Node.js ran the promise callbacks (called microtasks). They always run before the event loop moves on.
- The event loop then ran the timer, even though it was set to
0milliseconds. A timer means "not before this time", never "exactly now". - 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.
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`);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
documentorwindowin Node.js. They do not exist, so you getReferenceError: 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.
setTimeout(() => console.log("A"), 0);
console.log("B");
Promise.resolve().then(() => console.log("C"));
console.log("D");Show solution
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?
Node.js is built on V8, the same engine that runs JavaScript in Google Chrome.
2. Which of these is available in Node.js but not in a normal web page?
Node.js can read and write files on disk; it has no DOM, so document and window do not exist.
3. What happens when a request handler runs a loop that takes three seconds?
JavaScript runs on one thread, so long CPU work blocks the event loop for every user.
4. Which Node.js version is the Active LTS release in late 2026?
Node.js 26 "Lithium" became LTS on 28 October 2026; Node.js 24 is the previous LTS.
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
