Geuis
Geuis

Reputation: 42267

Getting a node.js process to die?

I'm trying to find the most elegant way for my node.js app to die when something happens. In my particular case, I have a config file with certain require parameters that have to be met before the server can start and be properly configured.

One way I have found to do this is:

var die = function(msg){
    console.log(msg)
    process.exit(1);
} 

die('Test end');

Is there a better way to handle this kind of situation?

Upvotes: 8

Views: 7524

Answers (2)

Andrey Sidorov
Andrey Sidorov

Reputation: 25446

better use console.error if you are doing process.exit immediately after.

console.log is non-blocking and puts your message into write queue where it is not processed because of exit()

update: console.log also blocks in latest versions (at least since 0.8.x).

Upvotes: 19

Alfred
Alfred

Reputation: 61771

If you want to abruptly exit then this will do just fine. If you want do any clean up you should do that first after which node.js will probably stop anyway, because nothing keeps event loop running.

Upvotes: 0

Related Questions