Node.js: getting started

Running JavaScript outside the browser, the event loop that makes it scale, and the module systems you will meet.

What Node.js is

Node.js runs JavaScript on the server using V8, the same engine as Chrome. Its defining trait is a single-threaded event loop: I/O is delegated and results are delivered via callbacks or promises, so one thread handles many concurrent connections.

node --version
node app.js
node                      # REPL
npm init -y               # create package.json
💡
Node is excellent for I/O-heavy work (APIs, streams, real-time). CPU-heavy work blocks the loop — move it to a worker thread or a separate service.

Modules

// CommonJS (traditional)
const fs = require("fs");
module.exports = { helper };

// ES modules (modern; "type": "module" in package.json or .mjs)
import { readFile } from "node:fs/promises";
export function helper() {}
Built-in moduleFor
node:fsFiles and directories
node:pathCross-platform path handling
node:httpHTTP servers and clients
node:osOperating-system information
node:cryptoHashing, random bytes, ciphers
node:child_processSpawn other programs
⚠️
New code should import the node: prefixed specifiers (node:fs). The prefix makes it explicit that the module is built in, not an installed package with the same name.

Configuration

const port = Number(process.env.PORT ?? 3000);

if (process.env.NODE_ENV !== "production") {
  console.warn("running in development mode");
}

process.exitCode = 1;   // prefer this over process.exit()
  • Read configuration from environment variables, never commit secrets.
  • Add .env and node_modules/ to .gitignore.
  • Pin dependencies and commit the lockfile for reproducible installs.

FAQ

CommonJS or ES modules?
ES modules for new projects; they are the standard and let you use top-level await. Many packages are still CommonJS, and both interoperate.
Is Node faster than Python?
It depends entirely on the workload. Node wins on concurrent I/O; both are fine for typical web APIs. Choose by ecosystem and team, not by microbenchmarks.

Files and paths in Node Building an HTTP server

Last refreshed 2026-09-17.