Process API in NodeJs

Learn via video courses
Topics Covered

Overview

Process in nodejs provides access to the information about the current nodejs program. This information could be versions of nodejs, returned value of a function, error, etc. Process in nodejs controls the process of the current nodejs program. Properties and methods of process API give information about the process of the current nodejs program. Events of process API help to execute the callback and return with an exit code.

Introduction

When we run the nodejs program on the system, it uses the in-built API named process in the run-time environment. The process API gives information about the environment while executing the current nodejs program.

It is kind of behind the scene where we as a user just give instructions. Handlers on the back take care of what, when, and how to do it. Also, give us information about the whole process.

Process Object in Node JS

A process object is a global object available in nodejs i.e. we can access it anywhere for any module or .js file in nodejs. We don't need to add the process module specifically as it is already available in nodejs. However, we can add the process module with the help of the require function.

In the above code block, we include the process module by using the require function. In general, the require function downloads the particular module in the current module or .js file.

Node JS Process Properties

process.version

It gives us information about the currently installed version of nodejs on the system. Also, we can use node -v and node --version to get the currently installed version of nodejs.

  • Create a file named versions.js file add the following code and run node version.js
  • Alternatively, we can run the following command in CLI (command line interface):

process.versions

  • It gives detailed information about installed versions of nodejs and all other dependencies.
  • Create a file named versions.js file add the following code and run node version.js
  • Alternatively, we can run the following command in CLI (command line interface):

process.env

  • It tells us about the environment variables of the current user for the system.
  • Create a file named env.js file add the following code and run node version.js
  • Also, If there is any environment variable set as named MYVARIABLE then we can access its value using process.env.MYVARIABLE.
  • Alternatively, we can run the following command in CLI (command line interface):

process.release

  • It provides information about the currently installed version of nodejs like name, long-term support, source URL, etc.

  • Create a file named release.js file and add the following code and run node version.js

  • Alternatively, we can run the following command in CLI (command line interface):

process.platform

  • It returns the operating system of the current environment. The operating systems could be windows, Linux, macOS, etc. If you are on windows, it will return win32.
  • Create a file named platform.js file and add the following code and run node platform.js.
  • Alternatively, we can run the following command in CLI (command line interface):

process.features

  • It provides information about features of nodejs whether it is enabled or disabled
  • Create a file named features.js file and add the following code and run node features.js
  • Alternatively, we can run the following command in CLI (command line interface):

process.arch

  • It provides information about CPU architecture whether it is x32 or x64.
  • Create a file named architecture.js file add the following code and run node architecture.js
  • Alternatively, we can run the following command in CLI (command line interface):

Also, other properties of the process help to communicate between the node app and CLI or other platforms.

  • process.stdin is used to read the data from CLI (command line interface) or other platforms
  • process.stdout is used to write the data
  • process.stderr is used to write the data if found errors

process.argv

  • It provides information about arguments that are passed while executing the .js file.
  • Create a file named process.js file add the following code and run node process.js.
  • Alternatively, we can run the following command in CLI (command line interface):

Node JS Process Functions

cwd()

  • It returns the path of the current working directory in the nodejs process.

hrtime()

  • It returns the array of time in seconds and nanoseconds. It doesn't follow the standard clock.
  • It provides information about the current time as compared to the previous time.

memoryUsage()

  • It returns detailed information about memory used in the nodejs process.

process.kill(pid[, signal])

  • It is used to kill the running process by pid and signal strings.

uptime()

  • It returns the active seconds of the current process in nodejs.

Node JS Process Event

exit

When the current process is about to end, nodejs uses the exit event to end all the event loops and stop listening to other events. Because the event listener is responsible to run asynchronous code and the exit event already stopped listening to the event. So, Asynchronous codes will not run.

beforeExit

When nodejs has nothing in its event loop and no pending tasks, it uses the beforeExit event. It does execute synchronous as well as asynchronous code. The beforeExit event can be the cause of continuing the process in nodejs because beforeExit can execute code if the code already exists in the event queue.

uncaughtException

When any error or exception occurs in the nodejs app, nodejs use uncaughtException to catch the error and exit from the current process.

Signal Events

There are various signal events defined in nodejs in the form of strings like SIGINT, SIGHUP, etc. When these signal events occur, we may use process.id and execute a callback function.

Exit Codes

Uncaught Fatal Exception

An uncaught exception that is not handled by the uncaughtException event handler.

Internal JavaScript Parse Error

An error occurs while parsing the code. Parsing is a kind of checking the minimum criteria before executing a nodejs program.

Internal JavaScript Evaluation Failure

If a function failed to return the value, the nodejs program exit with code Internal JavaScript Evaluation Failure.

Fatal Error

It was a fatal unrecoverable error in V8. When a fatal error occurs, nodejs simply prints the error to stderr with the prefix FATAL ERROR.

Non-function Internal Exception Handler

An uncaught exception occurs if an internal fatal exception handler failed to run.

Internal Exception Handler Run-Time Failure

If an uncaught exception or an internal fatal exception function gives an error and tries to handle that error. Then, the nodejs program exit with the code Internal Exception Handler Run-Time Failure.

Unused

It was recognized as an uncaught exception in the previous versions of nodejs.

Invalid Argument

It occurs If there is an option that must have value but the value is not provided.

Internal JavaScript Run-Time Failure

The JavaScript source code internal in the Node.js bootstrapping process threw an error when the bootstrapping function was called.

Invalid Debug Argument

It occurs if we debug the nodejs program and inspection flags are set but the port number is invalid.

Signal Exits

If nodejs get a fatal signal i.e SIGHUP or SIGKILL, then the nodejs program returns the exit code with 128 plus the value of the signal code. For example, if the value of the signal code is 2 (SIGINT), the exit code will be 128+2(=130)128+2(=130)

Conclusion

  • The process in nodejs helps to control the process involved in running the nodejs program.
  • Properties of the process object provide the details of environment variables. Also, we can add custom environment variables if required.
  • Methods in the process object also return the details about the current run time environment.
  • process is an instance of an event-emitter in nodejs. And it can trigger the callback if any particular event occurs.
  • Process on exit event returns the code 0 when the nodejs program runs successfully. Otherwise, the process returns other code defined in nodejs related to the resultant error.