C-style printf/sprintf for JavaScript
The `format` package (current version 0.2.2) provides `printf`, `sprintf`, and `vsprintf` functions for JavaScript, emulating the string formatting capabilities found in the C standard library. It supports a range of format specifiers including `b`, `c`, `d`, `f`, `o`, `s`, `x`, and `X`, along with precision control for floating-point numbers. The package was last updated in 2014 and explicitly targets Node.js versions 0.4.x and above, as well as web browsers by direct script inclusion. Its core value proposition was to offer a familiar C-style formatting API in JavaScript environments. Due to its age and lack of maintenance over the last decade, it should be considered an abandoned project, potentially incompatible with modern JavaScript ecosystems and lacking critical updates for security or compatibility.
Common errors
-
TypeError: require(...).printf is not a function
cause Attempting to destructure `printf` from the module when the module itself is typically the `sprintf` function.fixAccess `printf` as a property of the main imported module: `const format = require('format'); const printf = format.printf;` or use `const { printf } = require('format');` which is also supported. -
ReferenceError: require is not defined
cause Trying to use `require()` in a browser environment or in an ES module (ESM) context in Node.js without proper bundling or transpilation.fixIf in a browser, ensure the script is loaded via `<script>` tag directly or bundled for browser usage. If in a Node.js ESM module, load it using a dynamic import or wrapper: `const format = await import('format')` (and access `format.default` for sprintf, and `format.printf` etc.) or use a build tool to transpile CJS to ESM.
Warnings
- breaking This package is explicitly CommonJS (CJS) only. It does not provide ES module (ESM) exports and cannot be directly imported using `import` statements in modern Node.js or browser ESM environments without a transpilation step (e.g., with Webpack or Rollup).
- gotcha The package has not been updated since 2014 (version 0.2.2). This means it likely lacks support for newer JavaScript features, may have unaddressed bugs or security vulnerabilities, and might be incompatible with modern Node.js versions or browser environments.
- gotcha The `printf` function directly logs to `process.stdout` in Node.js, and typically `console.log` in browser environments (though this is implementation-dependent). Unlike `sprintf` or `vsprintf`, it does not return a formatted string.
Install
-
npm install format -
yarn add format -
pnpm add format
Imports
- format
const format = require('format') - printf
import { printf } from 'format'const { printf } = require('format') - vsprintf
import vsprintf from 'format/vsprintf'
const vsprintf = require('format').vsprintf
Quickstart
const format = require('format');
const printf = format.printf;
const vsprintf = format.vsprintf;
// Using sprintf (which is the default export of the 'format' module)
const result1 = format('%d is the answer to %s', 42, 'life, the universe, and everything');
console.log('sprintf example:', result1);
// Using printf to log directly to console (adds a newline for convenience)
console.log('\nprintf example:');
printf('%s world\n', 'hello');
// Using vsprintf with an array of arguments
const what = 'life, the universe, and everything';
const result2 = vsprintf('%d is the answer to %s', [42, what]);
console.log('\nvsprintf example:', result2);
// Demonstrating different format specifiers and precision
console.log(format('Binary: %b, Char: %c, Decimal: %d, Float: %.2f, Octal: %o, String: %s, Hex (lower): %x, Hex (upper): %X',
10, 'A', 123, 3.14159, 10, 'test string', 255, 255));