Prettier Plugin Expand JSON
raw JSON → 1.0.4 verified Sat Apr 25 auth: no javascript
Prettier plugin that expands all JSON arrays and objects into multi-line format, ensuring consistent formatting for JSON and JSONC files. Current stable version 1.0.4, released with no dependencies. Engine requires Node >=20. Unlike default Prettier behavior which may collapse short objects, this plugin always forces multi-line output. Works with special files like package.json and composer.json, and can be combined with other JSON plugins (must be listed last).
Common errors
error Cannot find module 'prettier-plugin-expand-json' ↓
cause Plugin not installed or not in node_modules, or Prettier cannot locate it.
fix
Run
npm install --save-dev prettier-plugin-expand-json and ensure your working directory contains node_modules. error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/prettier-plugin-expand-json/index.js from /path/to/prettier/index.js not supported. ↓
cause Prettier is loading the plugin via require(), but the plugin is ESM-only (since v1). This happens if Prettier version is <3 (which used CJS).
fix
Upgrade Prettier to version 3 or later:
npm install --save-dev prettier@3. error [error] prettier-plugin-expand-json: No parser found for file extension: .json ↓
cause Prettier is not configured to use JSON parser for .json files, or another plugin overrides the parser.
fix
Ensure Prettier config includes
"plugins": ["prettier-plugin-expand-json"] and that the plugin is listed last if multiple plugins are used. Warnings
gotcha Plugin must be listed last in the plugins array when combined with other JSON plugins (e.g., prettier-plugin-sort-json, prettier-plugin-pkg). If not, other plugins may override the formatting. ↓
fix Ensure 'prettier-plugin-expand-json' is the last element in the 'plugins' array in your Prettier config.
breaking Minimum Node engine is >=20. Using older Node versions will cause errors. ↓
fix Upgrade to Node 20 or later.
gotcha Plugin does not provide any options; it forces expansion of all arrays and objects. If you need selective expansion, consider other tools. ↓
fix No workaround; designed to always expand.
Install
npm install prettier-plugin-expand-json yarn add prettier-plugin-expand-json pnpm add prettier-plugin-expand-json Imports
- default wrong
const plugin = require('prettier-plugin-expand-json')correctin .prettierrc: { "plugins": ["prettier-plugin-expand-json"] }
Quickstart
npm install --save-dev prettier prettier-plugin-expand-json
echo '{"plugins":["prettier-plugin-expand-json"]}' > .prettierrc
echo '{"a":1,"b":[2,3]}' > test.json
npx prettier --write test.json
cat test.json
# Output:
# {
# "a": 1,
# "b": [
# 2,
# 3
# ]
# }