The "process" object

This article covers the use of `process.env` and `process.argv` in Node.js for handling environment variables and command-line arguments, respectively. It explains how to pass and access custom environment variables dynamically, such as setting a different `PORT` value for different environments. Additionally, it details how `process.argv` is used to parse command-line arguments, illustrating how to access the Node.js executable path and the script path along with any additional arguments provided during execution.

process.env

You can use the node command with custom environment variables.

e.g. VAL1=10 VAL2=20 ode app.js

The above values can be accessed using process object in app.js

e.g. process,env,VAL1 and proess.env.VAL2

This allows us to have flexibility of passing dynamic values with command. One of common use case is passing the PORT value as per different environments.

PORT=3000 node app.js This way we can make it dynamic configuration of port.

process.argv

process.argv is an array in Node.js that contains the command-line arguments passed when the Node.js process was launched. The first element will be 'node', the second element will be the name of the JavaScript file being executed, and the remaining elements will be any additional command-line arguments.

// consider this is executed like node app.js hello world
console.log('Process argv:', process.argv);
// ["node", "app.js", "hello", "world"]
// Accessing specific arguments from process.argv
// The first argument is typically the executable path of Node.js
console.log('Node executable path:', process.argv[0]);
 // The second argument is the path of the script being executed
console.log('Script path:', process.argv[1]);