{"record":{"id":"7acef922a6ef2b67","repo":"denoland/deno","slug":"err-invalid-repl-input","errorCode":"ERR_INVALID_REPL_INPUT","errorMessage":"Listeners for `uncaughtException` cannot be used in the REPL","messagePattern":"Listeners for `uncaughtException` cannot be used in the REPL","errorType":"validation","errorClass":"ERR_INVALID_REPL_INPUT","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/repl.ts","lineNumber":184,"sourceCode":"  p.setUncaughtExceptionCaptureCallback(null);\n  if (typeof _prevCaptureCallback === \"function\") {\n    p.setUncaughtExceptionCaptureCallback(_prevCaptureCallback);\n  }\n  _prevCaptureCallback = null;\n}\n\n// Node's REPL refuses user-installed `uncaughtException` listeners so the\n// REPL's own handler isn't shadowed. Mirror that with a counted\n// `newListener` guard installed once and removed when the last non-\n// standalone REPL exits. The REPL's own handler is named\n// `_replUncaughtHandler`, matching the only allowed function name.\nlet _newListenerGuardCount = 0;\nfunction _newListenerGuard(event: string, listener: any) {\n  if (\n    event === \"uncaughtException\" &&\n    listener && listener.name !== \"_replUncaughtHandler\"\n  ) {\n    throw new ERR_INVALID_REPL_INPUT(\n      \"Listeners for `uncaughtException` cannot be used in the REPL\",\n    );\n  }\n}\nfunction _addNewListenerGuard() {\n  if (_newListenerGuardCount++ === 0) {\n    process.prependListener(\"newListener\", _newListenerGuard);\n  }\n}\nfunction _removeNewListenerGuard() {\n  if (--_newListenerGuardCount === 0) {\n    process.removeListener(\"newListener\", _newListenerGuard);\n  }\n}\n\nconst reAtFrame = new SafeRegExp(/^\\s+at\\s/);\nconst reExtFrame = new SafeRegExp(/\\bext:[a-z_]+\\//);\nconst reNodeParenFrame = new SafeRegExp(/\\(node:[a-z_]+:/);","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/repl.ts#L166-L202","documentation":"Node's REPL installs its own uncaughtException handler so an error in evaluated code does not kill the interactive session. To keep that handler from being shadowed, Node (and Deno's node:repl polyfill) reject any additional `uncaughtException` listener registered while a non-standalone REPL is active. The polyfill enforces this with a refcounted `newListener` guard that only accepts functions named `_replUncaughtHandler`, so the offending `process.on()` call throws ERR_INVALID_REPL_INPUT synchronously.","triggerScenarios":"Running `process.on('uncaughtException', handler)` (or addListener/prependListener) inside a `deno` REPL session, or inside code evaluated by a non-standalone `repl.start()` — any listener whose function name is not `_replUncaughtHandler` triggers the throw.","commonSituations":"Pasting a script that installs a global crash handler into the REPL to debug it; `.load`ing a test-setup file that registers uncaughtException listeners; migrating CLI tools that assume they run as plain scripts rather than inside a REPL.","solutions":["Move the `process.on('uncaughtException', ...)` registration out of REPL-evaluated code into the entry-point script","Run the code as a file (`deno run app.ts`) instead of pasting it into the REPL","If you must stay interactive, wrap the risky section in try/catch instead of registering a global handler","As a discouraged last resort, name the handler `_replUncaughtHandler` — it bypasses the guard but shadows the REPL's handler"],"exampleFix":"// before (typed in the REPL)\nprocess.on('uncaughtException', (e) => console.error(e));\n// ERR_INVALID_REPL_INPUT\n\n// after (in the REPL)\ntry { risky(); } catch (e) { console.error(e); }\n// or: put process.on('uncaughtException', ...) in the script and run `deno run app.ts`","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  process.on('uncaughtException', handler);\n} catch (e) {\n  if (e.code === 'ERR_INVALID_REPL_INPUT') {\n    // Running inside a REPL: fall back to local try/catch handling\n  } else {\n    throw e;\n  }\n}","preventionTips":["Register global crash handlers only in entry-point scripts, never in REPL-loaded code","Use try/catch for exploratory error handling in a REPL","Detect REPL context at runtime by catching code ERR_INVALID_REPL_INPUT"],"tags":["repl","process","uncaught-exception","node-compat"],"backgroundTag":"uncaughtexception-listener-in-repl","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}