myHotTake

Tag: Prettier configuration

  • How to Harmonize ESLint and Prettier for Perfect Code?

    Hey there! If you enjoy this story, feel free to hit like or share it with your friends who might find it interesting.


    I’m standing in front of a giant chalkboard, a dusty piece of chalk in my hand. The task is simple yet profound—write a flawless equation that holds the room in awe. This equation is a metaphor for the perfect JavaScript code. But as I start writing, I notice two voices in my head. One whispers, “Precision!”—this is ESLint, the meticulous mathematician who demands each number and symbol be perfectly placed. The other voice chimes in with, “Flow!”—this is Prettier, the artist who wants the equation to look as beautiful as it is correct.

    I begin with ESLint, carefully drawing each element of my equation with exacting precision. The angles are sharp, and the alignment is impeccable. But as I step back, I see that while it satisfies the mathematician, the equation lacks a certain elegance, like a masterpiece without a frame.

    Then, Prettier takes the chalk. With a flourish, it smooths the edges, spacing the numbers just so, converting my rigid equation into a flowing piece of art. But wait! Some of ESLint’s rules have been overshadowed by this newfound elegance, and the mathematician within me frowns.

    I realize that for my equation to shine, these two perspectives must not just coexist but collaborate. So, I set to work, adjusting the equation with both precision and artistry in mind. I use special rules, known as ESLint-Prettier plugins, to mediate between these voices. They ensure that the mathematician’s precision and the artist’s flow work in harmony, neither overshadowing the other.

    With each stroke of chalk, I see the equation transform, each part contributing to a greater whole. Finally, as I step back, I see it—a perfect blend of precision and elegance. The equation is not just correct; it’s captivating. I feel a sense of triumph, knowing I’ve created a masterpiece that satisfies both the mathematician and the artist.


    First, I set up ESLint to catch potential errors and enforce coding standards. In my .eslintrc.json file, I define rules that ensure my code is consistent and maintainable:

    {
      "extends": ["eslint:recommended"],
      "rules": {
        "no-unused-vars": "warn",
        "eqeqeq": "error"
      }
    }

    These rules ensure that I avoid common pitfalls, like using undeclared variables or non-strict equality checks. But as I write, I remember my artist friend, Prettier, who wants my code to look just as good as it functions.

    I introduce Prettier into the mix by adding a .prettierrc configuration file:

    {
      "singleQuote": true,
      "semi": false
    }

    This setup ensures my code is formatted consistently, with single quotes and without semicolons at the end of statements, giving it that polished look.

    But here’s where the magic happens—the ESLint-Prettier plugin. I install eslint-config-prettier and eslint-plugin-prettier to ensure these tools don’t step on each other’s toes:

    npm install --save-dev eslint-config-prettier eslint-plugin-prettier

    Then, I update my ESLint configuration to include Prettier’s rules:

    {
      "extends": ["eslint:recommended", "plugin:prettier/recommended"],
      "rules": {
        "prettier/prettier": "warn"
      }
    }

    Now, ESLint and Prettier work together seamlessly, just like the mathematician and artist in me did on the chalkboard. My code is both precise and beautiful, each tool enhancing the other’s strengths.

    Key Takeaways:

    1. ESLint and Prettier Complement Each Other: ESLint enforces coding standards and catches potential errors, while Prettier ensures consistent formatting.
    2. Configuration is Key: Use ESLint-Prettier plugins to harmonize both tools, preventing conflicts and ensuring they work in tandem.
    3. Consistent Code is King: Combining these tools leads to cleaner, more readable, and maintainable code, much like a perfectly balanced equation.