JavaScriptNode.js Introduction

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.

Bash
$ 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
Top-level await in the REPL
The Node REPL wraps your input in an async context. You can write `await fetch("https://...")` directly without an `async` function.
Running a file

Bash
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

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

JS
// 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

JS
// 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 .js files are parsed.

  • .cjs files are always CommonJS; .mjs files are always ESM.

  • Inside ESM you can import a 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

JS
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"));
node: prefix
Use `node:` prefixes (`import "node:http"`) to make it explicit you mean the built-in, not an npm package with the same name.
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 like console.table and console.dir.

  • global (or globalThis) — the global object. In modern code, prefer globalThis.

  • __dirname and __filename — the directory and full path of the current module. Available in CommonJS only — in ESM use import.meta.url.

  • Buffer — raw binary data buffers (a Node-specific type that predates Uint8Array).

  • setTimeout, setInterval, setImmediate, queueMicrotask — timers.

Reading argv and env

JS
// 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.

Why Node won
A familiar language, non-blocking IO that scales without threads, and a package ecosystem big enough to find almost anything. Twenty years in, Node is still the default server-side JavaScript runtime — and it keeps getting faster with each release.