Node.js Process Module?

TheDevStory
2 min readSep 13, 2024

For backend developers, managing and understanding program processes is very important. Node.js offers the Process module to handle this task.

The key roles of the Process module can be summarized by 6 elements, as follow. ⭐⭐⭐⭐⭐⭐

  1. Accessing system information
  2. Managing environment variables (e.g., NODE_ENV)
  3. Handling command line arguments
  4. Process control
  5. Event handling
  6. Accessing standard I/O streams

Now let’s examine some code examples to see how we use these features.

// 1. Accessing system information
console.log(`Memory usage: ${process.memoryUsage().heapUsed} bytes`);
// Output example: Memory usage: 4236288 bytes

// 2. Managing environment variables
console.log(`Current NODE_ENV: ${process.env.NODE_ENV}`);
// Output example: Current NODE_ENV: development
process.env.MY_VARIABLE = "Hello, World!";
console.log(`MY_VARIABLE: ${process.env.MY_VARIABLE}`);
// Output example: MY_VARIABLE: Hello, World!

// 3. Handling command line arguments
console.log(`Arguments passed to the script: ${process.argv.slice(2)}`);
// Output example (when running: node script.js arg1 arg2):
// Arguments passed to the script: arg1,arg2

// 4. Process control
process.on('SIGTERM', () => {
console.log('Process is terminating.');
process.exit(0);
});
// Output example: (When receiving SIGTERM signal) Process is terminating.

// 5. Event handling
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
process.exit(1);
});
// Output example: (When an uncaught exception occurs)
// Uncaught Exception: Error: Intentionally thrown error

// 6. Accessing standard I/O streams
process.stdout.write('This is a message written directly to stdout.\n');
// Output: This is a message written directly to stdout.

// Printing current working directory
console.log(`Current working directory: ${process.cwd()}`);
// Output example: Current working directory: /home/user/projects/node-app

// Printing process ID
console.log(`Process ID: ${process.pid}`);
// Output example: Process ID: 12345

So simple, right? But sometime ‘simple can be the best’. For the beginners in Node.js or backend engineering, increasing proficiency with the Process module is crucial. Programming ultimately comes down to handling computer processes. So, I hope you master this module early on, so you don’t have to keep returning to your basic skills handbook. 🙏

🙇🙇 Thank you for reading!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

TheDevStory
TheDevStory

Written by TheDevStory

A web developer crafting online experiences. Also football(soccer) coach and Spanish Learner.

No responses yet

Write a response