Difficulty: Beginner
What is Node.js, how does it differ from JavaScript in the browser, and which global objects does it provide?
Node.js is a JavaScript runtime that runs JavaScript outside the browser, built on Google's V8 engine (which compiles JavaScript to machine code) and libuv (which provides the event loop and async I/O). It adds APIs for files, networking, processes, and cryptography that browsers deliberately do not expose, which is why it is used for web servers, CLIs, build tools, and backend services.
node:fs, node:http, node:crypto, node:path, node:stream, node:worker_threads and others, written in JavaScript and C++.| Aspect | Browser | Node.js |
|---|---|---|
| Global object | window (also globalThis) | global (also globalThis) |
| DOM | document, localStorage | None |
| System access | Sandboxed | Files, sockets, processes, env vars |
| Modules | ES modules | CommonJS and ES modules |
| Main use | UI | Servers, tooling, scripts |
The gap has narrowed. Modern Node ships many web-standard globals: fetch, Request, Response, URL, AbortController, TextEncoder, structuredClone, crypto.randomUUID(), Web Streams, and a WebSocket client (stable in Node 22). Recent versions can also run .ts files directly by stripping type annotations (enabled by default from Node 22.18 and 23.6), though a full type check still needs tsc.
process: the running process. process.env (environment variables, always strings), process.argv (CLI arguments), process.cwd(), process.pid, process.memoryUsage(), process.exitCode, and events such as SIGTERM, uncaughtException, unhandledRejection.Buffer: binary data, used by files, sockets, and crypto.globalThis / global: the global object. Prefer globalThis, which works everywhere.setTimeout, setInterval, setImmediate, queueMicrotask.require, module, exports, __dirname, __filename. These are not true globals; Node wraps each CJS file in a function that passes them in. In ES modules use import.meta.dirname and import.meta.filename (Node 20.11+).Node excels at I/O-bound work: APIs, gateways, chat and notification services, server-side rendering, streaming. Its single JavaScript thread is a poor fit for heavy CPU work like image processing or ML inference unless you move that work to worker_threads, a separate service, or native code. Since Node 15, an unhandled promise rejection crashes the process by default, so always handle rejections. For production, use an even-numbered LTS release.
// node app.js --port 3000
console.log('Node version:', process.version);
console.log('Platform:', process.platform);
console.log('PID:', process.pid);
console.log('CWD:', process.cwd());
console.log('Heap used (MB):', Math.round(process.memoryUsage().heapUsed / 1e6));
// argv[0] = node binary, argv[1] = script path
const args = process.argv.slice(2);
console.log('Args:', args);
// env vars are ALWAYS strings
const port = Number(process.env.PORT ?? 3000);
console.log('Port + 1 =', port + 1);
// CommonJS module-scope variables
console.log('Dir:', __dirname);
// Web-standard globals available in modern Node
console.log(typeof fetch, typeof structuredClone, crypto.randomUUID().length);
console.log(globalThis === global);
process exposes runtime and environment details that browser JavaScript cannot see. Environment variables are always strings, so convert them explicitly.
// Handle uncaught exceptions
process.on('uncaughtException', (err) => {
console.error('Uncaught:', err.message);
// Log, cleanup, then exit
process.exit(1);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection:', reason);
});
// Graceful shutdown on SIGTERM
process.on('SIGTERM', () => {
console.log('SIGTERM received. Shutting down...');
server.close(() => process.exit(0));
});
// Exit event
process.on('exit', (code) => {
console.log('Process exiting with code:', code);
});
Process events let you log crashes and react to signals. After an uncaughtException the process is in an unknown state, so log and exit rather than continuing. Since Node 15, an unhandled rejection crashes the process by default.
V8 Engine, libuv, Runtime, process object, globalThis, Web APIs