a blog for those who code

Monday 6 April 2015

Process Object in NodeJS

In this post we are going to discuss about Process in NodeJS. To give you an overview each and every Node application is an instance of a Node Process Object.This Node Process Object comes with built in functionality which we will go through in this post.

According to Nodejs.org, the process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter. In simple terms, Process object's methods and properties provides information about the application or the environment in which it is running.

The process.execPath method returns the execution path for the Node application, process.version provides Node Version and process.platform identifies the server platform.


Node normally exit with a status code of 0 when no more async operations are pending. Now we will show you what are other status code :

Status Code 1 : Uncaught Fatal Exception
Status Code 3 : Internal JavaScript Parse Error
Status Code 4 : Internal JavaScript Evaluation Failure
Status Code 5 : Fatal Error
Status Code 6 : Non Functional Internal Exception Handler
Status Code 7 : Internal Exception Handler Run-time Failure
Status Code 9 : Invalid Argument
Status Code 10 : Internal JavaScript Run-Time Failure
Status Code 12 : Invalid Debug Argument

The process object also has the support of STDIO (Standard IO) streams stdin, stdout, and stderr. stdin and stdout streams are asynchronous whereas stderr stream is a synchronous.

Another useful Process method is memoryUsage, which tells us how much memory the Node application is using. This could be extremely useful when you are doing performance tuning or doing some re-factoring. The heapTotal and heapUsed properties refer to the V8 engine's memory usage.


Last but not the least, process.nextTick attaches a callback function that's fired during the next tick in the Node event loop. The process.nextTick method can be useful if you want to delay a function asynchronously. This is important in developing APIs where you want to give the user the chance to assign event handlers after an object has been constructed.

Another reason you might end up using process.nextTick when you are running your application that has a function performing some complex time consuming operation, then you could break the process into sections each called via process.nextTick. Thus it will help you application to run smoothly rather than waiting for the complex operation to get finish.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment