Node.js vs Browser
Node.js and the browser both run JavaScript, often with the same V8 engine, so the language is identical: the same syntax, the same Array, Map, Promise and JSON. What differs is the environment around the language — which globals exist, what the code is allowed to touch, and how modules are loaded.
Side by side
| Browser | Node.js | |
|---|---|---|
| Purpose | draw and run a web page | run programs, servers and tools |
| Global object | window (also globalThis) |
global (also globalThis) |
| Page access | document, DOM, CSS |
none |
| File system | no (sandboxed) | full access via node:fs |
| Network | fetch, WebSocket (same-origin rules) |
fetch, http, raw TCP/UDP sockets |
| Processes | no | process, child_process, worker_threads |
| Modules | <script type="module">, ES modules |
CommonJS and ES modules |
| JS version | whatever the user's browser supports | you choose the Node version |
| Security | untrusted code, strict sandbox | trusted code with your user's permissions |
Globals you can rely on in both
A lot of web APIs are now available in Node too, which makes sharing code easy.
const shared = [
"console", "setTimeout", "fetch", "URL", "TextEncoder",
"structuredClone", "AbortController", "crypto",
];
for (const name of shared) {
console.log(name.padEnd(16), typeof globalThis[name]);
}console object
setTimeout function
fetch function
URL function
TextEncoder function
structuredClone function
AbortController function
crypto object
Globals that only exist in one place
const browserOnly = ["window", "document", "localStorage", "alert"];
const nodeOnly = ["process", "Buffer", "global"];
console.log("Browser-only names in Node:");
for (const n of browserOnly) console.log(" ", n, "->", typeof globalThis[n]);
console.log("Node-only names in Node:");
for (const n of nodeOnly) console.log(" ", n, "->", typeof globalThis[n]);
console.log("require works here:", typeof require("node:path").join);Browser-only names in Node:
window -> undefined
document -> undefined
localStorage -> undefined
alert -> undefined
Node-only names in Node:
process -> object
Buffer -> function
global -> object
require works here: function
This file is CommonJS, so require works — but it is not a property of globalThis. In CommonJS files it is a variable that Node injects into each module, and in ES modules it does not exist at all (you use import there).
Note: Node 22+ has an experimental
localStoragebehind a flag, but you should not rely on it. Use files or a database instead.
Writing code that runs in both
If a module only uses the language and shared web APIs, it can run in the browser and in Node unchanged. When you need to branch, detect the environment by feature, not by guessing:
function describeEnvironment() {
if (typeof process !== "undefined" && process.versions?.node) {
return "Node.js";
}
if (typeof window !== "undefined" && typeof document !== "undefined") {
return "browser";
}
return "something else (Deno, Bun, a worker...)";
}
console.log("This code is running in:", describeEnvironment());This code is running in: Node.js
The biggest mindset change: you own the machine
In the browser your code is a guest: it cannot read the user's files or open arbitrary connections. In Node your script runs with the same permissions as the user who started it. That power comes with responsibility:
- Never
evalor execute input you received from a request. - Validate file paths so a user cannot read
../../etc/passwd. - Keep secrets (API keys, passwords) in environment variables, not in code.
- Install only packages you trust — they run with your permissions too.
Tip: Node 22+ includes an experimental permission model. Running
node --permission --allow-fs-read=./data app.jsblocks the script from reading anything outside./data.
Test yourself
Which object is available in Node.js but not in a normal web page?
fetchprocessURLconsole
What name refers to the global object in both Node and the browser?
windowglobalglobalThisself
Why must Node code be more careful with user input than browser code?
- Node is slower
- Node code runs with the user's full system permissions
- Node cannot use strings
- Browsers validate input automatically