{"id":17049,"library":"prettyjson","title":"CLI JSON Formatter","description":"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.","status":"active","version":"1.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/rafeca/prettyjson","tags":["javascript","json","cli","formatting","colors"],"install":[{"cmd":"npm install prettyjson","lang":"bash","label":"npm"},{"cmd":"yarn add prettyjson","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettyjson","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides colorization for terminal output; historically a source of supply chain issues.","package":"colors","optional":false}],"imports":[{"note":"This package primarily uses CommonJS `require`. ESM import is not officially supported or documented, and direct ESM usage may require transpilation or wrapper.","wrong":"import prettyjson from 'prettyjson';","symbol":"prettyjson","correct":"const prettyjson = require('prettyjson');"},{"note":"The `render` method is the primary API for formatting data. It is directly exposed as a property of the main export.","wrong":"require('prettyjson').render(data, options);","symbol":"render","correct":"prettyjson.render(data, options);"}],"quickstart":{"code":"const prettyjson = require('prettyjson');\n\nconst complexData = {\n  user: {\n    id: 'usr_xyz123',\n    name: 'Alice Wonderland',\n    email: 'alice@example.com',\n    isActive: true,\n    roles: ['admin', 'editor'],\n    lastLogin: new Date('2024-04-20T14:30:00Z'),\n    preferences: {\n      theme: 'dark',\n      notifications: {\n        email: true,\n        sms: false\n      }\n    }\n  },\n  posts: [\n    { id: 1, title: 'First Post', content: 'Lorem ipsum...', tags: ['js', 'cli'], published: true },\n    { id: 2, title: 'Second Post', content: 'Dolor sit amet...', tags: ['node', 'format'], published: false }\n  ],\n  config: {\n    apiEndpoint: process.env.API_URL ?? 'https://api.example.com/v1',\n    timeoutMs: 5000\n  }\n};\n\nconst options = {\n  noColor: false,\n  keysColor: 'magenta',\n  dashColor: 'yellow',\n  stringColor: 'green',\n  multilineStringColor: 'cyan',\n  inlineArrays: false,\n  indent: 4\n};\n\nconsole.log(prettyjson.render(complexData, options));","lang":"javascript","description":"This quickstart demonstrates how to programmatically use `prettyjson` to render a complex JavaScript object into a colored, YAML-style string, customizing colors and indentation."},"warnings":[{"fix":"Migrate all configuration from environment variables to CLI arguments or options passed directly to the `render` method.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"all"},{"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.","message":"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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure `const prettyjson = require('prettyjson');` is at the top of your file and `prettyjson` is accessible in the current scope.","cause":"Attempting to call `prettyjson.render` before `prettyjson` has been correctly required or when `prettyjson` is undefined.","error":"TypeError: prettyjson.render is not a function"},{"fix":"Run `npm install prettyjson` or `yarn add prettyjson` in your project directory.","cause":"The `prettyjson` package has not been installed or is not resolvable in the current project's `node_modules`.","error":"Error: Cannot find module 'prettyjson'"},{"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.","cause":"Using a CLI option that either doesn't exist or is misspelled.","error":"Unknown option: --noalign"}],"ecosystem":"npm","meta_description":null}