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.
| Browser | Node.js | |
|---|---|---|
| Global object | window / self | global / globalThis |
| UI / DOM | document, DOM APIs | none |
| File system | no direct access | fs, path, os |
| Networking | fetch, XMLHttpRequest, WebSocket | http, net, dgram (+ fetch in modern Node) |
| Modules | ES Modules (native) | CommonJS and ES Modules |
| Binary data | ArrayBuffer, Blob | Buffer (plus ArrayBuffer) |
| Process info | limited | process (env, argv, cwd, signals) |
| Security model | sandboxed per-origin | full OS access with the user's permissions |
Key implications:
- Code that touches the DOM won't run in Node; code that touches
fs/processwon'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.