How to quickly sort imports with Prettier
Diego (Relatable Code)

Diego (Relatable Code) @diballesteros

About: 🚀 JS/React/Typescript Developer 🛠️ Self-taught developer New articles every Monday Let's connect on Twitter https://twitter.com/relatablecoder

Location:
Bogota D.C., Colombia
Joined:
Aug 22, 2021

How to quickly sort imports with Prettier

Publish Date: Mar 30 '22
79 5

Introduction

Having disorganized imports bothers me deeply when working on a project. I typically like to keep the entire codebase as organized as possible as it makes sifting through the files much easier. With the help of prettier and a plugin, we can easily sort imports!

As a side note if you’re using ESLint I have another article to sort imports using that.

Sort Imports: Setting everything up

First, let’s install the necessary dependencies. We’ll need just two: prettier and the plugin 📦.



npm install prettier @trivago/prettier-plugin-sort-imports --save-dev


Enter fullscreen mode Exit fullscreen mode

Sort Imports: Configuring the Rules

Now we can go ahead and start configuring our rules. This plugin receives an array of strings. It uses these strings to decide the order of our imports!

So for example in my small sample project I have the following files:

unsorted imports

So we’ll have to set up the rules to configure them. I typically like the following order:

  1. Package/third-party imports
  2. Project imports
  3. Relative imports

This will cover mostly everything! So let’s create a .prettierrc (a prettier configuration file) at the route of our project.

prettierrc file

Inside that file add the following rule:



{ 
    "importOrder": ["^components/(.*)$", "^[./]" ],
    "importOrderSeparation": true, 
    "importOrderSortSpecifiers": true 
}


Enter fullscreen mode Exit fullscreen mode

Let’s break down these options. They’re parsed through regex (yes I know the devil’s tongue). But it’s roughly the same format for every import type.

First, any type of regex that isn’t included will be sent to the top. This is crucial. As the unincluded third-party dependencies will just go to the top as we want. Afterward, we follow up without aliased components folder, and then our relative imports. I also have two additional rules to add linebreaks between the import groups and to sort specifiers in an import declaration

In this GitHub repo, you can find a list of the other rules that are available.

Sort Imports: The Result

Now as soon as we save the file (if you happen to have format on saving in whichever IDE you’re using) or format the file you’ll see the following result:

sorted imports

Wow, that look’s much cleaner 👌🏼

Conclusion

Hopefully, with that, you have more organized imports in your projects! If you have any other tidbits on how to clean up code leave them in the comments below!

More content at Relatable Code

If you liked this feel free to connect with me on LinkedIn or Twitter

Check out my free developer roadmap and weekly tech industry news in my newsletter.

Comments 5 total

  • Ian VanSchooten
    Ian VanSchootenDec 8, 2022

    Note that if you use this plugin, side-effect imports will also be sorted, which is not safe to do can result in a different behavior. I maintain a fork of the project which does not sort side-effects, if that's something that is important to you.

    github.com/IanVS/prettier-plugin-s...

    • Dio Ilham D
      Dio Ilham DDec 11, 2023

      nice, why not merge your fork into the main repo?

  • Azat S.
    Azat S.Jun 9, 2023

    Also you can use eslint-plugin-perfectionist with "Fix on save" option in your editor. It supports sorting not only alphabetically, but also by line length. And not only imports.

    github.com/azat-io/eslint-plugin-p...

  • Ryan Walker
    Ryan WalkerJan 2, 2024

    Side note- I had to add this to my .prettierrc

    "plugins": ["@trivago/prettier-plugin-sort-imports"]
    
    Enter fullscreen mode Exit fullscreen mode
    • Sergey Lubimov
      Sergey LubimovMar 19, 2024

      Thanks,
      Without this line it doesn't work.
      Diego, add the line to article`s text.

Add comment