{"id":10914,"library":"format","title":"C-style printf/sprintf for JavaScript","description":"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.","status":"abandoned","version":"0.2.2","language":"javascript","source_language":"en","source_url":"git://github.com/samsonjs/format","tags":["javascript"],"install":[{"cmd":"npm install format","lang":"bash","label":"npm"},{"cmd":"yarn add format","lang":"bash","label":"yarn"},{"cmd":"pnpm add format","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The 'format' module's default export is the sprintf function itself. It also exposes printf and vsprintf as properties.","symbol":"format","correct":"const format = require('format')"},{"note":"This package is CommonJS-only; direct ES module imports are not supported.","wrong":"import { printf } from 'format'","symbol":"printf","correct":"const { printf } = require('format')"},{"note":"vsprintf is a property of the main 'format' module. There is no separate path for it.","wrong":"import vsprintf from 'format/vsprintf'","symbol":"vsprintf","correct":"const vsprintf = require('format').vsprintf"}],"quickstart":{"code":"const format = require('format');\nconst printf = format.printf;\nconst vsprintf = format.vsprintf;\n\n// Using sprintf (which is the default export of the 'format' module)\nconst result1 = format('%d is the answer to %s', 42, 'life, the universe, and everything');\nconsole.log('sprintf example:', result1);\n\n// Using printf to log directly to console (adds a newline for convenience)\nconsole.log('\\nprintf example:');\nprintf('%s world\\n', 'hello');\n\n// Using vsprintf with an array of arguments\nconst what = 'life, the universe, and everything';\nconst result2 = vsprintf('%d is the answer to %s', [42, what]);\nconsole.log('\\nvsprintf example:', result2);\n\n// Demonstrating different format specifiers and precision\nconsole.log(format('Binary: %b, Char: %c, Decimal: %d, Float: %.2f, Octal: %o, String: %s, Hex (lower): %x, Hex (upper): %X', \n  10, 'A', 123, 3.14159, 10, 'test string', 255, 255));","lang":"javascript","description":"This quickstart demonstrates the core `sprintf`, `printf`, and `vsprintf` functions with various format specifiers and precision settings."},"warnings":[{"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.","message":"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).","severity":"breaking","affected_versions":">=0.2.2"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.2.2"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.2.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to destructure `printf` from the module when the module itself is typically the `sprintf` function.","error":"TypeError: require(...).printf is not a function"},{"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.","cause":"Trying to use `require()` in a browser environment or in an ES module (ESM) context in Node.js without proper bundling or transpilation.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}