Registry / observability / cli-sprintf-format

cli-sprintf-format

JSON →
library1.1.1jsnpmunverified

cli-sprintf-format is a utility package that provides an enhanced `sprintf`-like formatting function specifically tailored for command-line interface (CLI) applications. It builds upon Node.js's `util.formatWithOptions`, offering more robust type resolution, error handling, and intelligent color support. The package automatically detects terminal color capabilities via `supports-color` and applies coloring not only to inspected objects but also to primitive values, including colored JSON output. The current stable version is 1.1.1, released in late 2021, suggesting a relatively stable but infrequent release cadence focused on maintenance and bug fixes. Key differentiators include its improved object inspection depth (defaulting to 4, configurable via `FORMAT_INSPECT_DEPTH`) and specialized handling of `%s` and `%#s` format specifiers for string output, allowing for colored and inline display or raw string output, respectively.

npm install cli-sprintf-format
INSTALL
IMPORT
SIG · CLI-SPRINTF-FORMAT
C
cli-sprintf-format
observabilityjavascriptv1.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cliFormat
import cliFormat from 'cli-sprintf-format';
const cliFormat = require('cli-sprintf-format').default;
While the package is primarily CommonJS, it can be imported as a default export in ESM. The package exports a single function.
cliFormat (CJS)
const cliFormat = require('cli-sprintf-format');
This is the documented and primary way to import the formatter in CommonJS environments.

Demonstrates basic string, number, object, and JSON formatting, including the effect of environment variables on object inspection depth.

const cliFormat = require("cli-sprintf-format"); // Basic string and number formatting console.log(cliFormat("User '%s' has %d unread messages.", "Alice", 5)); // Formatting with an object, showing default inspection depth and coloring const userProfile = { id: 101, name: "Bob", email: "bob@example.com", settings: { theme: 'dark', notifications: true } }; console.log(cliFormat("Profile details: %o", userProfile)); // Formatting with JSON, which will also be colored if supported const data = { status: 'success', items: [{ id: 1, value: 'test' }] }; console.log(cliFormat("API Response: %j", data)); // Using environment variable to control inspection depth (run with: FORMAT_INSPECT_DEPTH=2 node your-script.js) console.log(cliFormat("Deep object (depth 2 via env): %o", { a: { b: { c: { d: 1 } } } }));
Debug
Known issues
gotchaWhen formatting strings with `%s`, cli-sprintf-format passes them through Node.js's `util.inspect` formatter. This results in colored output (if supported) and makes multiline strings appear inline. If you need the raw string output without this processing, use `%#s` instead.
fix
Use `%#s` instead of `%s` for string arguments that should not be processed by `util.inspect`.
affects: >=1.0.0
gotchaObject inspection depth for `%o` and `%O` specifiers defaults to `4`. For deeply nested objects, this might truncate output. The depth can be overridden globally via the `FORMAT_INSPECT_DEPTH` environment variable.
fix
Set the `FORMAT_INSPECT_DEPTH` environment variable (e.g., `FORMAT_INSPECT_DEPTH=6 node your_script.js`) to increase the inspection depth, or consider stringifying objects explicitly if full depth is always required.
affects: >=1.0.0
breakingA bug fix in v1.1.1 ensured consistent literal output in Node.js v12+ versions. While a 'fix', applications relying on the previous (buggy) behavior in Node.js v12 or higher might see subtle changes in string literal representation.
fix
Review string literal outputs in Node.js v12+ after upgrading to v1.1.1 to ensure no unintended formatting changes occurred. No code change is typically required unless specific literal formatting was being implicitly relied upon.
affects: >=1.1.1
Errors
Common errors & fixes
TypeError: cliFormat is not a function
Attempting to import `cli-sprintf-format` as a named export in an ESM module or incorrectly destructuring a CommonJS require.
fix
For ESM, use `import cliFormat from 'cli-sprintf-format';`. For CommonJS, use `const cliFormat = require('cli-sprintf-format');`.
Unexpected output format for strings (e.g., extra quotes, colors, newlines removed)
Using `%s` format specifier, which passes strings through `util.inspect`, resulting in formatting and colorization.
fix
If raw string output is desired, switch to `%#s` instead of `%s`.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
cli-colorrequiredProvides ANSI color formatting capabilities for terminal output.
supports-colorrequiredDetects and determines the level of color support in the terminal environment.
Agent activity
27 hits · last 30 days
node
22
OpenAI (training)
1
Resources
cli-sprintf-format — npm install cli-sprintf-format · libregistry