Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.on() is not a function

I'm quite new to Node.js and I'm not sure to completely understand what this error actually means:

process.on('uncaughtException', err => {

   ^

TypeError: process.on is not a function

I read that one should not import process because it's automatically injected. My code is like this:

var settings;

var jsonfile = require("jsonfile");
var file = "server/settings.json";

try {
    var obj = jsonfile.readFileSync(file);
    settings = obj;

} catch (err) {
    var msg = err + ". Exiting";
    console.error(msg);
    throw new Error("Fatal");
}

// some other functions

process.on('uncaughtException', function (err) {
    console.error((new Date).toUTCString() + ' uncaughtException:', err.message)
    console.error(err.stack)
    process.exit(1)
  })

module.exports.login = login;
module.exports.logout = logout;

My intentions are to exit if I cannot read the settings file. This is by design. I'm aware other approaches might be better, but my question is why I get the error above?

I'm running Node.js 8.12.0 on Windows 7 64-bit.

like image 432
Mark Avatar asked Oct 16 '25 03:10

Mark


1 Answers

if you want to add that, what you can do is use that right at the end of your app.js or server.js( whatever your file may be).

that will globally catch any uncaught errors and log them

app.listen(port, () => console.log(`app listening on port ${port}!`));

process.on('uncaughtException', function (error) {
    console.log(error);
}); 

so exporting is not necessary...

like image 179
ALPHA Avatar answered Oct 17 '25 15:10

ALPHA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!