Node.js's util module for all engines providing cross-platform utility functions for formatting, type checking, inheritance, promises, and debugging.
—
Essential tools for formatted string output and detailed object inspection with customizable styling and output options.
Printf-style string formatting with placeholder substitution.
/**
* Formats a string using printf-style placeholders
* @param {string} f - Format string with placeholders (%s, %d, %j, %%)
* @param {...any} args - Arguments to substitute into placeholders
* @returns {string} Formatted string
*/
function format(f, ...args): string;Supported placeholders:
%s - String conversion%d - Number conversion%j - JSON.stringify conversion%% - Literal percent signUsage Examples:
const util = require('util');
// Basic formatting
util.format('Hello %s', 'world'); // "Hello world"
util.format('Number: %d', 42); // "Number: 42"
util.format('JSON: %j', { a: 1 }); // "JSON: {"a":1}"
util.format('Percent: %%'); // "Percent: %"
// Multiple placeholders
util.format('%s is %d years old', 'Alice', 25); // "Alice is 25 years old"
// Extra arguments are appended
util.format('%s %s', 'a', 'b', 'c'); // "a b c"
// Non-string format argument
util.format(123, 'abc'); // "123 abc"Returns a string representation of objects with detailed formatting options.
/**
* Returns a string representation of an object
* @param {any} obj - Object to inspect
* @param {InspectOptions} [opts] - Inspection options
* @returns {string} String representation of the object
*/
function inspect(obj, opts?): string;
interface InspectOptions {
/** Show hidden (non-enumerable) properties */
showHidden?: boolean;
/** Recursion depth (default: 2) */
depth?: number;
/** Use ANSI color codes for styling */
colors?: boolean;
/** Enable custom inspect functions */
customInspect?: boolean;
}Usage Examples:
const util = require('util');
const obj = {
name: 'example',
data: { numbers: [1, 2, 3], active: true },
method: function() { return 'hello'; }
};
// Basic inspection
console.log(util.inspect(obj));
// With colors (for terminal output)
console.log(util.inspect(obj, { colors: true }));
// Show hidden properties
console.log(util.inspect(obj, { showHidden: true }));
// Control depth
console.log(util.inspect(obj, { depth: 1 }));
// Inspect primitive values
util.inspect(123); // "123"
util.inspect('hello'); // "'hello'"
util.inspect(true); // "true"
util.inspect(null); // "null"Configuration objects for customizing inspect output appearance.
// Color codes for ANSI styling
inspect.colors: {
bold: [number, number];
italic: [number, number];
underline: [number, number];
inverse: [number, number];
white: [number, number];
grey: [number, number];
black: [number, number];
blue: [number, number];
cyan: [number, number];
green: [number, number];
magenta: [number, number];
red: [number, number];
yellow: [number, number];
};
// Style mappings for different value types
inspect.styles: {
special: string;
number: string;
boolean: string;
undefined: string;
null: string;
string: string;
date: string;
regexp: string;
};Usage Examples:
const util = require('util');
// Customize colors
util.inspect.colors.custom = [95, 39]; // Custom color code
util.inspect.styles.myType = 'custom';
// Default color mappings
console.log(util.inspect.styles.number); // "yellow"
console.log(util.inspect.styles.string); // "green"
console.log(util.inspect.styles.boolean); // "yellow"Install with Tessl CLI
npx tessl i tessl/npm-util