Registry / serialization / format

format

JSON →
library1.0jsnpmunverified

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.

npm install format
INSTALL
IMPORT
SIG · FORMAT
F
format
serializationjavascriptv1.0
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.

format
const format = require('format')
The 'format' module's default export is the sprintf function itself. It also exposes printf and vsprintf as properties.
printf
const { printf } = require('format')
import { printf } from 'format'
This package is CommonJS-only; direct ES module imports are not supported.
vsprintf
const vsprintf = require('format').vsprintf
import vsprintf from 'format/vsprintf'
vsprintf is a property of the main 'format' module. There is no separate path for it.

This quickstart demonstrates the core `sprintf`, `printf`, and `vsprintf` functions with various format specifiers and precision settings.

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));
Debug
Known issues
breakingThis 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).
fix
For Node.js ESM projects, you must use `const format = require('format');` inside an async function or top-level await block, or configure your bundler to handle CJS modules. Consider using a more modern string formatting library if ESM is a requirement.
affects: >=0.2.2
gotchaThe 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.
fix
Proceed with caution. If starting a new project, evaluate more actively maintained alternatives for string formatting. For legacy projects, ensure thorough testing in your target environment.
affects: >=0.2.2
gotchaThe `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.
fix
Always use the return value of `format` (or `sprintf`) or `vsprintf` if you need the formatted string for further processing. Only use `printf` when the sole intention is to output the string directly.
affects: >=0.2.2
Errors
Common errors & fixes
TypeError: require(...).printf is not a function
Attempting to destructure `printf` from the module when the module itself is typically the `sprintf` function.
fix
Access `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
Trying to use `require()` in a browser environment or in an ES module (ESM) context in Node.js without proper bundling or transpilation.
fix
If 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.
Upgrade
Version history
1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
format — npm install format · libregistry