CLI JSON Formatter
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.
Common errors
-
TypeError: prettyjson.render is not a function
cause Attempting to call `prettyjson.render` before `prettyjson` has been correctly required or when `prettyjson` is undefined.fixEnsure `const prettyjson = require('prettyjson');` is at the top of your file and `prettyjson` is accessible in the current scope. -
Error: Cannot find module 'prettyjson'
cause The `prettyjson` package has not been installed or is not resolvable in the current project's `node_modules`.fixRun `npm install prettyjson` or `yarn add prettyjson` in your project directory. -
Unknown option: --noalign
cause Using a CLI option that either doesn't exist or is misspelled.fixVerify 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.
Warnings
- breaking Configuration 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.
- gotcha The `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.
- gotcha The 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.
Install
-
npm install prettyjson -
yarn add prettyjson -
pnpm add prettyjson
Imports
- prettyjson
import prettyjson from 'prettyjson';
const prettyjson = require('prettyjson'); - render
require('prettyjson').render(data, options);prettyjson.render(data, options);
Quickstart
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));