Node.js process object
Node.js has a number of built-in, global objects and object models available across modules. These objects have an important role in Node's runtime environment and server-side JavaScript execution. They allow programmers to interact with the Node.js process by providing information (console
for instance) and control (require
, setTimeout
, setInterval
) for the currently running process.
Today I'll be diving into the process
object, an instance of the EventEmiter
class. As a whole, EventEmitter allows one to create custom and event-driven programming. Because of this, process
provides information and control over the literal process currently operating in node.
Properties and Methods
Like all objects, process
is composed of properties and methods. They help programmers manage everything from environment variables, event handling, and control over the process lifecycle itself. The process object itself can have as many as 90-100 of these properties depending on the version, so I've chosen four to go over below that pertain specifically to the Process Environment.
1. process.env
- Type: Object
- Purpose: Stores environment variables for the current Node.js process. These are often used for configuration, such as storing sensitive data (API keys, login credentials, database access). The data is stored in a .env file in the project repo. For security, the file remains on the programmer's local repo and should never be pushed to the public bundle.
console.log(process.env.NODE_ENV); // 'production' or 'development'
console.log(process.env(process.env.API_KEY); // Access the API key from the .env file
2. process.on()
- Type: Method
- Purpose: Listens for and handles events occurring during the Node.js lifecycle. This is the way to attach operations to
process.exit
, as calling that terminates the Node.js process immediately. It can also be utilized withuncaughtException
andSIGINIT
(other events that kill the process) with a callbackprocess.on(event, callback)
process.on('exit', (code) => {
console.log(`The process is exiting with code: ${code}`);
});
process.on('uncaughtException', (err) => {
console.error('An uncaught error occurred:', err);
});
3. process.argv
- Type: Array
- Purpose: When you run a Node.js script, you can pass arguments directly into the command line. These arguments can later be accessed through
process.argv
. Command-line arguments in node always contain at least 2 arguments: the path to the Node executable (Node binary) and the path to the script being run. Additional arguments can be passed in after. A basic script run with arguments like:
node app.js --name=Josh --city=Nola
Would have a process.argv
containing:
[
'/usr/local/bin/node', // Node binary path
'/path/to/app.js', // Path to the script
'--name=Josh', // Arg 1
'--city=Nola' // Arg 2
]
4. process.stdin
, process.stdout
, and process.stderr
- Type: Streams
- Purpose: Standard input, output, and error streams that allow interaction with the console or other processes. They are useful for handling user input, logging, and error reporting.
//reading user input from the terminal
process.stdin.on('data', (data) => {
console.log(`You entered: ${data.toString().trim()}`);
});
// writing to stdout and stderr
process.stdout.write('This is standard output\n');
process.stderr.write('This is an error message\n');
Mastery of the process object starts with understanding its most essential properties. We've covered four key ones, but there are many more to explore. From managing environment variables to handling system signals and performance monitoring, these properties give developers powerful control over their Node.js applications.
Top comments (0)