The process object in Node.js is a global object that provides information about, and control over, the current Node.js process.
It allows us to interact with the underlying "operating system", "manage environment variables", and control the execution flow of our Node.js application.
Accessing Environment Variables: We can use `process.env` extends with `.` operator and `variableName` to access environment variables. This is useful for configuration and passing information to our Node.js application.
Handling Command-Line Arguments: We can pass arguments through the command line to the Node.js process using `process.argv` array and through that, we can control the behaviour of our application based on command-line input.
Standard Input/Output: Using `process.stdin`, `process.stdout`, and `process.stderr` we can interact with standard input, output, and error streams, respectively. This is useful for handling user input through command-line interfaces.
Exiting the Process: We can use `process.exit()` to exit the current Node.js process with a specified exit code. This is useful for terminating the process in response to certain conditions or errors.
Handling Signals: Using `process.on('signal', handler)` we can listen for signals such as SIGINT (Ctrl+C), SIGTERM, and SIGUSR1. This allows us to gracefully shut down our application in response to external events.
Monitoring Process Performance: The `process.memoryUsage()` and `process.cpuUsage()` methods of the `process` object provide information about the memory and CPU usage. This can be useful for monitoring and optimizing performance.
Properties and Methods provided by the process object in Node.js:
An object containing the user environment.
Environment Variables we can define in ".env", ".env.development", ".env.production" or ".env.testing" as per the configuration setup required.
LOGIN_SERVICE="https://login.com/signin"
We can access environment variables using "process.env.VARIABLE_NAME".
console.log(process.env.LOGIN_SERVICE);
An array containing the command-line arguments passed to the Node.js process.
The first two elements of `process.argv` are always 'node path' and the `path to the script` being run.
console.log(process.argv); // Output: [ 'node executable path']
When we run a Node.js script from the command line.
> node server.js argV1 argV2 // process.argv array will persist these values: [ '/path/to/node/executable', // process.argv[0] '/path/to/server.js', // process.argv[1] 'argV1', // process.argv[2] 'argV2', // process.argv[3] ]
Returns the current working directory of the Node.js process.
console.log(`Current Working Directory: ${process.cwd()}`);
'/Users/username/node-server'
The process ID (PID) of the Node.js process.
console.log(`PID: ${process.pid}`);
A string representing the operating system platform.
console.log(`Operating System: ${process.platform}`);
'Darwin' // Output: can be different on your machine
A string representing the version of Node.js.
console.log(`Node.js Version: ${process.version}`);
'v21.7.1' // Output: can be different on your machine
The `SIGINT` event occurs when the Node.js process receives the interrupt signal, such as pressing Ctrl + C in the terminal. We can listen for this event using the `process.on()` method to perform cleanup tasks or gracefully shut down our application when an interrupt signal occurs.
process.on('SIGINT', () => { console.log('Receives SIGINT signal for Exiting'); process.exit(); // Shut down application gracefully });
`process.stdin` is a readable stream that represents the standard input (stdin) of the Node.js process. We can use it to listen for user input from the terminal.
process.stdin.setEncoding('utf8'); process.stdin.on('data', (data) => { console.log('Recieving User Input: ', data); });
Here, we're setting the encoding to 'utf8' to ensure that input is treated as UTF-8 encoded strings. Now, we can listen for the 'data' event, which is emitted when data is available on the input stream and log it to the console.
`process.stdout` is a writable stream that represents the standard output (stdout) of the Node.js process. We can use it to output information to the terminal.
process.stdout.write('Writing data in stdout writable stream');
Here, we're using `process.stdout.write()` method to write the string to the standard output stream.
`process.stderr` is a writable stream that represents the standard error (stderr) of the Node.js process. It's typically used to output error messages to the terminal.
process.stderr.write('Something went wrong.');
Here, we're using `process.stderr.write()` to write an error message to the standard error stream.