{"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/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-core/src/collectHandles.ts#L35-L71","documentation":"collectHandles uses garbage collection to detect async handles keeping the process alive after a test. runGC first checks for globalThis.gc; if absent it tries to enable it via v8 flags and vm.runInNewContext('gc'). If gc is still unavailable after that, this error is thrown instructing the user to run Node with --expose-gc. It is a last-resort diagnostic for an unsupported or locked-down Node environment.","triggerScenarios":"Running Jest in an environment where --expose-gc cannot be enabled (some sandboxed/embedded Node runtimes, restricted containers), or an unusually locked-down Node build.","commonSituations":"Custom Node distributions without the v8 flag surface; very old or stripped Node builds; security-hardened sandboxes blocking setFlagsFromString.","solutions":["Run Node with --expose-gc: node --expose-gc node_modules/.bin/jest","Upgrade to a standard Node.js distribution","If this persists on a standard Node, report the issue to the Jest repo as the message suggests"],"exampleFix":"# before\njest\n# after\nnode --expose-gc node_modules/.bin/jest","handlingStrategy":"validation","validationCode":"if (typeof (globalThis as any).gc !== 'function') {\n  console.warn('Run node with --expose-gc for open-handles detection');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the jest CLI under node --expose-gc in CI for accurate handle reporting","Use a standard Node.js distribution"],"tags":["node","memory","diagnostics","environment"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}