Introduction to Node.js
Node.js is a JavaScript runtime built on Chrome's V8 engine. It takes the language you already know and adds the abilities a browser intentionally withholds: read and write files, open network sockets, spawn processes, and run for as long as you like. If JavaScript in the browser is "the language of the page", Node is "the language of the machine".
What Node actually is
A wrapper around V8, the same engine inside Chrome. The language is identical.
A standard library (
fs,http,path,os,crypto,stream, ...) for everything the OS can offer.A package ecosystem (npm) with millions of modules.
An event loop powered by
libuv, giving you non-blocking IO without threads.
Browsers expose the DOM. Node exposes the operating system. Both expose all of JavaScript.
The REPL
Run node with no arguments and you get an interactive prompt — handy for one-liners and quick experiments.
$ node Welcome to Node.js v20.10.0. > 1 + 1 2 > const xs = [1, 2, 3].map(n => n * 2) > xs [ 2, 4, 6 ] > .exit
Running a file
node app.js # run a script node --watch app.js # restart on file change node --inspect app.js # enable the debugger node -e 'console.log(2+2)' # evaluate inline
app.js
console.log("running on Node", process.version);running on Node v20.10.0
Modules — CommonJS and ESM
Node supports both module systems. CommonJS (require) is the original; ES modules (import) match browsers and modern tooling.
CommonJS
// math.js
function add(a, b) { return a + b; }
module.exports = { add };
// index.js
const { add } = require("./math.js");
console.log(add(2, 3));ES Modules
// math.mjs (or .js in a package with "type": "module")
export function add(a, b) { return a + b; }
// index.mjs
import { add } from "./math.mjs";
console.log(add(2, 3));A package's
"type"field controls how plain.jsfiles are parsed..cjsfiles are always CommonJS;.mjsfiles are always ESM.Inside ESM you can
importa CJS module; the named exports are detected best-effort.
Built-in modules
Node ships a rich standard library — no install needed. A few you will reach for constantly:
fs/fs/promises— read and write files, watch directories.path— join, resolve and parse filesystem paths.os— CPU count, platform, network interfaces, home directory.http/https— start a server, make a client request.crypto— hashing, random bytes, signing.stream— composable byte and object streams.child_process— spawn external programs.util— promisify, format, deprecation helpers.
A minimal HTTP server
import { createServer } from "node:http";
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("hello from Node\n");
});
server.listen(3000, () => console.log("http://localhost:3000"));Key globals
process— info about the running process:process.argv,process.env,process.exit,process.platform.console— same family as the browser, with extra methods likeconsole.tableandconsole.dir.global(orglobalThis) — the global object. In modern code, preferglobalThis.__dirnameand__filename— the directory and full path of the current module. Available in CommonJS only — in ESM useimport.meta.url.Buffer— raw binary data buffers (a Node-specific type that predatesUint8Array).setTimeout,setInterval,setImmediate,queueMicrotask— timers.
Reading argv and env
// node greet.js Ada Lin console.log(process.argv.slice(2)); // [ "Ada", "Lin" ] console.log(process.env.HOME); // /Users/... console.log(process.platform, process.arch); // "darwin", "arm64"
The event loop, briefly
Node uses the same single-threaded model as the browser. Slow IO is handed off to libuv thread pools or the OS, and your callback runs when the result arrives. You write straight-line JavaScript; the event loop keeps everything responsive.
npm and the ecosystem
Node and npm ship together. The first thing you do in a Node project is npm init and npm install <something>. See the npm Basics page for the full tour.
Beyond Node
Deno — Node's designer Ryan Dahl rebuilt it: ESM-only, TypeScript-native, secure by default.
Bun — a JavaScriptCore-based runtime focused on speed, with a built-in bundler and test runner.
Workers — Cloudflare Workers, Vercel Edge, Deno Deploy — run a JavaScript subset close to the user.