Sub topic 1
Engines, Runtimes, and Host Environments
When people say "JavaScript runs in the browser" or "JavaScript runs on Node.js," they are talking about two layers. The first layer is the engine, which understands the language. The second is the host, which connects the language to the outside world. Keeping the two apart explains most of the differences you will see in this tutorial.
Prerequisites
This chapter assumes you know:
- Basic JavaScript syntax: variables, functions, objects, and arrays
- Promises or callbacks, at a level where you can read code that uses them
- Basic HTML structure
- How to run commands in a terminal
The engine: the language itself
A JavaScript engine is a program that parses, compiles, and runs JavaScript as defined by the ECMAScript specification. The engine provides the core language: syntax, Array, Object, JSON, Math, Promise, Map, and similar built-ins. It manages memory with a garbage collector and uses a just-in-time compiler to make hot code fast.
An engine on its own cannot read files, draw on a screen, or open a network connection. ECMAScript defines no input or output at all.
| Engine | Used by |
|---|---|
| V8 | Chrome, Edge, Node.js, Deno |
| SpiderMonkey | Firefox |
| JavaScriptCore | Safari, Bun |
The runtime: engine plus host APIs
A runtime is an engine embedded in a host program. The host adds APIs, which are objects and functions the engine does not define, and an event loop that decides when your code runs. The browser adds the DOM, fetch, localStorage, and setTimeout. Node.js adds process, the fs and http modules, and its own version of setTimeout.
Chrome and Node.js share V8, so the language behaves the same in both. The global objects around it are different.
Seeing the split
Save this as globals.js. Run it with node globals.js, then paste it into a browser's DevTools console.
const names = [
'Array', 'JSON', 'Promise', 'Math', // engine (ECMAScript)
'setTimeout', 'console', 'fetch', // host, provided by both
'document', 'process', // host, provided by one only
];
for (const name of names) {
console.log(name.padEnd(12), typeof globalThis[name]);
}The first four names report function or object in both places, because the engine defines them. document is undefined in Node.js and process is undefined in a browser page. setTimeout, console, and fetch (in Node.js 18 and later) exist in both, but each host implements them separately. In the browser, setTimeout returns a number. In Node.js it returns a Timeout object.
Common errors
Most environment errors happen when code assumes a host API that the current runtime does not provide.
ReferenceError: document is not definedhappens when browser code runs in Node.js, for example in a test or during server-side rendering.ReferenceError: require is not definedhappens when Node.js CommonJS code is loaded directly in a browser.- Some developers assume
setTimeoutandfetchare part of the language. They are host APIs that happen to be widespread, and their details can differ. - New syntax needs a newer engine. Optional chaining (
?.), for example, fails with aSyntaxErroron an old engine whatever host it runs in.
When you hit one of these errors, ask which layer the missing feature belongs to. If the engine lacks it, you need a newer engine version or a transpiler. If the host lacks it, you need a different API, or you need to detect the environment first. A later chapter covers environment detection.
Exercise
A script calls JSON.parse, fs.readFileSync, and document.querySelector. For each one, say whether the engine or the host provides it, and in which runtime the script could call it.
Show answer
JSON.parse comes from the engine (ECMAScript), so it works in both runtimes. fs.readFileSync comes from the Node.js host, so it works only in Node.js. document.querySelector comes from the browser host's DOM, so it works only in a browser page.
Exercise
Chrome and Node.js both use V8. Why does code that runs in Chrome sometimes throw a ReferenceError in Node.js?
Show answer
V8 provides only the ECMAScript language. Globals such as window, document, and localStorage come from the browser host. Node.js does not provide them, so referring to them there throws a ReferenceError.