Node.js lets you run JavaScript outside the browser, on servers and on your own computer. This chapter explains what that means, why Node.js was created, and how it grew into one of the most used backend technologies.
In this chapter you'll learn
- what Node.js is (and what it is not)
- the three ideas that make Node.js different: the V8 engine, events, and non-blocking I/O
- why Node.js was created, and how it grew from a side project to the OpenJS Foundation
- what npm is and why it matters so much
Before you start: you only need basic JavaScript (variables, functions, objects).
What is Node.js?
Node.js is a JavaScript runtime. A runtime is everything a program needs to actually run: something that executes the code, plus the tools that code can use (files, network, timers, and so on).
For a long time, JavaScript could only run inside a web browser. The browser gave JavaScript two things:
- a JavaScript engine that executes the code (Chrome's is called V8)
- browser APIs such as
document,window,fetchandsetTimeout
Node.js takes the same V8 engine out of Chrome and gives it a different set of tools. Instead of document and window, you get access to things a server needs: the file system, the network, processes and operating system information.
| JavaScript in the browser | JavaScript in Node.js | |
|---|---|---|
| Engine | V8 (Chrome), SpiderMonkey (Firefox), JavaScriptCore (Safari) | V8 |
| Can access | the web page (document), the browser window | files, network, operating system |
| Global object | window | global (globalThis works in both) |
| Typical use | buttons, forms, animations on a page | web servers, APIs, command-line tools, scripts |
A quick way to see the difference: open a terminal and run node, then type typeof window. Node answers 'undefined', because there is no browser window on a server.
Node.js is not a programming language and not a framework. The language is still JavaScript. Frameworks like Express (Season 2) are built on top of Node.js.
The three big ideas
1. Powered by the V8 engine
V8 is Google's open-source JavaScript engine, written in C++. It is the same engine Chrome uses. V8 is fast because it compiles frequently used JavaScript into machine code, the instructions your CPU understands directly.
You don't need to know V8's internals to use Node.js, but knowing the basics helps you write faster code and answer interview questions. We take a first look in Chapter 02: JS on the Server and go deep in Chapter 08: Deep dive into v8 JS Engine.
2. Event-driven
In an event-driven program, your code says "when this happens, run this function". The function you hand over is called a callback.
// "When the file has been read, run this callback"
import fs from "node:fs"; // load Node's built-in file system module (Chapter 04 explains import)
fs.readFile("notes.txt", "utf-8", (err, data) => {
console.log("File finished reading:", data);
});
console.log("This line runs first, while the file is still being read.");// "When the file has been read, run this callback"
import fs from "node:fs"; // load Node's built-in file system module (Chapter 04 explains import)
fs.readFile("notes.txt", "utf-8", (err, data) => {
console.log("File finished reading:", data);
});
console.log("This line runs first, while the file is still being read.");Think of a restaurant buzzer. You order, get a buzzer, and sit down. You don't stand at the counter blocking everyone else. When the food is ready, the buzzer goes off (the event) and you go pick it up (the callback runs).
The part of Node.js that keeps track of all these "when X happens" requests is called the event loop. It's the heart of Node.js and gets its own chapter: Chapter 09: libuv & event loop.
3. Non-blocking (asynchronous) I/O
I/O means Input/Output: reading a file, querying a database, calling another server. These are slow compared to running code. A disk read can take milliseconds; a network request can take hundreds of milliseconds. Your CPU could run millions of instructions in that time.
- Blocking I/O: the program waits and does nothing until the slow operation finishes.
- Non-blocking I/O: the program starts the slow operation, carries on with other work, and gets notified (through a callback) when the result is ready.
Node.js uses non-blocking I/O by default. That's why one Node.js process can serve thousands of users at the same time, even though your JavaScript runs on a single thread. You'll see this in action with real code in Chapter 07: sync async, setTimeout Zero - Code, and learn who does the waiting (a C library called libuv) in Chapter 06: libuv & async IO.
Why Node.js was created
The problem with servers in 2009
The most popular web server at the time was Apache. In its classic setup, Apache gives each connection its own thread or process. A thread is like a worker who handles one task at a time.
That works well, until many users connect at once:
- While a thread waits for the database or the disk, it sits idle, but still uses memory (often several megabytes each).
- With thousands of connections, you run out of memory for threads long before you run out of CPU.
This was known as the C10K problem: how do you serve 10,000 concurrent connections on one machine?
Ryan Dahl's idea
In 2009, Ryan Dahl built Node.js around a different model: one thread plus an event loop.
- Waiting connections don't need their own thread. The operating system watches them and reports when there is something to do.
- The single JavaScript thread only works when there is actual work, then moves on to the next event.
He first experimented with Mozilla's SpiderMonkey engine, then switched to Google's V8 for its speed. The project was briefly called web.js, then renamed Node.js, because it could do much more than serve web pages.
Modern servers have caught up. Nginx and Apache's newer event mode also use event-driven designs. Node.js's lasting advantage is that you can write the whole backend in JavaScript, with this efficient model built in.
How Node.js grew
Joyent sponsors Node.js (2010)
Joyent, a cloud company, saw the potential, hired Ryan Dahl and sponsored Node.js's development.
npm is born (2010)
npm (Node Package Manager) was created by Isaac Z. Schlueter in January 2010. It lets you download and share code packages with a single command:
npm install expressnpm install expressToday the npm registry is the largest software package registry in the world, with millions of packages. You'll use npm from Season 2 onwards: see Chapter S2 03: Creating our Express server for package.json, node_modules and how installing works.
Node.js on Windows (2011)
At first, Node.js only ran on Linux and macOS. In 2011, Microsoft partnered with Joyent to bring Node.js itself to Windows. To make this possible, the team built libuv, a C library that hides the differences between operating systems. libuv is still the engine room of Node.js, and you'll meet it properly in Chapter 06: libuv & async IO.
New leadership (2012)
Ryan Dahl stepped back from the project in 2012, and Isaac Schlueter became the project lead.
The io.js fork (2014)
Many contributors felt Node.js was moving too slowly under Joyent's control. In December 2014, a group of core contributors (the fork was created by Fedor Indutny) started io.js, a fork: a copy of the project developed separately. io.js shipped faster and used newer versions of V8.
Reunited under a foundation (2015)
In 2015, both sides merged back under a neutral, community-run Node.js Foundation. The first combined release was Node.js 4.0. That's why Node.js versions jump from 0.12 straight to 4.
The OpenJS Foundation (2019)
In 2019, the Node.js Foundation merged with the JS Foundation to form the OpenJS Foundation, which also looks after projects such as Express, jQuery, webpack and Electron.
Node.js today
Node.js releases a new major version every six months. Even-numbered versions (20, 22, 24...) become LTS (Long-Term Support) releases that get fixes for about three years. For real projects, always use an active LTS version. You'll install one in Chapter 03: Let's Write the code.
Common uses for Node.js:
- REST APIs and web backends: what you build in Season 2
- Real-time apps: chat, live notifications, multiplayer games (thanks to WebSockets)
- Command-line tools: npm, ESLint and Vite are themselves Node.js programs
- Build tooling for frontend apps, like React's dev server
Where Node.js is not the best fit: long, CPU-heavy work such as video encoding or large machine-learning jobs. Heavy calculations block the single JavaScript thread. Chapter 10: Thread Pool in libuv explains why, and what to do about it.
Key takeaways
- Node.js is a runtime that runs JavaScript outside the browser, using Chrome's V8 engine.
- Instead of browser APIs, Node.js gives you files, network and operating system access.
- Node.js is event-driven and uses non-blocking I/O, so one thread can handle many connections.
- It was created by Ryan Dahl in 2009 to solve the problem of thread-per-connection servers.
- npm (by Isaac Schlueter, 2010) is the package manager that made the ecosystem explode.
- libuv was built to make Node.js cross-platform, and handles all the asynchronous work.
- Use an LTS version for real projects.
Interview questions
Q1. What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime built on Chrome's V8 engine. It lets you run JavaScript outside the browser, mainly to build servers, APIs and command-line tools. It uses an event-driven, non-blocking I/O model, which makes it efficient for apps that do a lot of waiting on networks, files or databases.
Q2. Is Node.js a language, a framework or a library?
None of those. It's a runtime environment. The language is JavaScript. Frameworks such as Express, NestJS or Fastify are built on top of Node.js.
Q3. Is Node.js single-threaded?
Your JavaScript runs on a single main thread, so only one piece of your code runs at a time. But Node.js as a whole is not single-threaded: libuv keeps a thread pool (4 threads by default) for work like file system access and hashing, and V8 uses background threads for garbage collection. The precise answer: "JavaScript execution is single-threaded; I/O happens in the background." Details in Chapter 10: Thread Pool in libuv.
Q4. What does non-blocking I/O mean?
When Node.js starts an I/O operation (like reading a file), it doesn't wait for it to finish. It registers a callback, continues running other code, and runs the callback once the result is ready. With blocking I/O, the program would sit idle until the operation completed.
Q5. Why is Node.js good for I/O-heavy apps but not for CPU-heavy ones?
I/O-heavy apps (APIs, chat servers) spend most of their time waiting, and Node.js can wait on thousands of things at once without extra threads. CPU-heavy work (image processing, big calculations) keeps the single JavaScript thread busy, so every other request has to wait. For CPU-heavy tasks, use worker threads, a separate service, or a language better suited to it.
Q6. What is the difference between JavaScript in the browser and in Node.js?
Both use a JavaScript engine (Node always uses V8). The browser provides web APIs like document, window and the DOM. Node.js provides system APIs like fs (files), http (servers), process and os. The global object is window in the browser and global in Node.js; globalThis works in both.
Q7. What is npm?
npm is the package manager for Node.js. It's two things: a command-line tool (npm install, npm run) and an online registry of public packages. It records your project's dependencies in package.json, as covered in Chapter S2 03: Creating our Express server.
Q8. What is an LTS version and which one should you use?
LTS stands for Long-Term Support. Even-numbered Node.js releases become LTS and receive bug and security fixes for about three years. For production apps, use the current active LTS version, not the newest "Current" release, which changes more often.
What's next
You now know what Node.js is. In Chapter 02: JS on the Server you'll look at how it runs JavaScript on a server: what a server is, and how V8 turns your code into something the CPU can execute.