What is process?
1. What process is
processis an object provided by Node.js that describes the currently running JavaScript process in the Node.js environment.
It's available anywhere in the program, with no need to import it:
console.log(process);An analog in other languages:
- in Python,
sys+os; - in C,
getpid(),argv,exit(), and so on.
2. Main capabilities of process
| Category | What it does |
|---|---|
| Process information | PID, Node.js version, architecture, platform |
| Environment variables | process.env |
| Launch arguments | process.argv |
| Standard streams | process.stdin, process.stdout, process.stderr |
| Ending the process | process.exit() |
| Lifecycle events | beforeExit, exit, uncaughtException, SIGINT |
| Resource information | Memory, CPU, uptime, the current directory |
| Node.js flags and options | process.execPath, process.execArgv |
3. Main properties and methods of process
process.pid
→ The ID of the current process (Process ID)
console.log(process.pid); // for example, 9342process.version
→ The Node.js version
console.log(process.version); // v22.8.0process.platform
→ Which platform Node.js is running on
console.log(process.platform); // 'win32' | 'linux' | 'darwin'process.arch
→ The processor architecture
console.log(process.arch); // 'x64', 'arm64', 'ia32', etc.process.cwd()
→ The current working directory
console.log(process.cwd()); // /Users/tim/projects/appprocess.env
→ An object holding all environment variables
console.log(process.env.NODE_ENV); // 'production'
console.log(process.env.PATH); // the system PATHUsed for configuration:
if (process.env.NODE_ENV === 'production') {
console.log('Running in production');
}process.argv
→ The command-line arguments Node.js was started with
node app.js dev 8080console.log(process.argv);
// [ '/usr/local/bin/node', '/path/to/app.js', 'dev', '8080' ]Often used for CLI tools.
process.memoryUsage()
→ Shows how much memory the app is using
console.log(process.memoryUsage());
/*
{
rss: 24576000,
heapTotal: 5464064,
heapUsed: 3661728,
external: 8704
}
*/process.uptime()
→ Time (in seconds) since the process started
console.log(process.uptime()); // 5.312process.exit([code])
→ Ends the process
process.exit(0); // a normal exit
process.exit(1); // an exit with an errorCode 0 means success,
any other code means an error (for example, used in CI/CD or shell scripts).
process.on(event, handler)
→ Subscribing to process events
Examples:
When the program ends:
process.on('exit', (code) => {
console.log('Exiting with code:', code);
});An uncaught error:
process.on('uncaughtException', (err) => {
console.error('Uncaught error:', err);
});When Ctrl+C is pressed:
process.on('SIGINT', () => {
console.log('Process interrupted by user');
process.exit();
});process.stdin, process.stdout, process.stderr
→ Input/output streams
process.stdout.write('Enter your name: ');
process.stdin.on('data', (data) => {
console.log(`Hello, ${data.toString().trim()}!`);
});This is a basic way to work with the console with no external libraries.
4. An example in a real Node.js application
console.log(`Started with PID: ${process.pid}`);
console.log(`OS: ${process.platform}`);
console.log(`Node.js: ${process.version}`);
console.log(`Memory: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`);
process.on('exit', (code) => {
console.log(`Process exited with code ${code}`);
});
setTimeout(() => {
console.log('Exiting in 2 seconds...');
process.exit(0);
}, 2000);5. Why process matters
- It lets you interact with the operating system;
- it manages the environment and configuration (through
env); - it gives access to CLI parameters;
- it manages input/output streams;
- it lets you catch errors and exit cleanly.
6. Common use cases
| Scenario | Used for |
|---|---|
| Environment configuration | process.env.NODE_ENV |
| CLI applications | process.argv |
| Debugging and logging | process.memoryUsage(), process.uptime() |
| Ending the process | process.exit() |
| Error handling | process.on('uncaughtException', handler) |
| Graceful shutdown | process.on('SIGINT', cleanup) |
7. Quick summary
| Property / Method | Purpose |
|---|---|
process.pid | The process ID |
process.platform | The OS platform |
process.env | Environment variables |
process.argv | Launch arguments |
process.cwd() | The current directory |
process.memoryUsage() | Memory usage |
process.uptime() | How long the process has run |
process.exit([code]) | Ending the process |
process.on(event, cb) | Subscribing to process events |
process.stdin / stdout / stderr | Input/output streams |
In one sentence:
processis Node.js's global object that gives access to information about the running process, environment variables, command-line arguments, input/output streams, and control over how the program exits.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.