The Node.js util Module

Use promisify, format, inspect, and other utilities for integration and diagnostics.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What is the util module? Why do we use it? Common util methods promisify() callbackify() format() types inspect() Real-world examples Interview questions What is the util Module? The util module is a built-in Node.js module that provides helper functions for debugging, formatting, working with callbacks, and promises. Simple Definition: The util module contains utility functions that make Node.js development easier. Since it's a built-in module, you don't need to install it. Importing the util Module CommonJS const util = require ( "node:util" ); or const util = require ( "util" ); ES Module import util from "node:util" ; Real-Life Example 🧰 Imagine a toolbox. Toolbox │ ├── Hammer ├── Screwdriver ├── Wrench └── Pliers You use different tools for different jobs. Similarly, util Module │ ├── promisify() ├── callbackify() ├── inspect() ├── format() └── types Why Do We Use util ? The util module helps you: Convert callbacks into Promises Convert Promises into callbacks Format strings Print complex objects Check data types 1. util.promisify() Converts a callback-based function into a Promise-based function . Callback Function const fs = require ( "fs" ); fs . readFile( "test.txt" , "utf8" , ( err , data ) => { if ( err ) throw err ; console . log( data ); }); Using promisify() const fs = require ( "fs" ); const util = require ( "util" ); const readFile = util . promisify( fs . readFile); async function readData () { const data = await readFile ( "test.txt" , "utf8" ); console . log( data ); } readData (); Now you can use async/await . Real-Life Example 📖 Without promisify() Read File │ ▼ Wait for Callback With promisify() Read File │ ▼ await readFile() Cleaner and easier to read. 2. util.callbackify() Converts a Promise-based function into a callback-based function . const util = require ( "util" ); async function getUser () { return "Ajay" ; } const callbackVersion = util . callbackify( getUser ); callbackVersion (( err , result ) => { console . log( result ); }); Output Ajay 3. util.format() Formats strings like printf() in C. const util = require ( "util" ); const message = util . format( "Hello %s, your age is %d" , "Ajay" , 30 ); console . log( message ); Output Hello Ajay, your age is 30 Common Placeholders Placeholder Meaning %s String %d Number %j JSON %% Percent (%) Example console . log( util . format( "%d + %d = %d" , 10 , 20 , 30 )); Output 10 + 20 = 30 4. util.inspect() Prints objects in a readable format. const util = require ( "util" ); const user = { name: "Ajay" , skills: [ "Node.js" , "Angular" ] }; console . log( util . inspect( user )); Output { name: 'Ajay', skills: [ 'Node.js', 'Angular' ] } Show Nested Objects const obj = { a: { b: { c: { d: 10 } } } }; console . log( util . inspect( obj , { depth: null })); Using depth: null displays all nested levels. 5. util.types Provides methods to check JavaScript types. const util = require ( "util" ); console . log( util . types . isDate( new Date ())); Output true Another example: console . log( util . types . isMap( new Map ())); Output true Real-Life Example 🏦 Suppose you're reading customer data. Database │ ▼ Read File │ ▼ Convert Callback │ ▼ await readFile() util.promisify() makes your code cleaner and easier to maintain. Common util Methods Method Purpose promisify() Convert callback → Promise callbackify() Convert Promise → Callback format() Format strings inspect() Display objects types Check data types Interview Question What is the output? const util = require ( "util" ); console . log( util . format( "Name: %s, Age: %d" , "Ajay" , 30 ) ); Output Name: Ajay, Age: 30 When Should You Use the util Module? ✅ Convert old callback APIs to Promises. ✅ Debug large or deeply nested objects. ✅ Format log messages. ✅ Check specific object types. Best Practices ✅ Prefer util.promisify() when working with older callback-based APIs. ✅ Use async/await with promisified functions for cleaner code. ✅ Use util.inspect() instead of JSON.stringify() when debugging complex objects. ❌ Don't use callbackify() unless you need to integrate with callback-based APIs. Common Mistakes ❌ Forgetting to await a Promisified Function const data = readFile ( "test.txt" , "utf8" ); console . log( data ); Output Promise { <pending> } ✅ Correct const data = await readFile ( "test.txt" , "utf8" ); console . log( data ); Using JSON.stringify() for Debugging console . log( JSON . stringify( obj )); It may hide useful information or fail with circular references. ✅ Better console . log( util . inspect( obj , { depth: null })); Key Points ✅ util is a built-in Node.js module. ✅ promisify() converts callback functions into Promise-based functions. ✅ callbackify() converts Promise-based functions into callback-based functions. ✅ format() formats strings using placeholders. ✅ inspect() prints objects in a readable format. ✅ util.types provides reliable type-checking helpers. 🧠 Easy Formula Need async/await? │ ▼ util.promisify() Need callback? │ ▼ util.callbackify() Need formatted text? │ ▼ util.format() Need to debug objects? │ ▼ util.inspect() Need to check types? │ ▼ util.types Remember: 🔄 promisify() → Callback ➜ Promise 🔁 callbackify() → Promise ➜ Callback 🛠️ util is a toolbox of helpful utilities built into Node.js.