Sub topic 9
Where structuredClone Is Available
This chapter assumes you know how structuredClone copies values, which values it rejects, and how to run code in a browser console or Node.js. It covers which runtimes provide structuredClone as a global, how to check for it before you call it, and what to do when it is missing.
Support across runtimes
structuredClone is a global function. It is defined by the HTML standard rather than the ECMAScript language specification, so each host environment adds it on its own schedule. Any current browser or server runtime has it. Older versions do not.
| Runtime | First version with `structuredClone` |
|---|---|
| Chrome and Edge | 98 |
| Firefox | 94 |
| Safari (macOS and iOS) | 15.4 |
| Node.js | 17.0 |
| Deno | 1.14 |
| Bun | 1.0 and later |
Inside a browser, the function also works in web workers, not only on the main thread. In Node.js, version 18 was the first long-term support release that included it, so any supported Node version has it today.
Common places it goes missing
Most code runs where structuredClone exists. The failures tend to come from these situations:
- Older browsers you still support. If your support list includes Safari 15.3 or earlier, the call throws
ReferenceError: structuredClone is not defined. - Old Node.js versions. Build scripts or serverless functions pinned to Node 16 or earlier do not have it.
- Simulated test environments. Some test setups, especially older jsdom-based environments in Jest, run code in a fake browser global that does not define
structuredClone, even when the Node process underneath does. - TypeScript type errors. The types live in the DOM library and in
@types/node. If yourtsconfig.jsonleaves out"DOM"and your Node types are out of date, the compiler reportsCannot find name 'structuredClone'. The runtime may support it fine.
The test environment case catches people out, because the same code passes in the browser and fails only under test.
Feature detection
Feature detection means checking whether something exists before you use it, rather than guessing from a version number. For structuredClone, use typeof. It does not throw on an undeclared name:
function deepCopy(value) {
if (typeof structuredClone === 'function') {
return structuredClone(value);
}
throw new Error('structuredClone is not available in this runtime');
}
console.log(deepCopy({ when: new Date(0) }).when instanceof Date); // trueWriting if (structuredClone) would throw a ReferenceError in runtimes that lack the function, so use the typeof check.
Fallbacks
When the global is missing, you have three practical options.
- Use V8 serialization in older Node.js. The
v8module uses the same serializer that sits under structured cloning, and it has existed since Node 8. Its behavior is close tostructuredClonebut not identical, so test the types you depend on. - Load a polyfill. Libraries such as
core-jsinclude astructuredClonepolyfill for old browsers. A polyfill reimplements the algorithm in JavaScript, so it is slower and may not handle every host type. - Fail clearly. If an old runtime is not something you support, throw an error with a clear message instead of falling back to a JSON round-trip, which would quietly change your data.
Here is a CommonJS helper that combines the native function with the Node fallback:
function deepCopy(value) {
if (typeof structuredClone === 'function') {
return structuredClone(value);
}
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
const v8 = require('v8');
return v8.deserialize(v8.serialize(value));
}
throw new Error('No structured clone implementation available');
}
const original = { ids: new Set([1, 2]), nested: { n: 1 } };
const copy = deepCopy(original);
copy.nested.n = 99;
console.log(original.nested.n); // 1
console.log(copy.ids.has(2)); // trueExercise
A colleague writes const clone = structuredClone ? structuredClone : null; to detect support. What happens in a runtime without structuredClone, and how should the check be written?
Show answer
Reading the undeclared name throws a ReferenceError before the conditional is evaluated. Use typeof structuredClone === 'function', which returns 'undefined' instead of throwing.
Exercise
Your code calls structuredClone and works in Chrome and in Node 20, but a Jest test fails with "structuredClone is not defined". What is the likely cause?
Show answer
The test runs in a simulated browser environment (such as an older jsdom setup) whose global object does not define structuredClone, even though the underlying Node process has it. Switch the test environment, or provide the function in the test setup.