structuredClone
doesn't work in my NodeJS app. When I try to use it I get the error: structuredClone is not defined nodejs
.
If I create a simple file, and run:
console.log({
globals: Object.keys(global),
structuredClone: global.structuredClone
});
I get:
{
globals: [
'global',
'clearInterval',
'clearTimeout',
'setInterval',
'setTimeout',
'queueMicrotask',
'performance',
'clearImmediate',
'setImmediate'
],
structuredClone: undefined
}
I've updated my node modules as per this question/answers. My package.json
includes:
"dependencies": {
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^2.1.1",
"aws-appsync": "^4.1.7",
"aws-sdk": "^2.1205.0",
"dotenv": "^16.0.2",
"fs": "^0.0.1-security",
"graphql-tag": "^2.12.6",
"node-fetch": "^2.6.7"
},
"devDependencies": {
"@types/jest": "^29.0.0",
"@types/node": "^18.7.15",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint": "^8.23.0",
"jest": "^28.0.1",
"nodemon": "^2.0.19",
"serverless-plugin-typescript": "^2.1.2",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
}
tsconfig.json
:
{
"compilerOptions": {
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"allowJs": true,
"target": "es5",
"outDir": ".build",
"moduleResolution": "node",
"lib": ["es2015"],
"rootDir": "./"
}
}
I don't see why it should be available on the global
object. The way I understand it is that you can use the function straight away:
const obj = structuredClone(anotherObject)
See https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Also, keep in mind that you need to run Node.js 17+.