{"record":{"id":"079b685a6ef10032","repo":"jestjs/jest","slug":"cannot-find-global-gc-function-please-run-node","errorCode":null,"errorMessage":"Cannot find `global.gc` function. Please run node with `--expose-gc` and report this issue in jest repo.","messagePattern":"Cannot find `global\\.gc` function\\. Please run node with `--expose-gc` and report this issue in jest repo\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-core/src/collectHandles.ts","lineNumber":53,"sourceCode":"  }\n\n  return false;\n}\n\nconst alwaysActive = () => true;\n\nconst hasWeakRef = typeof WeakRef === 'function';\n\nconst asyncSleep = promisify(setTimeout);\n\nlet gcFunc: (() => void) | undefined = (globalThis as any).gc;\nfunction runGC() {\n  if (!gcFunc) {\n    v8.setFlagsFromString('--expose-gc');\n    gcFunc = vm.runInNewContext('gc');\n    v8.setFlagsFromString('--no-expose-gc');\n    if (!gcFunc) {\n      throw new Error(\n        'Cannot find `global.gc` function. Please run node with `--expose-gc` and report this issue in jest repo.',\n      );\n    }\n  }\n\n  gcFunc();\n}\n\n// Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js\n// Extracted as we want to format the result ourselves\nexport default function collectHandles(): HandleCollectionResult {\n  const activeHandles = new Map<\n    number,\n    {error: Error; isActive: () => boolean}\n  >();\n  const hook = asyncHooks.createHook({\n    destroy(asyncId) {\n      activeHandles.delete(asyncId);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-core/src/collectHandles.ts#L35-L71","documentation":"Thrown by runGC in collectHandles when, after attempting to enable garbage collection via v8.setFlagsFromString('--expose-gc') and evaluating gc in a new context, the gc function is still undefined. collectHandles uses GC to release weakly-held async resources so it can report which handles keep a test process alive; without GC the open-handles diagnostic cannot run.","triggerScenarios":"Running Jest (specifically the open-handles collection feature) on a Node build or runtime where --expose-gc is ineffective and global.gc is unavailable. This is rare on standard Node builds but can occur on restricted/embedded runtimes.","commonSituations":"Embedded Node distributions that strip v8 flags; sandboxed environments that block setFlagsFromString; unusual Node forks. The message asks users to launch node with --expose-gc and to report the issue upstream.","solutions":["Run node with --expose-gc so global.gc is available from the start (Jest's CLI already does this, but programmatic launches may not)","Disable open-handle collection if not needed (it is best-effort)","Report the environment details to the Jest repo as the message requests, since standard Node builds should not hit this"],"exampleFix":"// before\nnode ./run-jest.js\n\n// after\nnode --expose-gc ./run-jest.js","handlingStrategy":"validation","validationCode":"function assertGcAvailable(): void {\n  if (typeof (globalThis as any).gc !== 'function') {\n    throw new Error('global.gc is not available. Restart with node --expose-gc');\n  }\n}","typeGuard":"function hasGc(): boolean {\n  return typeof (globalThis as { gc?: unknown }).gc === 'function';\n}","tryCatchPattern":null,"preventionTips":["Always launch the Jest CLI with node --expose-gc when relying on open-handle diagnostics","In restricted runtimes, disable open-handle collection if GC cannot be exposed","Pin to a standard Node distribution rather than stripped/embedded builds"],"tags":["core","open-handles","gc","node-flags","diagnostic"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}