2

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": "./"
  }
}

2 Answers 2

3

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+.

Sign up to request clarification or add additional context in comments.

Comments

1

I had the same issue and (like robbash said) it was because I was running an old version of node.js. structuredClone() is only available from version 17. I upgraded and it works now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.