pretty-format2
raw JSON → 2.0.4 verified Sat Apr 25 auth: no javascript
pretty-format2 is a JavaScript library for stringifying any value into a human-readable format, designed to be faster and more feature-rich than alternatives like JSON.stringify, pretty-format, and util.inspect. Version 2.0.4 is a work-in-progress with no stable release yet; the package is experimental and subject to breaking changes. Key differentiators include support for plugins, custom indentation, color themes via ANSI escape codes, and a streaming API. It aims to be more performant than pretty-format and more customizable than util.inspect for both Node.js and browser environments.
Common errors
error TypeError: prettyFormat is not a function ↓
cause Using CommonJS require on an ESM-only package.
fix
Switch to ESM import: import { prettyFormat } from 'pretty-format2'
error SyntaxError: Unexpected token 'export' ↓
cause Using older Node.js version (<12) that doesn't support ESM syntax.
fix
Upgrade Node.js to version 12 or later, or use a transpiler like Babel.
error Module not found: Can't resolve 'ansi-styles' ↓
cause Optional dependency ansi-styles not installed but tried to use color theme.
fix
Install ansi-styles manually: npm install ansi-styles
error prettyFormat(...).highlight is not a function ↓
cause Passing options incorrectly: highlight should be a boolean, not invoked as a method.
fix
Use: prettyFormat(obj, { highlight: true })
Warnings
gotcha The package is marked as WIP and experimental; no stable release yet. Breaking changes may occur between minor versions. ↓
fix Pin to exact version and monitor releases for changes.
breaking Version 2.0.0 changed from a default export to named exports. CommonJS require broke. ↓
fix Use ESM import with named syntax: import { prettyFormat } from 'pretty-format2'.
deprecated The option 'colors' was renamed to 'theme' in version 2.0.0. The old name still works but will be removed in a future release. ↓
fix Replace `colors` with `theme` in the options object.
gotcha Highlight option only works in Node.js terminals that support ANSI escape codes. Browser output may show raw codes. ↓
fix Pre-check environment or provide a polyfill for ANSI sequences.
gotcha React element detection requires the optional dependency 'react-is'. If not installed, React elements will print as plain objects. ↓
fix Install react-is as a dependency: npm install react-is
Install
npm install pretty-format2 yarn add pretty-format2 pnpm add pretty-format2 Imports
- prettyFormat wrong
const prettyFormat = require('pretty-format2')correctimport { prettyFormat } from 'pretty-format2' - plugins wrong
import plugins from 'pretty-format2/plugins'correctimport { plugins } from 'pretty-format2' - PrettyFormatOptions wrong
import { PrettyFormatOptions } from 'pretty-format2'correctimport type { PrettyFormatOptions } from 'pretty-format2'
Quickstart
import { prettyFormat } from 'pretty-format2';
const obj = {
name: 'example',
nested: { a: 1, b: 2 },
date: new Date('2023-01-01'),
regex: /test/gi,
fn: function() { return true; },
[Symbol('id')]: 42,
};
const formatted = prettyFormat(obj, {
indent: 2,
printFunctionName: true,
highlight: true,
theme: {
key: 'yellow',
string: 'green',
number: 'blue',
boolean: 'magenta',
null: 'red',
undefined: 'grey',
},
});
console.log(formatted);