KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
Node.jsNode.js Tutorial
Lesson

Node.js Process

10 min reading
Free Course

Node.js Process: Environment Variables, CLI Args, and Graceful Shutdown

The global process object is an EventEmitter representing the currently running Node.js OS process. It gives your code access to environment variables (process.env), command-line arguments (process.argv), memory diagnostics, exit codes, and Unix shutdown signals like SIGTERM.

Reading process.env, process.argv, and Memory Stats

Store secrets and ports in process.env, parse CLI flags from process.argv.slice(2) (or util.parseArgs() in Node 18.3+), and remember that every value in process.env is always a string (or undefined).

const port = Number(process.env.PORT || 3000);
const isProduction = process.env.NODE_ENV === 'production';
const cliArgs = process.argv.slice(2);
const heapMb = (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(1);

console.log({ port, isProduction, cliArgs, heapMb: `${heapMb} MB` });

Notice that process.env.ENABLE_CACHE = 'false' is a non-empty string, so Boolean('false') would evaluate to true! Always compare explicitly (process.env.ENABLE_CACHE === 'true').

Handling Graceful Shutdown (SIGINT & SIGTERM)

When Docker, Kubernetes, or Ctrl+C stops your server, the OS sends SIGTERM or SIGINT. Listening for those signals lets your app stop accepting new HTTP connections, finish active requests, close database pools, and exit cleanly with code 0.

function registerShutdown(closeResources) {
  for (const signal of ['SIGINT', 'SIGTERM']) {
    process.on(signal, async () => {
      console.log(`Received ${signal}, shutting down gracefully...`);
      await closeResources();
      process.exit(0);
    });
  }
}

Common Mistakes to Avoid

  • Calling process.exit(1) immediately before logs or DB queries flush: Hard exits drop buffered writes and active HTTP responses. Set process.exitCode = 1 and let the event loop drain cleanly when possible.
  • Checking if (process.env.DEBUG) without comparing 'true': Because env vars are always strings, DEBUG="false" is truthy in JavaScript!
  • Ignoring uncaughtException or unhandledRejection: Log fatal errors cleanly, flush telemetry, and exit non-zero (1) so your process manager (PM2, systemd, Docker) restarts a clean process.

Frequently Asked Questions

What is the difference between process.cwd() and import.meta.dirname?

process.cwd() returns the terminal folder from which the user launched node, while import.meta.dirname (or __dirname) is always the folder containing the current .js file.

What do Node.js exit codes 0 and 1 mean?

Exit code 0 signals success to the shell or CI runner. Any non-zero code (commonly 1) signals failure.

How can I parse CLI flags without installing commander or yargs?

Node.js 18.3+ includes import { parseArgs } from 'node:util', which parses --port 8080 and boolean flags cleanly out of the box.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js Global Objects
Continue JourneyNext: Node.js Child Processes

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum