admin管理员组

文章数量:1393402

I'm quite new to Node.js and I'm not sure to pletely 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.

I'm quite new to Node.js and I'm not sure to pletely 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.

Share Improve this question asked Jan 15, 2019 at 12:11 MarkMark 5,19511 gold badges73 silver badges152 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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...

You should move the process.on() function before the try catch, otherwise your uncaughtException event won't take effect.

var settings;

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

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

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

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

本文标签: javascriptprocesson() is not a functionStack Overflow