prettier-plugin-multiline-arrays

raw JSON →
4.1.7 verified Sat Apr 25 auth: no javascript

Prettier plugin that forces array elements to wrap onto new lines, with control over how many elements appear per line via `multilineArraysWrapThreshold` (default -1, no auto wrap) and `multilineArraysLinePattern` (default '1', one element per line). TypeScript, JavaScript, and JSON supported. Current stable version 4.1.7, requires Prettier >=3.0.0 <4.0.0 and Node >=20. Ships TypeScript types. Differentiated by comment overrides per array and set/reset patterns. Maintained by electrovir.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/prettier-plugin-multiline-arrays/dist/index.js from /path/to/not-supported.js not supported.
cause The package is ESM-only from v4, but you are using require() in a CommonJS context.
fix
Switch to ESM (use import, add 'type': 'module' in package.json) or use dynamic import() in CommonJS: const plugin = await import('prettier-plugin-multiline-arrays');
error [prettier] Cannot find module 'prettier-plugin-multiline-arrays'
cause Plugin not installed or not in the correct node_modules path.
fix
Ensure the plugin is installed: npm install --save-dev prettier-plugin-multiline-arrays. If using a monorepo, ensure it's in the root or hoisted.
error TypeError: Cannot read properties of undefined (reading 'length')
cause Incompatible Prettier version - this plugin requires Prettier 3.x.
fix
Upgrade Prettier to ^3.0.0 and node to >=20 as required by the plugin's engine.
breaking v4.0.0 dropped support for Prettier 2.x and earlier. Requires Prettier >=3.0.0.
fix Upgrade Prettier to ^3.0.0 and ensure Node >=16 (v4.1.7 requires Node >=20).
breaking v4.0.0 switched to ESM-only. CommonJS require() will fail.
fix Use ESM imports or dynamic import(). If you cannot switch to ESM, stay on v3.x.
gotcha Plugin order among multiple plugins can affect behavior. The documentation recommends rearranging if other plugins interfere.
fix Place 'prettier-plugin-multiline-arrays' before other plugins that modify formatting of arrays. Check the plugin's own Prettier config for reference.
gotcha The `multilineArraysLinePattern` option overrides Prettier's default line-length-based wrapping for arrays. Arrays may exceed the column limit if the pattern forces more items per line than can fit.
fix If you need line-length enforcement, set a pattern that fits within your desired column width, or rely on Prettier's default wrapping by not setting this option.
gotcha Comment overrides (e.g., `// prettier-multiline-arrays-next-threshold: 4`) apply to the next line only. For persistent overrides, use `set-` variants.
fix Use `// prettier-multiline-arrays-set-threshold: 4` to apply to all subsequent arrays, then `// prettier-multiline-arrays-reset` to revert.
npm install prettier-plugin-multiline-arrays
yarn add prettier-plugin-multiline-arrays
pnpm add prettier-plugin-multiline-arrays

Install and configure the plugin, then run Prettier to see multiline array formatting.

// Install: npm install --save-dev prettier prettier-plugin-multiline-arrays

// prettier.config.mjs
export default {
  plugins: ['prettier-plugin-multiline-arrays'],
  multilineArraysWrapThreshold: 2, // wrap arrays with >2 elements
  multilineArraysLinePattern: '2 1', // first line 2 items, second line 1, repeat
};

// Example input (before formatting):
const arr = ['a', 'b', 'c', 'd'];

// After prettier (with above config):
const arr = [
  'a', 'b',
  'c', 'd',
];