Node.js Basics & Runtime

Difficulty: Beginner

Question

What is Node.js, how does it differ from JavaScript in the browser, and which global objects does it provide?

Answer

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.

What Node.js is made of

Node.js vs browser JavaScript

AspectBrowserNode.js
Global objectwindow (also globalThis)global (also globalThis)
DOMdocument, localStorageNone
System accessSandboxedFiles, sockets, processes, env vars
ModulesES modulesCommonJS and ES modules
Main useUIServers, 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.

The global objects you should know

Where Node.js fits and where it does not

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.

Inside the Node.js runtime. Your code calls Node APIs, which sit on V8 for execution and libuv for async I/O.
  1. Your application code: JavaScript or TypeScript, plus npm packages.
  2. Node.js core APIs: fs, http, crypto, stream, process, Buffer.
  3. C++ bindings: Bridge between JavaScript and native libraries.
  4. V8 engine: Compiles and runs JavaScript, manages the heap.
  5. libuv: Event loop, thread pool, cross-platform async I/O.
  6. Operating system: Files, sockets, processes, signals.

Code examples

Key global objects in Node.js

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

Process Event Handling

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

Key points

Common follow-up questions

Interview tips

Concepts covered

V8 Engine, libuv, Runtime, process object, globalThis, Web APIs

More Node.js Interview questions