prettier-plugin-sort

raw JSON →
0.1.0 verified Fri May 01 auth: no javascript

An all-in-one Prettier plugin for sorting imports, package.json keys, and export statements. Version 0.1.0 is current (May 2025), with a rapid release cadence focused on stability and performance. Key differentiators: zero runtime dependencies, TypeScript support, and a modular approach inspired by eslint-plugin-import/order. Supports import attributes (ES2023), type import styling options (separate, inline-first, inline-last, mixed), and respects side-effect imports as chunk boundaries. Ships type definitions. Peer dependency on Prettier >=3.0.0.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/prettier-plugin-sort/dist/index.js from /path/to/.prettierrc.js not supported.
cause prettier-plugin-sort is ESM-only; cannot be required from a CommonJS config file.
fix
Rename .prettierrc.js to .prettierrc.mjs, or use a JSON config file.
error SyntaxError: Unexpected token 'export'
cause Prettier version <3.0.0 does not support the plugin API used by prettier-plugin-sort.
fix
Upgrade Prettier to >=3.0.0: npm i -D prettier@latest
error Cannot read properties of undefined (reading 'sort')
cause The sort function is not found because the import path is wrong or the plugin is not properly loaded.
fix
Ensure 'prettier-plugin-sort' is listed in the plugins array of your Prettier config. Do not import it directly unless using programmatic API.
error TypeError: Cannot set property 'importOrderSeparation' of undefined
cause The plugin options are being set before the plugins array is defined in the config.
fix
Move the 'plugins' array to the top level of your Prettier config, or use a valid JSON config format.
breaking In v0.1.0, side-effect imports are no longer moved; they act as chunk boundaries. Previously they would be reordered along with other imports.
fix If your code relies on side-effect imports being sorted, set importOrderSideEffects: "move" (not yet implemented). Otherwise, no action needed.
breaking In v0.0.3, the plugin gained support for ES2023 import attributes (with { type: 'json' }). Older versions silently dropped such attributes.
fix Upgrade to v0.0.3 or later to preserve import attributes.
gotcha The internal pattern detection uses hard-coded prefixes (/ , ~ , @/). It does not read tsconfig paths or bundler aliases.
fix Use one of the recognized prefixes for internal modules, or wait for custom pattern support (future release).
gotcha CommonJS require() will throw an error because the package is ESM-only.
fix Use dynamic import or convert your project to ESM. If using CommonJS, use await import('prettier-plugin-sort').
npm install prettier-plugin-sort
yarn add prettier-plugin-sort
pnpm add prettier-plugin-sort

Demonstrates installation, Prettier config activation, default import sorting behavior, and CLI usage.

// Install: npm i -D prettier prettier-plugin-sort

// .prettierrc
{
  "plugins": ["prettier-plugin-sort"],
  "importOrderGroups": ["builtin", "external", "parent", "sibling", "index"],
  "importOrderSeparation": true,
  "importOrderTypeImports": "separate",
  "importOrderMergeDuplicates": true
}

// Example: sort imports in a JS/TS file
// Input:
// import App from './App.tsx';
// import fs from 'node:fs';
// import lodash from 'lodash';
// import path from 'node:path';
// import react from 'react';
// Run: npx prettier --write file.ts
// Output:
// import fs from 'node:fs';
// import path from 'node:path';
// 
// import lodash from 'lodash';
// import react from 'react';
// 
// import App from './App.tsx';