{"id":13596798,"url":"https://github.com/trivago/prettier-plugin-sort-imports","last_synced_at":"2026-01-07T10:20:15.407Z","repository":{"id":37650567,"uuid":"281063315","full_name":"trivago/prettier-plugin-sort-imports","owner":"trivago","description":"A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order. ","archived":false,"fork":false,"pushed_at":"2025-02-06T10:09:08.000Z","size":2183,"stargazers_count":3629,"open_issues_count":84,"forks_count":142,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-07T07:04:48.617Z","etag":null,"topics":["hacktoberfest","import","javascript","opensource","plugin","prettier","prettierrc","sort","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trivago.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-20T08:44:42.000Z","updated_at":"2025-05-06T09:43:53.000Z","dependencies_parsed_at":"2023-09-29T12:47:40.470Z","dependency_job_id":"3c22925b-d697-4f4d-b77d-bab32fc3d833","html_url":"https://github.com/trivago/prettier-plugin-sort-imports","commit_stats":{"total_commits":214,"total_committers":36,"mean_commits":5.944444444444445,"dds":0.5700934579439252,"last_synced_commit":"61d069711008c530f5a41ca4e254781abc5de358"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trivago%2Fprettier-plugin-sort-imports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trivago%2Fprettier-plugin-sort-imports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trivago%2Fprettier-plugin-sort-imports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trivago%2Fprettier-plugin-sort-imports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trivago","download_url":"https://codeload.github.com/trivago/prettier-plugin-sort-imports/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101586,"owners_count":22014907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","import","javascript","opensource","plugin","prettier","prettierrc","sort","typescript"],"created_at":"2024-08-01T16:02:48.345Z","updated_at":"2026-01-07T10:20:15.401Z","avatar_url":"https://github.com/trivago.png","language":"TypeScript","funding_links":[],"categories":["Uncategorized","TypeScript","Protips for the fastest Developer Experience","typescript"],"sub_categories":["Uncategorized"],"readme":"# Prettier plugin sort imports\n\nA prettier plugin to sort import declarations by provided Regular Expression order.\n\n**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**\n\n### Input\n\n```javascript\nimport React, {\n    FC,\n    useEffect,\n    useRef,\n    ChangeEvent,\n    KeyboardEvent,\n} from 'react';\nimport { logger } from '@core/logger';\nimport { reduce, debounce } from 'lodash-es';\nimport { Message } from '../Message';\nimport { createServer } from '@server/node';\nimport { Alert } from '@ui/Alert';\nimport { repeat, filter, add } from '../utils';\nimport { initializeApp } from '@core/app';\nimport { Popup } from '@ui/Popup';\nimport { createConnection } from '@server/database';\n```\n\n\n### Output\n\n```javascript\nimport { debounce, reduce } from 'lodash-es';\nimport React, {\n    ChangeEvent,\n    FC,\n    KeyboardEvent,\n    useEffect,\n    useRef,\n} from 'react';\n\nimport { createConnection } from '@server/database';\nimport { createServer } from '@server/node';\n\nimport { initializeApp } from '@core/app';\nimport { logger } from '@core/logger';\n\nimport { Alert } from '@ui/Alert';\nimport { Popup } from '@ui/Popup';\n\nimport { Message } from '../Message';\nimport { add, filter, repeat } from '../utils';\n```\n\n### Install\n\nusing npm\n\n```shell script\nnpm install --save-dev @trivago/prettier-plugin-sort-imports\n```\n\nusing yarn\n\n```shell script\nyarn add --dev @trivago/prettier-plugin-sort-imports\n```\n\nusing pnpm\n\n```shell script\npnpm add -D @trivago/prettier-plugin-sort-imports\n```\n\n**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**\n\n**Note: If formatting `.vue` sfc files please install `@vue/compiler-sfc` if not in your dependency tree - this normally is within Vue projects.**\n\n### Usage\n\nAdd an order in prettier config file.\n\n```ecmascript 6\nmodule.exports = {\n  \"printWidth\": 80,\n  \"tabWidth\": 4,\n  \"trailingComma\": \"all\",\n  \"singleQuote\": true,\n  \"semi\": true,\n  \"importOrder\": [\"^@core/(.*)$\", \"^@server/(.*)$\", \"^@ui/(.*)$\", \"^[./]\"],\n  \"importOrderSeparation\": true,\n  \"importOrderSortSpecifiers\": true\n}\n```\n\n**Note: There may be an issue with some package managers, such as `pnpm` or when using `prettier` v3.x. You can solve it by providing additional configuration option in prettier config file.\n\n```js\nmodule.exports = {\n    ...\n    \"plugins\": [\"@trivago/prettier-plugin-sort-imports\"]\n}\n```\n\n### APIs\n\n#### **`importOrder`**\n\n**type**: `Array\u003cstring\u003e`\n\nA collection of Regular expressions in string format.\n\n```\n\"importOrder\": [\"^@core/(.*)$\", \"^@server/(.*)$\", \"^@ui/(.*)$\", \"^[./]\"],\n```\n\n_Default behavior:_ The plugin moves the third party imports to the top which are not part of the `importOrder` list.\nTo move the third party imports at desired place, you can use `\u003cTHIRD_PARTY_MODULES\u003e` to assign third party imports to the appropriate position:\n\n```\n\"importOrder\": [\"^@core/(.*)$\", \"\u003cTHIRD_PARTY_MODULES\u003e\", \"^@server/(.*)$\", \"^@ui/(.*)$\", \"^[./]\"],\n```\n\nYou can also use `\u003cBUILTIN_MODULES\u003e` to control the position of Node.js builtin modules (like `fs`, `path`, `http`, and their `node:` prefixed variants):\n\n```\n\"importOrder\": [\"\u003cBUILTIN_MODULES\u003e\", \"\u003cTHIRD_PARTY_MODULES\u003e\", \"^@core/(.*)$\", \"^@server/(.*)$\", \"^@ui/(.*)$\", \"^[./]\"],\n```\n\nWhen `\u003cBUILTIN_MODULES\u003e` is included in your `importOrder`, Node.js builtin modules will be sorted to that position. If not included, builtin modules are treated as regular third-party imports.\n\n#### `importOrderSeparation`\n\n**type**: `boolean`\n\n**default value**: `false`\n\nA boolean value to enable or disable the new line separation\nbetween sorted import declarations group. The separation takes place according to the `importOrder`.\n\n```\n\"importOrderSeparation\": true,\n```\n\nIf this option is enabled and `\u003cSEPARATOR\u003e` is used in the `importOrder` array, the plugin \nwill ONLY add newlines at those locations and at the end of the imports.\n\n#### `importOrderSortSpecifiers`\n\n**type**: `boolean`\n\n**default value:** `false`\n\nA boolean value to enable or disable sorting of the specifiers in an import declarations.\n\n#### `importOrderGroupNamespaceSpecifiers`\n\n**type**: `boolean`\n\n**default value:** `false`\n\nA boolean value to enable or disable sorting the namespace specifiers to the top of the import group.\n\n#### `importOrderCaseInsensitive`\n\n**type**: `boolean`\n\n**default value**: `false`\n\nA boolean value to enable case-insensitivity in the sorting algorithm\nused to order imports within each match group.\n\nFor example, when false (or not specified):\n\n```ecmascript 6\nimport ExampleView from './ExampleView';\nimport ExamplesList from './ExamplesList';\n```\n\ncompared with `\"importOrderCaseInsensitive\": true`:\n\n```ecmascript 6\nimport ExamplesList from './ExamplesList';\nimport ExampleView from './ExampleView';\n```\n\n#### `importOrderParserPlugins`\n\n**type**: `Array\u003cstring\u003e`\n\n**default value**: `[\"typescript\", \"jsx\"]`\n\nPreviously known as `experimentalBabelParserPluginsList`.\n\nA collection of plugins for babel parser. The plugin passes this list to babel parser, so it can understand the syntaxes\nused in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails,\nyou can use this field to enforce the usage of the plugins' babel parser needs.\n\n**To pass the plugins to babel parser**:\n\n```\n  \"importOrderParserPlugins\" : [\"classProperties\", \"decorators-legacy\"]\n```\n\n**To pass the options to the babel parser plugins**: Since prettier options are limited to string, you can pass plugins\nwith options as a JSON string of the plugin array:\n`\"[\\\"plugin-name\\\", { \\\"pluginOption\\\": true }]\"`.\n\n```\n  \"importOrderParserPlugins\" : [\"classProperties\", \"[\\\"decorators\\\", { \\\"decoratorsBeforeExport\\\": true }]\"]\n```\n\n**To disable default plugins for babel parser, pass an empty array**:\n\n```\nimportOrderParserPlugins: []\n```\n\n### `importOrderSortByLength`\n**type**: `'asc' | 'desc' | null`\n**default value**: `null`\n\nA choice value to enable sorting imports within their groups based on their string lengths, the two options being ascending and descending.\nLeaving the value blank or setting it to null will result in length being ignored\n\n### `importOrderSideEffects`\n**type**: `boolean`\n**default value**: `true`\n\nBy default, the plugin sorts [side effect imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#import_a_module_for_its_side_effects_only) like any other imports in the file. If you need to keep side effect imports in the same place but sort all other imports around them, set this option to false.\n\nExample:\n\nInitial file:\n\n```js\nimport z from 'z'\nimport a from 'a'\n\nimport 'side-effect-lib'\n\nimport c from 'c'\nimport b from 'b'\n```\nWhen sorted:\n\n```js\nimport a from 'a'\nimport z from 'z'\n\nimport 'side-effect-lib'\n\nimport b from 'b'\nimport c from 'c'\n```\n\n\n### Ignoring import ordering\n\nIn some cases it's desired to ignore import ordering, specifically if you require to instantiate a common service or polyfill in your application logic before all the other imports. The plugin supports the `// sort-imports-ignore` comment, which will exclude the file from ordering the imports.\n\n```javascript\n// sort-imports-ignore\nimport './polyfills';\n\nimport foo from 'foo'\n```\n\n#### `importOrderImportAttributesKeyword`\n\n**type**: `'assert' | 'with' | 'with-legacy'`\n\nThe import attributes/assertions syntax: \n- `with`: `import \"...\" with { type: \"json\" }`\n- `assert`: `import \"...\" assert { type: \"json\" }`\n- `with-legacy`: `import \"...\" with type: \"json\"`.\n\n```json\n  \"importOrderImportAttributesKeyword\": 'with'\n```\n\n_Default behavior:_ When not specified, @babel/generator will try to match the style in the input code based on the AST shape.\n\n\n### How does import sort work ?\n\nThe plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.\nThe imports which are not part of the `importOrder` is considered as _third party imports_.\n\nAfter, the plugin sorts the _local imports_ and _third party imports_ using [natural sort algorithm](https://en.wikipedia.org/wiki/Natural_sort_order).\n\nIn the end, the plugin returns final imports with _third party imports_ on top and _local imports_ at the end.\n\nThe _third party imports_ position (it's top by default) can be overridden using the `\u003cTHIRD_PARTY_MODULES\u003e` special word in the `importOrder`.\n\n### Pattern Matching Implementation\n\nThis plugin uses [minimatch](https://github.com/isaacs/minimatch) for pattern matching of import paths. The matching is performed using the exact version specified in the plugin's dependencies to ensure consistent behavior. This is important to note because different versions of minimatch or other glob matching libraries might have subtle differences in their pattern matching behavior.\n\nIf you're experiencing unexpected matching behavior, please ensure you're using patterns compatible with minimatch's syntax, which might differ slightly from other glob implementations.\n\n### FAQ / Troubleshooting\n\nHaving some trouble or an issue ? You can check [FAQ / Troubleshooting section](./docs/TROUBLESHOOTING.md).\n\n### Compatibility\n\n| Framework              | Supported                | Note                                             |\n| ---------------------- | ------------------------ | ------------------------------------------------ |\n| JS with ES Modules     | ✅ Everything            | -                                                |\n| NodeJS with ES Modules | ✅ Everything            | -                                                |\n| Angular                | ✅ Everything            | Supported through `importOrderParserPlugins` API |\n| Ember                  | ✅ Everything            | `prettier-plugin-ember-template-tag` is required |\n| React                  | ✅ Everything            | -                                                |\n| Solid                  | ✅ Everything            | -                                                |\n| Svelte                 | ✅ Everything            | `prettier-plugin-svelte` is required             |\n| Vue                    | ✅ Everything            | `@vue/compiler-sfc` is required                  |\n\n\n### Used by\n\nWant to highlight your project or company ? Adding your project / company name will help plugin to gain attraction and contribution.\nFeel free to make a Pull Request to add your project / company name.\n\n-   [trivago](https://company.trivago.com)\n-   [AuresKonnect](https://aures.com)\n-   [FactorialHR](https://factorialhr.com)\n\n### Contribution\n\nFor more information regarding contribution, please check the [Contributing Guidelines](./CONTRIBUTING.md). If you are trying to\ndebug some code in the plugin, check [Debugging Guidelines](./docs/DEBUG.md)\n\n### Maintainers\n\n| [Ayush Sharma](https://github.com/ayusharma)                             | [Behrang Yarahmadi](https://github.com/byara)                         |\n| ------------------------------------------------------------------------ | --------------------------------------------------------------------- |\n| ![ayusharma](https://avatars2.githubusercontent.com/u/6918450?s=120\u0026v=4) | ![@byara](https://avatars2.githubusercontent.com/u/6979966?s=120\u0026v=4) |\n| [@ayusharma\\_](https://twitter.com/ayusharma_)                           | [@behrang_y](https://twitter.com/behrang_y)                           |\n\n### Disclaimer\n\nThis plugin modifies the AST which is against the rules of prettier.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrivago%2Fprettier-plugin-sort-imports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrivago%2Fprettier-plugin-sort-imports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrivago%2Fprettier-plugin-sort-imports/lists"}