prettier-plugin-sort-imports

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

A Prettier plugin that sorts import statements by line length or alphabetically. Currently at version 1.8.11 with monthly releases. It automatically loads into Prettier, sorting by line length by default. Differentiates itself from alternatives by offering line-length sorting (not just alphabetical) and import-type grouping (NPM vs local, type vs value). Requires TypeScript >4.0.0 as a peer dependency. Supports both Prettier 2 and 3 with separate entry points.

error Cannot read properties of undefined (reading 'sortingMethod')
cause Plugin not loaded or not properly configured in Prettier config
fix
Ensure prettier-plugin-sort-imports is installed and referenced in plugins array (for Prettier 3) or pluginSearchDirs (for Prettier 2)
error Error: Invalid importTypeOrder array: must contain either 'all' or a combination of 'NPMPackages', 'localImports', 'localImportsValue', 'localImportsType'
cause importTypeOrder contains incomplete or incompatible types
fix
Use only 'all' alone, or specify NPMPackages with one of the local import types (e.g., ['NPMPackages', 'localImports'])
error TypeError: Cannot read properties of undefined (reading 'some')
cause Prettier version mismatch (trying to use Prettier 3 entry point with Prettier 2)
fix
Use the correct plugin path: './node_modules/prettier-plugin-sort-imports/dist/index.js' for Prettier 3, './node_modules/prettier-plugin-sort-imports/dist/index.2.js' for Prettier 2
gotcha Plugin must be manually added to plugins array for Prettier 3; auto-discovery may not work
fix Add 'plugins: ["./node_modules/prettier-plugin-sort-imports/dist/index.js"]' to Prettier config
gotcha Cannot combine 'all' import type with other types in importTypeOrder
fix Use only 'all' alone, or specify an array without 'all' (e.g., ['NPMPackages', 'localImports'])
deprecated pluginSearchDirs option is deprecated in Prettier 3; use plugins array instead
fix Remove pluginSearchDirs and directly reference the plugin path in the plugins array
gotcha Line-length sorting uses the entire import statement length, not just the source path
fix Consider using alphabetical sorting if you want predictable order based on module name
gotcha Newlines between import groups are stripped when stripNewlines is true, which may break intentional grouping
fix Set stripNewlines to false if you want to preserve blank lines between import blocks
npm install prettier-plugin-sort-imports
yarn add prettier-plugin-sort-imports
pnpm add prettier-plugin-sort-imports

Demonstrates installation, configuration with import type ordering and line-length sorting, and the resulting import order change.

// Install: npm install --save-dev prettier prettier-plugin-sort-imports
// prettier.config.js
module.exports = {
  sortingMethod: 'lineLength',
  importTypeOrder: ['NPMPackages', 'localImports'],
  plugins: ['./node_modules/prettier-plugin-sort-imports/dist/index.js'],
};

// Before
import z from 'zoo';
import a from './local';
import b from 'bar';
import type { C } from './types';

// After formatting with Prettier
import b from 'bar';
import z from 'zoo';
import a from './local';
import type { C } from './types';