Node.js: running JavaScript outside the browser

Node.js is the environment that runs JavaScript on a machine, with no browser involved. It brings file and network access, and powers every front-end tool.
3 min read
Believemy logo

For years JavaScript could only live inside a tab. The same developer therefore wrote the interface in one language and the server in another.

Node changed that by lifting the engine out of the browser and installing it on an ordinary machine. A server, a backup script, a command line tool: all of it becomes writable in the same language.


Definition

Node.js is a runtime that executes JavaScript outside the browser. It reuses the engine that powers Chrome, adds access to the file system and the network, and ships npm in the same package.

JAVASCRIPT
// info.js
import { readFile } from 'node:fs/promises';

const text = await readFile('./package.json', 'utf8');
const project = JSON.parse(text);

console.log(project.name + " version " + project.version);
// shop version 1.4.0

The file runs through node info.js. The node: prefix names a module supplied by the runtime itself, so there is nothing to install.


What Node adds, and what it removes

Available in NodeMissing from Node
node:fs, reading and writing filesdocument and the whole DOM
node:http, answering requestswindow and its shortcuts
process, arguments and environment variableslocalStorage and cookies

The language itself does not shift an inch: same arrays, same promises, same Event loop. What differs is the library of functions offered around it.

Good to know

Node also runs the tooling of web development, even when the final result is a static page. Vite, ESLint and the TypeScript compiler are Node programs, which is why it sits on every front-end machine.


ES modules or CommonJS

Node understands both systems. A .js file is read as CommonJS by default, and as a ES module as soon as package.json declares "type": "module". The .cjs and .mjs extensions settle the matter file by file.

An ES module loses __dirname and __filename on the way, replaced by import.meta.url. That is the difference that surprises people most during a conversion.


Frequently asked questions

Question

Which Node version should I install?

An even-numbered one, which comes with long term support. On a machine shared between several projects, a version manager such as nvm avoids conflicts, since each project can state its own requirement in the engines field.


Question

Is Node a good fit for heavy computation?

Less so than other environments, because your code runs on a single thread: one long calculation blocks every request waiting behind it. Node shines on work that spends its time waiting for the network or the disk, and genuinely intensive work moves into a Web Worker or a separate process.


Question

How do I restart a script on every change?

The node --watch info.js option is now enough, with nothing to install. Node also ships a test runner and an environment file reader, tools that used to demand one more dependency. Our Node.js course starts from these foundations to build a real 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.