eslint-plugin-sort-keys
raw JSON → 2.3.5 verified Sat Apr 25 auth: no javascript
Autofix-enabled fork of ESLint's built-in sort-keys rule. Current stable version is 2.3.5. It automatically sorts object keys alphabetically with an autofix, unlike the core rule which only warns. It also moves associated comments with properties, supports minKeys threshold, and is intended for ESLint flat config. The package is maintained, with bug fixes released periodically.
Common errors
error ESLint couldn't find the plugin "sort-keys". ↓
cause Plugin not installed or incorrectly referenced in config.
fix
Run npm install --save-dev eslint-plugin-sort-keys and verify package.json includes it.
error Configuration for rule "sort-keys/sort-keys-fix" is invalid. ↓
cause Rule name or options are not correct.
fix
Use 'sort-keys/sort-keys-fix': 'warn' (string severity). Options object is supported but optional.
error TypeError: Cannot read properties of undefined (reading 'getSourceCode') ↓
cause Plugin version incompatible with ESLint version.
fix
Use ESLint >=8 and plugin >=2.3.0. For ESLint 9 flat config, ensure flat config usage as shown.
Warnings
gotcha Rule name is 'sort-keys-fix', not 'sort-keys'. Using 'sort-keys' in rules will apply the core rule without autofix. ↓
fix Use 'sort-keys/sort-keys-fix' as the rule name.
gotcha Flat config requires plugins to be an object, not an array. ↓
fix Use plugins: { 'sort-keys': require('eslint-plugin-sort-keys') } in eslint.config.js.
deprecated The plugin is a fork of leo-buneev/eslint-plugin-sort-keys-fix which is no longer maintained. ↓
fix This fork is still active; however, consider migrating to ESLint's built-in sort-keys if autofix is not critical.
gotcha Multiple runs may be needed to fully sort if comments or nested objects are involved. ↓
fix Run ESLint with --fix multiple times or ensure you are using v2.3.5+ which improves comment handling.
Install
npm install eslint-plugin-sort-keys yarn add eslint-plugin-sort-keys pnpm add eslint-plugin-sort-keys Imports
- default wrong
import sortKeysPlugin from 'eslint-plugin-sort-keys'correctconst sortKeysPlugin = require('eslint-plugin-sort-keys') - sort-keys-fix rule wrong
rules: { 'sort-keys': 'warn' }correctplugins: ['sort-keys'], rules: { 'sort-keys/sort-keys-fix': 'warn' } - flat config (eslint.config.js) wrong
plugins: ['sort-keys']correctplugins: { 'sort-keys': require('eslint-plugin-sort-keys') }
Quickstart
// Install: npm install --save-dev eslint eslint-plugin-sort-keys
// File: eslint.config.js
module.exports = {
plugins: ['sort-keys'],
rules: {
'sort-keys': 0, // disable core sort-keys to avoid duplication
'sort-keys/sort-keys-fix': 'warn', // or 'error' to enforce
},
};
// Example object that will be autofixed:
// { c: 1, a: 2, b: 3 } -> { a: 2, b: 3, c: 1 }