ReferenceError: structuredClone is not defined (JavaScript) — how to fix
ReferenceError: structuredClone is not defined
at deepCopy (src/utils.js:3:10)
Quick fix: Upgrade Node to 17+ (use a current LTS) and check node -v in CI. In Jest's jsdom environment, add a polyfill in a setup file, e.g. import 'core-js/actual/structured-clone' or @ungap/structured-clone. For plain JSON data, JSON.parse(JSON.stringify(obj)) works as a fallback.
What it means
structuredClone(value) makes a deep copy of objects, arrays, Map, Set, Date, typed arrays and more, and handles circular references. It's a global provided by the runtime, not by the JavaScript language engine alone, so some environments don't have it:
- Node.js before 17.0
- browsers older than roughly 2022 releases (Chrome 98, Firefox 94, Safari 15.4)
- Jest tests running in the
jsdomenvironment - some other test or SSR environments that build their own global object
Check where you are:
node -v
node -e "console.log(typeof structuredClone)" # "function" on Node 17+
Fix 1: upgrade Node
If the error happens when running a script, server or build, upgrade to a current LTS:
nvm install --lts
nvm use --lts
Then check the version used by CI, Docker (FROM node:20 etc.) and your IDE's run configuration, because those often use a different Node than your terminal.
Fix 2: Jest with jsdom
jest-environment-jsdom replaces Node's globals with a browser-like window from jsdom, which doesn't implement structuredClone, so it fails even on Node 20. Add a polyfill in a setup file.
jest.config.js:
module.exports = {
testEnvironment: 'jsdom',
setupFiles: ['<rootDir>/jest.setup.js'],
};
jest.setup.js with core-js:
import 'core-js/actual/structured-clone';
or with @ungap/structured-clone:
import structuredClone from '@ungap/structured-clone';
if (!('structuredClone' in globalThis)) {
globalThis.structuredClone = structuredClone;
}
Install the one you choose with npm install -D core-js or npm install -D @ungap/structured-clone.
If the test doesn't need DOM APIs, switch that file to the Node environment instead with a docblock at the top:
/**
* @jest-environment node
*/
Fix 3: support old browsers
If you ship code to users on older browsers, either polyfill it (core-js via your Babel/bundler setup) or feature-detect:
const clone = typeof structuredClone === 'function'
? structuredClone
: (v) => JSON.parse(JSON.stringify(v));
The JSON fallback only works for plain data: it drops functions and undefined, turns Date into strings, loses Map/Set, and throws on circular references.
Fix 4: lint error 'structuredClone' is not defined
ESLint's no-undef rule reports it when your config doesn't know the global. With flat config, add a recent version of the globals package:
import globals from 'globals';
export default [
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
];
In TypeScript, Cannot find name 'structuredClone' means your lib or @types/node is too old. Update @types/node or include a recent "lib" such as ["ES2022", "DOM"] in tsconfig.json.
Things structuredClone can't copy
Even where it exists, it throws DataCloneError for functions, DOM nodes and some class instances, and it doesn't keep prototypes (class instances become plain objects). For those, write a custom copy.
Last updated 2026-11-29.