Reputation: 155
I was wondering if there is any way i could declare event listeners on the Node.JS' process
module. i.e being able to define event listeners in one file globally which applies to the entire project.
For example: I want to be able to listen to the process module's on(exit)
event to perform some logging before the process quits with the mentioned or default exitCode
(This is in a saperate file from where i'd call the process.exit()
if(!process.env.TELEGRAM_TOKEN) {
process.exit(100);
}
and i have the listener in another module declared as:
process.on("exit", (code) => {
switch(code) {
case 100:
console.error(`${code} - ${NO_API_KEYS}`);
break;
default:
console.error(`${code} - ${DEFAULT_EXIT_ERROR}`)
}
});
Problem is - It exits with code 100
but it never logs the console.error()
defined on the event listener.
Upvotes: 1
Views: 1726
Reputation: 708146
It appears that your module is not being loaded so the code is never being run to register the event handlers for the exit
event.
All you have to do to get its initialization code to run is to import the module from your main app module or any other module of yours that is part of your app initialization.
import './mymodule.js'
Upvotes: 1