Node.js and browser JavaScript run the same language, but in different environments. The language (syntax, types, Promise, classes, modules) is identical because both use a V8-based engine, while the available APIs differ: browsers give you the DOM, window and fetch for pages, and Node.js gives you the file system, processes, networking and the process object for servers and tools. Knowing this split is the main idea behind the "nodejs vs browser javascript" question.
The rest of this guide shows what is shared, what is missing on each side, and how to write code that runs in both.
The core language is defined by the ECMAScript standard, and Node.js (built on Google's V8 engine) and Chromium-based browsers share the same engine family. Firefox and Safari use other engines (SpiderMonkey and JavaScriptCore), but they all implement the same language. So all of these work everywhere:
Array, Map, Set, Date, JSON, Math and RegExpPromise, async and awaitimport and exportsetTimeout and setIntervalSave this as numbers.js and run it with node numbers.js, or paste it into the browser developer console. The logic is the same; only where console.log prints differs (your terminal versus the DevTools console).
A browser runs JavaScript inside a web page, so it exposes objects that describe the page and the user's device:
| Browser API | Purpose |
|---|---|
window, document | The page and its DOM tree |
localStorage, sessionStorage | Small key-value storage per origin |
navigator, location, history | Browser and URL information |
Events such as click | Reacting to user input |
alert, prompt | Built-in dialogs |
| Canvas, WebGL, Web Audio | Graphics and sound |
Browser code is also sandboxed. A page cannot read arbitrary files from your disk, start other programs or open raw TCP sockets, and network requests are limited by rules such as CORS (cross-origin resource sharing).
Node.js runs JavaScript on your machine, outside a page, so it exposes system-level modules:
| Node.js API | Purpose |
|---|---|
node:fs | Read and write files |
node:path, node:os | Paths and operating system information |
node:http, node:net | Build servers and sockets |
node:child_process | Start other programs |
process | Arguments, environment variables, exit codes |
node:crypto, node:stream, node:worker_threads | Hashing, streams and threads |
This works in Node.js (in an ES module file, for example app.mjs) and fails in a browser, because browsers have no node:fs. The node: prefix is the recommended way to import built-in modules because it makes clear they are not packages from npm.
Each environment has a top-level global object, but the traditional names differ.
| Name | Browser | Node.js |
|---|---|---|
window | Yes | No |
document | Yes | No |
global | No | Yes (older name) |
globalThis | Yes | Yes |
Use globalThis when you need the global object in code that must run in both. Also note a scope difference: a top-level var in a classic browser script becomes a property of window, but in Node.js a top-level variable in a file stays local to that module and does not become global.
Modern Node.js has adopted many web-standard APIs, so the gap is smaller than it used to be. In current Node.js releases you can use fetch, URL, URLSearchParams, AbortController, TextEncoder, structuredClone, setTimeout, Blob and the Web Streams API without installing anything. Check the Node.js documentation for the exact status of each API in your version.
The same snippet works in a browser console and in Node.js (as an ES module, where top-level await is allowed). In the browser it is still subject to CORS, while in Node.js it is not, because there is no page origin.
Browsers load scripts with <script> tags, and with type="module" they load ES modules directly. Node.js supports two module systems: CommonJS (require, module.exports) and ES modules (import, export). A file is treated as an ES module in Node.js when it ends in .mjs or when the nearest package.json has "type": "module".
Other practical differences:
node_modules folder; browsers load code from URLs or from a bundle you build with a tool such as Vite.Not by default, since there is no page. Libraries such as jsdom emulate a DOM for testing or scraping, but they are an emulation, not a real browser.
No. Browsers do not provide require. Use import with ES modules, or a bundler that converts CommonJS for the browser.
No. Node.js is a runtime that runs standard JavaScript with extra system APIs. Only the surrounding APIs differ.
Stick to the standard language and web-standard APIs such as fetch and URL, use globalThis, and keep environment-specific code (DOM or node:fs) in separate modules.
Node.js and browser JavaScript share the same language and many web-standard APIs, but each adds its own environment: the DOM and sandboxed page APIs in the browser, and the file system, processes and networking in Node.js. Use globalThis for the shared global object, node:-prefixed imports for Node.js built-ins, and keep environment-specific code isolated when you want to reuse logic across both.