{"id":12062,"library":"sprintf-js","title":"sprintf-js","description":"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.","status":"active","version":"1.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/alexei/sprintf.js","tags":["javascript"],"install":[{"cmd":"npm install sprintf-js","lang":"bash","label":"npm"},{"cmd":"yarn add sprintf-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add sprintf-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { sprintf } from 'sprintf-js';","symbol":"sprintf","correct":"const { sprintf } = require('sprintf-js');"},{"note":"As a CommonJS module, `vsprintf` is exposed via `module.exports`. Using ESM `import` syntax will typically fail in non-transpiled Node.js environments.","wrong":"import { vsprintf } from 'sprintf-js';","symbol":"vsprintf","correct":"const { vsprintf } = require('sprintf-js');"},{"note":"This is the explicit usage pattern demonstrated in the package's README, clearly indicating CommonJS named exports.","symbol":"sprintf, vsprintf (individual require)","correct":"var sprintf = require('sprintf-js').sprintf;\nvar vsprintf = require('sprintf-js').vsprintf;"}],"quickstart":{"code":"const { sprintf, vsprintf } = require('sprintf-js');\n\n// Basic string formatting with argument reordering\nconst formattedString1 = sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants');\nconsole.log(formattedString1); // Expected: \"Polly wants a cracker\"\n\n// Formatting with an array of arguments\nconst alphabetLetters = ['a', 'b', 'c', 'd'];\nconst formattedString2 = vsprintf('The first 4 letters of the English alphabet are: %s, %s, %s and %s', alphabetLetters);\nconsole.log(formattedString2); // Expected: \"The first 4 letters of the English alphabet are: a, b, c and d\"\n\n// Example with numeric formatting (padding and precision)\nconst pi = 3.1415926535;\nconst formattedPi = sprintf('Pi to 2 decimal places: %.2f, padded to 10 chars with zero: %010.2f', pi, pi);\nconsole.log(formattedPi); // Expected: \"Pi to 2 decimal places: 3.14, padded to 10 chars with zero: 0000003.14\"\n\n// Example with named arguments (available since a minor version, not explicitly in 1.1.3 README but a common feature)\nconst user = { name: 'Dolly' };\nconst namedArgString = sprintf('Hello %(name)s', user);\nconsole.log(namedArgString); // Expected: \"Hello Dolly\"","lang":"javascript","description":"Demonstrates basic string formatting with positional arguments, array-based formatting using `vsprintf`, numeric formatting with padding and precision, and an example of named arguments."},"warnings":[{"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.","message":"As 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.","severity":"gotcha","affected_versions":">=1.1.1"}],"env_vars":null,"search_vec":"'1.1.3':35 '2016':39 'align':52 'argument':49 'binari':59 'browser':26 'c':19 'c-style':18 'capabl':23 'choic':102 'complet':73 'comprehens':43 'consid':91 'convers':57 'current':31 'dedic':108 'depend':87 'differenti':70 'e.g':58 'engin':110 'environ':29 'featur':74 'format':22,46 'function':16,78 'hexadecim':61 'implement':10 'includ':48,71 'infrequ':96 'interpol':105 'javascript':9,113 'js':3,6 'json':65 'key':69 'last':36 'lightweight':81 'make':98 'matur':92 'natur':82 'node.js':28 'notabl':85 'notat':63 'objects/arrays':67 'octal':60 'overkil':112 'packag':89 'pad':51 'precis':53 'provid':17 'publish':37 'reliabl':101 'reorder':50 'runtim':86 'scientif':62 'set':44,75 'specifi':47 'sprintf':2,5,13,77 'sprintf-j':1,4 'stabl':32,94 'string':21,104 'style':20 'support':41 'templat':109 'type':56 'updat':97 'various':55 'version':33 'vsprintf':15","created_at":"2026-04-19T13:41:13.533773+00:00","updated_at":"2026-04-19T13:41:13.533773+00:00","problems":[{"fix":"Use CommonJS `require` syntax: `const { sprintf, vsprintf } = require('sprintf-js');` or `const sprintf = require('sprintf-js').sprintf;`","cause":"Attempting to use ES module `import` syntax in a CommonJS-only Node.js environment or without a bundler correctly transforming the import.","error":"TypeError: sprintf is not a function"},{"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.","cause":"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.","error":"Uncaught ReferenceError: Buffer is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/alexei/sprintf.js","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sprintf-js","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}