Prettier Plugin: Sort Imports (by length or alphabetically)
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
A Prettier plugin that automatically sorts import statements by line length or alphabetically. V1.7.0 active, released for Prettier 2.x+ (Node.js 12+). Differentiators: supports sorting by import line length (unique feature), flexible import type ordering (NPM packages vs local values vs local types), newline stripping between groups, and ignore comments. Competitors like @trivago/prettier-plugin-sort-imports focus on alphabetical/group-based sorting and do not offer length-based sorting.
Common errors
error Cannot find module 'prettier-plugin-sort-imports' ↓
cause Plugin not installed or not in node_modules.
fix
Run
npm install --save-dev prettier-plugin-sort-imports to install the plugin. error Unknown option: sortingMethod ↓
cause Prettier version <2.0.0 does not support plugin options properly.
fix
Upgrade Prettier to 2.0.0+ or use a .prettierrc.js file with
plugins: [require.resolve('prettier-plugin-sort-imports')]. error Invalid sorting method: 'alphabetical' is not a valid option ↓
cause Typo in option value (e.g., 'alphabetical' instead of 'alphabetical').
fix
Check the spelling: use
"sortingMethod": "alphabetical" (correct spelling: alphabetical). error Error: importTypeOrder must contain either 'all' or a combination of 'NPMPackages', 'localImportsValue', 'localImportsType'. ↓
cause Invalid combination of import types in `importTypeOrder`.
fix
Use one of:
['all'], ['NPMPackages', 'localImports'], or ['NPMPackages', 'localImportsValue', 'localImportsType']. Warnings
gotcha Alphabetical sorting sorts by the entire import path, so `../` imports may rank lower than expected. ↓
fix Use `'lineLength'` sorting method if you want a different order.
deprecated The option `localImports` in `importTypeOrder` is deprecated in favor of using both `localImportsValue` and `localImportsType`. ↓
fix Use `['NPMPackages', 'localImportsValue', 'localImportsType']` instead of `['NPMPackages', 'localImports']`.
breaking Plugin no longer works with Prettier <2.0.0. ↓
fix Upgrade Prettier to 2.0.0 or later.
gotcha The `packageJSONFiles` option is only used when `importTypeOrder` includes `'NPMPackages'`. Setting it with `['all']` has no effect. ↓
fix Only define `packageJSONFiles` if you are using `NPMPackages` in `importTypeOrder`.
gotcha Comments between imports may be moved if `stripNewlines` is true. The comment sticks to the import above it initially. ↓
fix Use `// sort-imports-begin-ignore` and `// sort-imports-end-ignore` to preserve comments in a block.
Install
npm install prettier-plugin-sort-imports-desc yarn add prettier-plugin-sort-imports-desc pnpm add prettier-plugin-sort-imports-desc Imports
- none (plugin autoloads) wrong
N/Acorrect// Add to .prettierrc: { "plugins": ["prettier-plugin-sort-imports"] }
Quickstart
npm install --save-dev prettier-plugin-sort-imports
# Create .prettierrc with:
{
"plugins": ["prettier-plugin-sort-imports"],
"sortingMethod": "lineLength",
"stripNewlines": true,
"importTypeOrder": ["NPMPackages", "localImports"],
"newlineBetweenTypes": true
}