Node.js: running JavaScript outside the browser

Node.js runs JavaScript outside the browser, with access to files, network and process: what changes, what stays exactly the same.
3 min read
Believemy logo

For ten years JavaScript could only live inside a tab. Writing a server, a backup script or a command-line tool called for another language.

Node.js removed that border by carrying the browser's engine somewhere else.


Definition

Node.js is a runtime that executes JavaScript outside the browser. It embeds Chrome's V8 engine and adds what a tab is not allowed to do: read files, open a port, launch other programs, read environment variables.

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

const content = await readFile("invoices.json", "utf8");
const invoices = JSON.parse(content);
const total = invoices.reduce((sum, i) => sum + i.amount, 0);

console.log(`${invoices.length} invoices, ${total} euros`);

The node: prefix marks a module supplied by the runtime itself, with nothing to install. The syntax is the everyday one: Array.reduce(), JSON and await behave exactly as they do in a browser. That last point is what makes the environment worth learning quickly, since the whole language transfers and only the surrounding library has to be discovered.


What changes, what stays

Available in Node.jsAvailable in the browser
process, for arguments and environmentwindow, the global object of the page
node:fs, to read and write fileslocalStorage, for limited storage
node:http, to listen on a portdocument and the tree of the page

The language is identical on both sides: Array, Promise, class or Map do not change from one environment to the other. What differs is the set of objects the environment places around them.


One thread, never blocked

Node runs application code on a single thread. It holds up under load because slow operations, disk and network, are handed to the system and delivered back later by the Event loop, rather than waited on in place. The same model explains why many simultaneous connections cost so little here, where one operating system thread per connection would have cost a great deal more.

Good to know

The model has a flip side: a long calculation blocks the entire server, since nobody else holds the thread. Expensive work is handed to a dedicated worker thread, or to another process.


Frequently asked questions

Question

Is Node.js a language?

No, the language is still JavaScript. Node.js is the environment running it, just as the browser is, with its own library of modules. Confusing the two leads people to hunt in the language for functions that in fact belong to the runtime.


Question

Which version should be installed?

Always one carrying an even major number, known as long-term support: it receives security fixes for roughly three years. Odd-numbered releases exist for experimentation and stop being maintained quickly. A version manager lets several live side by side and switch per project.


Question

Is Node right for everything?

It shines on work that waits a lot and computes little: APIs, file servers, command-line tools, network-driven processing. For intensive continuous computation, other runtimes keep the edge. Our Node.js course starts from that divide to build a complete API.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.