{"record":{"id":"dd462b0130e0b169","repo":"denoland/deno","slug":"err-invalid-this-dd462b","errorCode":"ERR_INVALID_THIS","errorMessage":"Value of \"this\" must be of type EventEmitterReferencingAsyncResource","messagePattern":"Value of \"this\" must be of type EventEmitterReferencingAsyncResource","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_events.mjs","lineNumber":1244,"sourceCode":"const kAsyncResource = Symbol(\"kAsyncResource\");\nconst kEventEmitter = Symbol(\"kEventEmitter\");\n\n// EventEmitterAsyncResource and its helper class are defined lazily to avoid\n// eagerly loading node:async_hooks (which provides AsyncResource).\nlet _EventEmitterAsyncResource;\nfunction getEventEmitterAsyncResource() {\n  if (_EventEmitterAsyncResource) return _EventEmitterAsyncResource;\n  const { AsyncResource } = lazyAsyncHooks();\n\n  class EventEmitterReferencingAsyncResource extends AsyncResource {\n    constructor(ee, type, options) {\n      super(type, options);\n      this[kEventEmitter] = ee;\n    }\n\n    get eventEmitter() {\n      if (this[kEventEmitter] === undefined) {\n        throw new ERR_INVALID_THIS(\"EventEmitterReferencingAsyncResource\");\n      }\n      return this[kEventEmitter];\n    }\n  }\n\n  _EventEmitterAsyncResource = class EventEmitterAsyncResource\n    extends EventEmitter {\n    constructor(options = undefined) {\n      let name;\n      if (typeof options === \"string\") {\n        name = options;\n        options = undefined;\n      } else {\n        if (new.target === _EventEmitterAsyncResource) {\n          validateString(options?.name, \"options.name\");\n        }\n        name = options?.name || new.target.name;\n      }","sourceCodeStart":1226,"sourceCodeEnd":1262,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/_events.mjs#L1226-L1262","documentation":"The internal class EventEmitterReferencingAsyncResource (built lazily by getEventEmitterAsyncResource) stores the emitter in the kEventEmitter symbol property set by its constructor; its eventEmitter getter throws ERR_INVALID_THIS at _events.mjs:1244 when that property is undefined. That only happens when the getter runs with a receiver that never executed the constructor - i.e. the getter was detached from a real instance and invoked with the wrong 'this'.","triggerScenarios":"Extracting the getter and calling it bare or via .call(): const { eventEmitter } = asyncResource; or Object.getOwnPropertyDescriptor(proto, 'eventEmitter').get.call({}); calling the getter through Reflect.apply with a foreign receiver; a subclass that overrides the constructor without calling super() so kEventEmitter is never assigned; accessing .eventEmitter on an object created via Object.create(proto) instead of new.","commonSituations":"Destructuring-style bugs (const { eventEmitter } = resource); wrapper or proxy code that rebinds getters; copy-style clones that copy accessors but not symbol properties; async-hooks experimentation.","solutions":["Always access the property on the instance itself: resource.eventEmitter, never a detached getter","Do not destructure getters; read them directly or via a helper that keeps the receiver","In subclasses, always call super(...) so the internal symbol properties are installed","Verify the receiver with instanceof before touching the getter in generic code"],"exampleFix":"// before\nconst { eventEmitter } = asyncResource; // 'this' lost when invoked later\neventEmitter.emit('x');\n\n// after\nasyncResource.eventEmitter.emit('x');","handlingStrategy":"type-guard","validationCode":"const { EventEmitterAsyncResource } = require('node:events');\n\nif (!(resource instanceof EventEmitterAsyncResource)) {\n  throw new TypeError('expected EventEmitterAsyncResource');\n}\nconst ee = resource.eventEmitter;","typeGuard":"const isEEAR = (v) =>\n  v instanceof require('node:events').EventEmitterAsyncResource;","tryCatchPattern":null,"preventionTips":["Access eventEmitter as a property on the instance; never destructure getters","Call super() in all subclass constructors","Reject prototype-only objects in APIs: always construct with new"],"tags":["events","async-hooks","this-binding","node-compat"],"backgroundTag":"invalid-this-receiver","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"}