How does Node.js differ from running JavaScript in the browser?

2 minbeginnernodejsbrowserglobalsenvironmentdifference

Quick Answer

Same language and engine (V8), different host environment. The browser gives you `window`, the DOM, `fetch`, and sandboxed storage; Node gives you `global`/`process`, file-system and network access, `require`/CommonJS, Buffers, and no DOM.

Detailed Answer

Answer: Both run JavaScript on V8, but the host environment and available APIs differ.

BrowserNode.js
Global objectwindow / selfglobal / globalThis
UI / DOMdocument, DOM APIsnone
File systemno direct accessfs, path, os
Networkingfetch, XMLHttpRequest, WebSockethttp, net, dgram (+ fetch in modern Node)
ModulesES Modules (native)CommonJS and ES Modules
Binary dataArrayBuffer, BlobBuffer (plus ArrayBuffer)
Process infolimitedprocess (env, argv, cwd, signals)
Security modelsandboxed per-originfull OS access with the user's permissions

Key implications:

  • Code that touches the DOM won't run in Node; code that touches fs/process won't run in the browser.
  • Node has no same-origin sandbox — a Node script can read your files and open sockets, so supply-chain security (what you npm install) matters.
  • Isomorphic/universal code (shared between client and server) must avoid environment-specific globals or guard for them.