Updated March 23, 2023
Introduction to Node.js Process
Node.js gives us the privilege to retrieve process information such as the architecture, platform, version, process id, release. It also allows aborting processes and much more.
The process is the global object and accessible from anywhere. It is an instance of EventEmitter class. The process object provides access to a bunch of process events like ‘beforeExit’, ‘exit’, ‘disconnect’, ‘uncaughtException’, ‘unhandledRejection’, etc and functions like process.cwd(), process.abort() . Few properties of process objects that are widely used are process.env, process.stdout, process.stdin, process.stderr, etc.
Process Events
Let us list down some of the process events:
1. beforeExit
Usually, node exits when the event loop is empty and there is no further scheduled work to be performed. The beforeExit process event is used to perform asynchronous calls just before the node exits.
2. exit
The exit event takes place when the node is about to exit. It cannot perform any asynchronous calls like beforeExit event.
3. disconnect
If the node.js process is spawned with an IPC (Inter-Process Communication) Channel, the disconnect event is emitted when the IPC Channel is closed.
4. uncaughtException
The uncaughtException event occurs when an uncaught JavaScript exception pops out all the way back to the Event Loop. The default behavior of an uncaught JavaScript exception is that, node prints the stack trace to stderr. But adding a handler explicitly which handles uncaughtException, overrides the node’s default behavior.
5. unhandledRejection
The unhandledRejection event is emitted when a promise gets rejected and no error handler is provided to handle the error. Usually, the catch() method is used to handle the error or exceptions that occurred during promises.
Demonstration of few events
Code:
const process = require('process')
process.on('beforeExit', (code) => {
console.log(`With code ${code} process beforeExit event`);
});
process.on('exit', (code) => {
console.log(`With code ${code} process exit event`);
});
console.log('This message is displayed first');
Output:
Properties of Node.js Process
Let us have a look through a few process properties:
1. process.env
The process.env contains an object that contains the value of the user environment.
2. process.pid
The process.pid property returns the PID of the node.js process.
3. process.ppid
The process.ppid property returns the PID of the current parent node.js process.
4. process.release
The process.release property returns the information to the current release, including urls and headers.
5. process.title
The process.title property returns the title of the current process.
6. process.version
The process.version gives you the currently installed version of the node. If you would really like to check it out, go to the command prompt and do the following steps:
- node
- process.version
We used global.process, because as we know the process object is accessible from anywhere.
Output:
7. process.versions
The process.versions also give us the version value, but as we can see below using global.process.versions we are able to retrieve the versions of all other modules or dependencies present in the node stack.
Output:
8. process.platform
Using global.process.the platform, we can fetch the name of the operating system platform on which our Node.js process is running. The possible values are linux, win32, darwin, etc.
9. process.channel
The process.channel property is a reference to the IPC Channel if the Node.js process was been spawned by the IPC Channel. The process.channel property returns “undefined” as its value if the IPC Channel doesn’t exists.
10. process.connected
The process.connected returns its value “true” if the Node.js process is spawned with an IPC Channel and retains the value as long as it is connected. It returns “false” value upon the process.disconnect(). Once it is disconnected, we are no longer able to send messages using the process.send() method.
11. process.argv
The process.argv returns an array that consists of all command-line invocation arguments. The following screenshot illustrates the same:
12. process.argv0
It consists of the original value of argv[0] (read-only copy) when the Node.js starts.
13. process.arch
To fetch the value of operating system CPU architecture we use process.arch property. Possible values are : x32, x64, arm64, ia32, etc.
14. process.stdin
To read input, stdin stands for ‘standard in’.
15. process.stdout
To write output, stdout stands for ‘standard output’.
16. process.stderr
Exceptions are written using process.stderr(standard error)
Functions of Node.js Process
Let us have a quick look through some node.js process functions:
a) process.cwd()
To retrieve the information of the current working directory we can use the process.cwd() method as shown:
b) process.abort()
The process.abort() method gives us the privilege to exit from the current process immediately.
c) process.setuid(id)
To set the user identity of the node process we use process.setuid(). The value of the id can either be a username(string) or numeric type.
d) process.getuid()
To fetch the numeric user identity of the node process we use process.getuid() method.
e) process.seteuid(id)
To set the effective user identity of the node process we use process.seteuid().
The value of the id can either be a username(string) or numeric type.
f) process.geteuid()
To fetch the numerical effective user identity of the node process we use process.geteuid() method.
g) process.setgid(id)
To set the group identity of the node process we use process.setgid(id) method.
The value of the id can either be a group name (string) or numeric type.
h) process.getgid()
To retrieve the numeric group identity of the node process, process.getgid() method is used.
i) process.setegid(id)
To set the effective group identity of the node process we use process.setgid(id) method. The value of the id can either be a group name (string) or numeric type.
j) process.getegid()
To retrieve the numerical effective group identity of the node process, process.getgid() method is used.
Conclusion
Thus we overlooked through the global process object of the node (being the instance of EventEmitter), few of its events as beforeExit, exit, uncaughtException, etc.
We also went through few properties of the node.js process like process.pid, process.arch and some of the process functions.
Recommended Articles
This is a guide to Node.js Process. Here we discuss the properties of the node.js process and some of the process functions. You may also have a look at the following articles to learn more –