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 badges2 Answers
Reset to default 4if 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
版权声明:本文标题:javascript - process.on() is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744675886a2619107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论