Registry / serialization / sprintf-js

sprintf-js

JSON →
library1.1.3jsnpmunverified

sprintf-js is a JavaScript implementation of the `sprintf` and `vsprintf` functions, providing C-style string formatting capabilities for both browser and Node.js environments. The current stable version is 1.1.3, last published in 2016. It supports a comprehensive set of format specifiers, including argument reordering, padding, alignment, precision, and various type conversions (e.g., binary, octal, hexadecimal, scientific notation, and JSON for objects/arrays). Its key differentiators include a complete feature set for `sprintf` functionality and its lightweight nature, having no notable runtime dependencies. The package is considered mature and stable, with infrequent updates, making it a reliable choice for string interpolation when a dedicated templating engine is overkill.

npm install sprintf-js
INSTALL
IMPORT
SIG · SPRINTF-JS
S
sprintf-js
serializationjavascriptv1.1.3
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.

sprintf
const { sprintf } = require('sprintf-js');
import { sprintf } from 'sprintf-js';
This package is primarily a CommonJS module. While bundlers can often resolve ESM `import` statements, native Node.js environments and older browser setups require `require()` for direct usage.
vsprintf
const { vsprintf } = require('sprintf-js');
import { vsprintf } from 'sprintf-js';
As a CommonJS module, `vsprintf` is exposed via `module.exports`. Using ESM `import` syntax will typically fail in non-transpiled Node.js environments.
sprintf, vsprintf (individual require)
var sprintf = require('sprintf-js').sprintf; var vsprintf = require('sprintf-js').vsprintf;
This is the explicit usage pattern demonstrated in the package's README, clearly indicating CommonJS named exports.

Demonstrates basic string formatting with positional arguments, array-based formatting using `vsprintf`, numeric formatting with padding and precision, and an example of named arguments.

const { sprintf, vsprintf } = require('sprintf-js'); // Basic string formatting with argument reordering const formattedString1 = sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants'); console.log(formattedString1); // Expected: "Polly wants a cracker" // Formatting with an array of arguments const alphabetLetters = ['a', 'b', 'c', 'd']; const formattedString2 = vsprintf('The first 4 letters of the English alphabet are: %s, %s, %s and %s', alphabetLetters); console.log(formattedString2); // Expected: "The first 4 letters of the English alphabet are: a, b, c and d" // Example with numeric formatting (padding and precision) const pi = 3.1415926535; const formattedPi = sprintf('Pi to 2 decimal places: %.2f, padded to 10 chars with zero: %010.2f', pi, pi); console.log(formattedPi); // Expected: "Pi to 2 decimal places: 3.14, padded to 10 chars with zero: 0000003.14" // Example with named arguments (available since a minor version, not explicitly in 1.1.3 README but a common feature) const user = { name: 'Dolly' }; const namedArgString = sprintf('Hello %(name)s', user); console.log(namedArgString); // Expected: "Hello Dolly"
Debug
Known issues
gotchaAs of v1.1.1, older JavaScript environments (e.g., Internet Explorer < 9) may require polyfills for certain built-in features such as `String.prototype.repeat()`, `Array.isArray()`, and `Object.create()`. Running `sprintf-js` in these environments without proper polyfills can lead to runtime errors or unexpected behavior.
fix
If targeting older environments, ensure necessary polyfills are included (e.g., through a bundler like Webpack/Rollup with Babel, or by explicitly importing polyfills) before `sprintf-js` is loaded.
affects: >=1.1.1
Errors
Common errors & fixes
TypeError: sprintf is not a function
Attempting to use ES module `import` syntax in a CommonJS-only Node.js environment or without a bundler correctly transforming the import.
fix
Use CommonJS `require` syntax: `const { sprintf, vsprintf } = require('sprintf-js');` or `const sprintf = require('sprintf-js').sprintf;`
Uncaught ReferenceError: Buffer is not defined
This error typically occurs when a Node.js-specific global object (`Buffer`) is referenced in a browser environment without an adequate polyfill, often a consequence of the v1.1.1 polyfill warning.
fix
Ensure your build process includes polyfills for Node.js globals if bundling for the browser, especially when targeting environments that do not natively provide these. Verify that all required ES5/ES6+ polyfills mentioned in the `v1.1.1` warning are present.
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
sprintf-js — npm install sprintf-js · libregistry