diff --git a/lib/events.js b/lib/events.js index 6505606b4e5826..b69b335cbe5f4f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -9,6 +9,7 @@ var isArray = Array.isArray; // that to be increased. Set to zero for unlimited. var defaultMaxListeners = 10; EventEmitter.prototype.setMaxListeners = function(n) { + if (!this._events) this._events = {}; this._events.maxListeners = n; }; diff --git a/test/simple/test-event-emitter-add-listeners.js b/test/simple/test-event-emitter-add-listeners.js index 7c4b7232ebd051..306f4fca8ec7fb 100644 --- a/test/simple/test-event-emitter-add-listeners.js +++ b/test/simple/test-event-emitter-add-listeners.js @@ -23,6 +23,12 @@ console.log('start'); e.emit('hello', 'a', 'b'); + +// just make sure that this doesn't throw: +var f = new events.EventEmitter(); +f.setMaxListeners(0); + + process.addListener('exit', function() { assert.deepEqual(['hello'], events_new_listener_emited); assert.equal(1, times_hello_emited);