Process Module
Last Updated : 2026
The process object represents the current Node.js process. It provides information about the environment, such as the current working directory, command line arguments, environment variables and control over, the currently running Node.js application.
It is a global object that can be accessed from anywhere in your code. you do not need to use require() or import, to use it in your code,but best practice is to import it.
- Commonjs Syntax:
const process = require("node:process");
- ESM Syntax:
import process from "node:process";
Some common properties and methods of the process object are:
- process.argv: An array of command-line arguments passed to the Node.js process.
console.log("Command Line Arguments:", process.argv);
- process.env: An object containing environment variables.
console.log("Environment Variables:", process.env);
- process.exit(): Forcibly terminates the current synchronous process with a specific exit status code (0 for success, 1 or higher for errors).
process.exit();
- process.uptime(): Returns the number of seconds the Node.js process has been running.
console.log("Uptime:", process.uptime());
- process.cwd(): Returns the current working directory of the Node.js process.
console.log("Current Working Directory:", process.cwd());
- process.chdir(): Changes the current working directory of the Node.js process.
process.chdir("/path/to/new/directory");
- process.memoryUsage(): Returns an object containing memory usage statistics for the current Node.js process.
console.log("Memory Usage:", process.memoryUsage());
- process.stdout.write() : A writable stream used to send output data (similar to console.log but not including newline)
process.stdout.write("Hello, world!\n");
- process.on('uncaughtException', (err) => {}): Registers a listener for uncaught exceptions in the Node.js process.Catches any error that was thrown but never handled in a try-catch block
process.on("uncaughtException", (error) => {
console.error(`Application crashed due to: ${error.message}`);
process.exit(1);
});
Note: Position of this listener is important, it should be above if you not use asynchronous code.
- process.on('exit', () => {}): Registers a listener for the exit event of the Node.js process. This event is emitted when the process exits. Triggers right before the program terminates to run cleanup actions
process.on("exit", (code) => {
console.log(`System about to shut down with exit code: ${code}`);
});
