Registry / serialization / prettyjson

prettyjson

JSON →
library1.0.3jsnpmunverified

prettyjson is a Node.js package designed to format JSON data into a human-readable, colored YAML-style output, primarily for command-line interface (CLI) applications. It offers both a programmatic API for Node.js scripts and a direct CLI tool for processing JSON files, `stdin`, or interactive input. The current stable version is 1.2.5. Release cadence appears sporadic, with the last update over a year ago, but still maintained with dependency fixes. Its key differentiator lies in its focus on visually appealing, colored output directly in the terminal, making debugging and data inspection easier compared to raw JSON. It allows extensive customization of colors and formatting options, including indentation and inline arrays. It depends on the `colors` library for terminal output styling.

npm install prettyjson
INSTALL
IMPORT
SIG · PRETTYJSON
P
prettyjson
serializationjavascriptv1.0.3
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.

prettyjson
const prettyjson = require('prettyjson');
import prettyjson from 'prettyjson';
This package primarily uses CommonJS `require`. ESM import is not officially supported or documented, and direct ESM usage may require transpilation or wrapper.
render
prettyjson.render(data, options);
require('prettyjson').render(data, options);
The `render` method is the primary API for formatting data. It is directly exposed as a property of the main export.

This quickstart demonstrates how to programmatically use `prettyjson` to render a complex JavaScript object into a colored, YAML-style string, customizing colors and indentation.

const prettyjson = require('prettyjson'); const complexData = { user: { id: 'usr_xyz123', name: 'Alice Wonderland', email: 'alice@example.com', isActive: true, roles: ['admin', 'editor'], lastLogin: new Date('2024-04-20T14:30:00Z'), preferences: { theme: 'dark', notifications: { email: true, sms: false } } }, posts: [ { id: 1, title: 'First Post', content: 'Lorem ipsum...', tags: ['js', 'cli'], published: true }, { id: 2, title: 'Second Post', content: 'Dolor sit amet...', tags: ['node', 'format'], published: false } ], config: { apiEndpoint: process.env.API_URL ?? 'https://api.example.com/v1', timeoutMs: 5000 } }; const options = { noColor: false, keysColor: 'magenta', dashColor: 'yellow', stringColor: 'green', multilineStringColor: 'cyan', inlineArrays: false, indent: 4 }; console.log(prettyjson.render(complexData, options));
prettyjson --version
Debug
Known issues
breakingConfiguration via environment variables was deprecated in v0.13.0 and was intended to be removed in v1.0.0. While the documentation still mentions CLI options, reliance on environment variables for configuration is highly discouraged and may lead to unexpected behavior or breakage in future minor or patch versions.
fix
Migrate all configuration from environment variables to CLI arguments or options passed directly to the `render` method.
affects: >=1.0.0
gotchaThe `colors.js` dependency, used for terminal output, has had historical issues with supply chain attacks (e.g., in early 2022). While `prettyjson` pins its `colors` dependency to `1.4.0` since v1.2.2, developers should be aware of the potential risks associated with this dependency.
fix
Ensure `colors` is pinned to a safe version (`1.4.0` or later stable versions if `prettyjson` updates) in your project's `package-lock.json` or `yarn.lock` files, and regularly audit your dependency tree for security vulnerabilities.
affects: all
gotchaThe package primarily targets CommonJS environments. Attempting to use `import prettyjson from 'prettyjson'` directly in an ESM module without a build step or specific Node.js configuration for CJS interoperability may result in module resolution errors.
fix
For CommonJS, use `const prettyjson = require('prettyjson');`. For ESM projects, consider using dynamic `import('prettyjson')` or a build tool to handle CJS module imports.
affects: all
Errors
Common errors & fixes
TypeError: prettyjson.render is not a function
Attempting to call `prettyjson.render` before `prettyjson` has been correctly required or when `prettyjson` is undefined.
fix
Ensure `const prettyjson = require('prettyjson');` is at the top of your file and `prettyjson` is accessible in the current scope.
Error: Cannot find module 'prettyjson'
The `prettyjson` package has not been installed or is not resolvable in the current project's `node_modules`.
fix
Run `npm install prettyjson` or `yarn add prettyjson` in your project directory.
Unknown option: --noalign
Using a CLI option that either doesn't exist or is misspelled.
fix
Verify the correct CLI option name. For example, the `noalign` option was introduced as `--noalign` in v1.2.0, but other options like `--nocolor` or `--inline-arrays` have specific spellings. Refer to the documentation for supported arguments.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
colorsrequiredProvides colorization for terminal output; historically a source of supply chain issues.
Agent activity
2 hits · last 30 days
node
2
Resources
prettyjson — npm install prettyjson · libregistry