I have a Node.js app that represents a class room. I'm using Node version 11.11.0. This app has two files: index.js and student.js. The relevant code looks like this:
index.js
const EventEmitter = require('events');
const Student = require('./student');
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`);
student.attend();
}
}
start();
student.js
'use strict';
const EventEmitter = require('events');
class Student {
constructor(name) {
this.name = name;
}
attend() {
// simulate a student randomly asking a question within 10 minutes
let minutes = (Math.floor(Math.random() * 10) + 1) * 60000;
setTimeout(function() {
EventEmitter.emit('handRaised', 'What is the 2 + 3?');
}, minutes);
}
}
module.exports = Student;
When I run this, I get an error that says EventEmitter.emit is not a function
. I've tried several variations without any luck. What am I doing wrong?