LEARNNode CLI Automation

Node.js Process

There is a core module of Node.js called process that provides you the information about the current Node.js process. You can also use this module to control your Node.js processes. Let's take a look at it now.


It is an object which is an instance of EventEmitter. Since it is a global module, you don't need to require it in your project.

The process object has an on method which you can use to exit from any process in your project. You can also add some custom code that will be run before the process exits like this:

process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code: ', code);
process.on('exit', (code) => {
console.log('Process exit event with code: ', code);
console.log('This message is displayed first.');

When this code will run, it will first log the console.log. After this, it is going to log the text in the terminal which is in the beforeExit process.on method. And finally, it will run the code in the exit process.on method.

exit process

You can read about the processes here. For now, I am going to show you a node module that will help you to custom handle your errors.

Wrapping Up

This process object has many use cases. For instance, you can use it to get your current working directory. You can also use it to better handle your CLI errors. So I will highly recommend you to learn this global module.

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)