Skip to content

Node.js vs Browser

The same JavaScript language runs in both Node.js and the browser, but the environments around it are very different. This lesson compares globals, APIs, and module systems so you know exactly what transfers between the two.

Same Language, Different Environments

Both Node.js and browsers execute standard ECMAScript, syntax like let, arrow functions, classes, async/await, and array methods behave identically in both. What differs is the surrounding environment: the browser gives you a DOM, window, and fetch tied to a page; Node.js gives you file system access, process control, and network servers tied to an operating system.

This means code that manipulates the DOM (document.querySelector) has no meaning in Node.js, and code that reads files from disk (fs.readFile) has no meaning in a browser tab, even though both are "just JavaScript."

// Works only in a browser
document.querySelector('button').addEventListener('click', () => {
  console.log('clicked');
});

// Works only in Node.js
import fs from 'node:fs';
const contents = fs.readFileSync('./data.txt', 'utf8');

Neither snippet works in the other environment: browsers have no fs module, and Node.js (without extra libraries) has no document.

Environment-Specific Globals

Browser only:  window, document, localStorage, fetch (built-in)
Node.js only:  process, require/module, Buffer, __dirname
Shared:        console, setTimeout, JSON, Promise, fetch (Node 18+)
  • window and document exist only in browsers, Node.js has no visual UI to represent.
  • process, Buffer, and (in CommonJS) require/module exist only in Node.js.
  • Modern Node.js (18+) includes a global fetch, closing one long-standing gap with browsers.
  • console, timers, JSON, and Promise behave the same in both environments.

Node.js vs Browser Cheatsheet

A side-by-side reference of what's available where.

Feature Browser Node.js
DOM access Yes (document, window) No
File system No (sandboxed) Yes (fs module)
Module system ES modules, <script type="module"> CommonJS and ES modules
Networking fetch, XMLHttpRequest, WebSocket http, net, fetch (18+)
Process control None process.exit, signals, child processes
Global object window globalThis (no window)

There's No DOM in Node.js

A frequent beginner mistake is trying to use document or window inside a Node.js script, expecting server-side rendering to "just work" the way it does in a browser. Node.js has no rendering engine and no DOM by default; if you need DOM-like behavior on the server (for testing or server-side rendering), you install a library like jsdom explicitly.

  • React/Vue SSR frameworks bring their own virtual DOM implementations, not the browser's real DOM.
  • Testing tools like Jest can simulate a DOM using jsdom as an opt-in environment.
  • Plain Node.js scripts have no visual concept at all, they are pure data and I/O.

Module Systems Differ Historically

Browsers standardized on ES modules (import/export with <script type="module">). Node.js started with its own CommonJS system (require/module.exports) years before ES modules existed, and later added ES module support alongside it. Both are covered in depth in the Modules lessons ahead.

Common Mistakes

  • Using document or window in a plain Node.js script and getting a ReferenceError.
  • Assuming localStorage is available in Node.js, it is a browser-only storage API.
  • Forgetting that Node.js's global object is globalThis (or global), not window.
  • Copy-pasting browser fetch/DOM code into a Node.js API route without adjusting for the missing DOM.

Key Takeaways

  • Node.js and browsers run the same JavaScript language but expose very different surrounding APIs.
  • Browsers provide the DOM and window; Node.js provides file system, process, and OS-level APIs.
  • Node.js has no DOM by default, tools like jsdom add DOM-like behavior only when explicitly installed.
  • Modern Node.js includes fetch globally, narrowing (but not eliminating) the gap with browser APIs.

Pro Tip

When you see an error like "document is not defined" or "require is not defined", it's almost always a sign that browser code and Node.js code got mixed in the wrong environment, check which runtime the file is actually meant to execute in.