Chapter 35: Node.js Util Module

1. What is the util module?

The util module is a small but extremely useful collection of utility functions that don’t belong anywhere else.

It’s like Node.js’s “miscellaneous helpers” drawer.

Most functions in util are not glamorous, but they save you from writing the same 5–15 lines of boilerplate code over and over again.

In 2025–2026 reality:

  • Some functions are very frequently used (especially promisify, inspect, format)
  • Some are almost never used anymore (legacy helpers)
  • The module is very stable — almost no breaking changes for years

2. Modern import style (recommended)

JavaScript

Always use node:util prefix — makes it obvious it’s core Node.

3. The most important & frequently used functions

Let’s go through the ones you will actually use in real projects.

3.1 util.promisify(original) — the #1 most useful function

Takes a callback-style function and turns it into a promise-style function.

Classic example – before & after

Before (callback hell)

JavaScript

After — using promisify

JavaScript

Very common real pattern – promisify entire modules

JavaScript

Even better 2025–2026 style: Many core modules already have promise versions — prefer them when available:

JavaScript

3.2 util.inspect(value, options?) — the best way to print objects

JavaScript

Very useful flags

Option Typical value Effect
depth null or 5 How deep to go into nested objects (null = infinite)
colors true Color output in terminal
compact false More readable multi-line output
maxArrayLength 100 Limit long arrays
showHidden true Show non-enumerable properties

Common pattern – debug helper

JavaScript

3.3 util.format(formatString, …values) — better than string concatenation

JavaScript

Supported placeholders

  • %s → string
  • %d → number
  • %j → JSON
  • %o → object (nice inspect)
  • %% → literal %

Modern alternative: Many people just use template literals now:

JavaScript

But util.format is still very useful when you want consistent formatting or when working with legacy code.

3.4 util.isDeepStrictEqual(val1, val2) — deep equality check

JavaScript

Used in:

  • Testing
  • Memoization / caching
  • State comparison

Note: assert.deepStrictEqual uses this under the hood.

4. Other useful functions (less common but still important)

Function What it does When you use it
callbackify(original) Turn promise function → callback function Making promise APIs work with old callback code
TextDecoder / TextEncoder Convert between Buffer ↔ string (UTF-8, etc.) Low-level encoding/decoding
types.isPromise(val) Check if value is a Promise Defensive coding
types.isDate(val) Check if value is Date object Type guards

Example – callbackify

JavaScript

5. Real-world patterns (how professionals use util)

Pattern 1 – Debug / logging helper (very common)

JavaScript

Pattern 2 – Safe deep comparison in caching / memoization

JavaScript

Pattern 3 – Formatting log messages

JavaScript

6. Common mistakes & traps

Mistake Consequence Fix
Using util.inspect without {depth: null} Truncated output for deep objects Always set depth: null or Infinity
Using util.format with huge objects Very large log lines Prefer inspect or template literals
Using callbackify on already promise-based functions Double wrapping → bugs Check if function already returns promise
Relying on util.is* for type checking Not exhaustive Prefer TypeScript types when possible

Summary – Quick cheat sheet 2025–2026

You want to… Use this function Typical real-world use case
Convert callback API → promise API promisify(fn) Bridge old libraries to async/await
Pretty-print objects / debug util.inspect(value, {depth: null, colors: true}) Logging, debugging, test output
Format strings safely util.format(template, …values) Structured logging
Deep equality check util.isDeepStrictEqual(a, b) Memoization, testing, state comparison
Turn promise function → callback callbackify(asyncFn) Compatibility with old callback code

Would you like to go much deeper into any specific area?

  • Full promisify all common core modules example
  • Building a real debug logger with util.inspect
  • Using callbackify in legacy migration scenarios
  • util.inspectcustom inspection (symbols, classes)
  • Comparison: util.inspect vs console.dir vs JSON.stringify

Just tell me which direction feels most useful — I’ll continue with detailed, production-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *