After this lesson, you will be able to: Distinguish browser JS from Node.js: the global object, APIs available in each, when code transfers between them.
Same language, different runtimes. Knowing what's available where prevents 'works in browser, broken in Node' bugs.
Both runtimes implement the JS language (ECMAScript). They differ in built-in APIs. Browser: DOM, fetch, localStorage, IndexedDB, Web Crypto, postMessage, navigator. Node: fs, path, http, crypto (Node-specific), Buffer, process, child_process. Shared (in modern Node 20+): fetch, AbortController, URL, structuredClone, console.
Same name `globalThis` in both runtimes.
// Modern (works in both)globalThis.foo = 1;// Browser-specificwindow.foo = 1; // same as globalThis.foo in browser// Node-specificglobal.foo = 1; // same as globalThis.foo in Node// Detect runtimeif (typeof window !== "undefined") {// browser} else if (typeof process !== "undefined") {// node}
ESM (modern): `import / export`. Works in browsers natively (with type="module") and in Node 14+ when package.json has `"type": "module"`. CommonJS (Node legacy): `require / module.exports`. Default for older Node packages. Modern projects: ESM everywhere. Some legacy Node libs still need CommonJS interop.
A third class of runtime: V8 isolates without the full Node API. Available: fetch, URL, crypto, Request/Response. NOT available: fs, child_process, most Node-specific APIs. Same code can run in browser + edge with minimal changes. Node-only code (fs, etc.) breaks.
Using `window.location` in code that runs server-side. Crash on SSR. Using `process.env` in browser code. Bundled bundlers inline at build time but raw `process` is undefined. Mixing CommonJS and ESM in one project carelessly. Calling `fs.readFile` in an Edge function. Edge runtimes don't have fs.
Pick the Node-only one.
Sign in and purchase access to unlock this lesson.