A Developer's Journey: Fixing Prettier and ESLint Issues in a Collaborative Backend Development Environment
As a volunteer backend developer at Recreax, I've had the opportunity to work on real-world projects, learn new technologies, and encounter challenges that have shaped my problem-solving abilities. One memorable experience occurred while collaborating with my team on the Weave Health Care API.
In this project, I encountered an error that initially puzzled me, but ultimately served as a valuable lesson in teamwork and code quality. The error appeared while trying to push my latest changes to our remote GitHub repository, and it looked something like this:
error Replace 'Controller, Post, Get, Body' with `Controller, Post, Get, Body`
prettier/prettier
This error was related to Prettier and ESLint, two tools that help maintain consistent coding styles across a project. It was a formatting issue, but resolving it revealed deeper insights into how linting tools work, and more importantly, how best practices can improve collaboration in a development team.
The Challenge
Formatting Errors Upon Commit
The error arose from violating our code formatting rules enforced by Prettier and ESLint. When I attempted to commit my code changes using Git, Prettier flagged inconsistencies in how certain TypeScript decorators and imports were formatted. Specifically, it was asking to replace single quotes '
with backticks `
, and vice versa.
At first glance, it might seem like a minor issue, but in a team environment where multiple developers contribute to the same codebase, such small inconsistencies can snowball into bigger problems. Code that doesn’t follow a unified format becomes harder to maintain, and debugging can become unnecessarily tedious.
The Fix
Streamlining the Code with Prettier and ESLint
To fix the error, I followed these steps:
Review the Prettier and ESLint Configuration: I began by checking our
.prettierrc
and.eslintrc
files to understand our codebase's formatting rules. In many cases, conflicts between these tools stem from misconfigurations. It’s important to ensure that both tools are set up to work in harmony. If they aren't, they could provide conflicting rules, leading to errors during commits.Run Lint and Prettier Locally: I ran the following command to fix the formatting issues flagged by Prettier:
npm run lint --fix
This command automatically corrects minor formatting issues, and it helped resolve the Prettier warnings. Running this before committing code ensures that all formatting inconsistencies are handled locally, preventing them from appearing in pull requests.
Fix Issues Manually if Needed: For more complex issues that couldn’t be auto-fixed, I manually edited the code, ensuring that everything adhered to our project's formatting rules. In this case, I replaced any improper quotes or decorators that didn’t match Prettier's expected style.
Commit and Push Again: After resolving the issues, I was able to commit and push the code successfully to the GitHub repository, without any linting errors.
Lessons Learned: Best Practices for Avoiding Formatting Conflicts
In a collaborative development environment, it’s crucial to establish best practices for code formatting and quality checks. Here are some key takeaways I learned from this experience:
Adopt Code Quality Tools Early: Set up tools like Prettier, ESLint, and Husky early in your project to catch formatting issues before they become problems. Husky can be used to enforce linting rules during the commit phase, ensuring that developers can’t push unformatted code.
Align ESLint and Prettier Configurations: Make sure your ESLint and Prettier configurations are compatible to avoid conflicts. Use tools like
eslint-config-prettier
to turn off conflicting rules and ensure a smooth integration.Run Lint Checks Before Committing: Encourage team members to run
npm run lint --fix
before committing any code. This ensures that all formatting issues are handled at the local level and reduces the number of formatting-related pull request reviews.Use Pre-Commit Hooks: Implement pre-commit hooks using Husky to automate lint checks and formatting corrections before code is even committed. This reduces the risk of inconsistent code making its way into the repository.
Code Reviews Focus on Logic, Not Formatting: By automating formatting, code reviews can focus on logic, functionality, and security, rather than wasting time on trivial formatting issues.
Conclusion: Improving Team Collaboration through Consistent Practices
Encountering this issue while developing the Weave Health Care API taught me the importance of enforcing consistent code practices in a team. Not only does it ensure that the codebase is clean and easy to maintain, but it also fosters better collaboration between developers. By using tools like Prettier and ESLint effectively, we can focus more on delivering high-quality features and less on trivial formatting errors.
References
Prettier Documentation
Prettier is a powerful code formatter that enforces a consistent style across your codebase. Learn more about its configuration options and usage here: Prettier Docs.ESLint Documentation
ESLint is a popular linter that helps identify and fix problems in your code, enhancing its readability and maintainability. Read about setting up ESLint for different projects and integrating it with Prettier here: ESLint Docs.Configuring ESLint with Prettier
Integrating Prettier with ESLint requires aligning their configurations to avoid conflicts. A guide on configuring ESLint and Prettier to work together effectively can be found here: Integrating ESLint + Prettier.Husky Git Hooks
Husky helps enforce linting and formatting rules during the commit phase by enabling Git hooks in JavaScript and TypeScript projects. Here’s how to get started with Husky: Husky Git Hooks Guide.npm run lint --fix Command
This command can automatically fix minor formatting issues, helping maintain code quality. For more on how it works and its benefits, check out the official npm guide on running scripts: npm Scripts.
Subscribe to my newsletter
Read articles from Aliyu Gambo Aliyu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aliyu Gambo Aliyu
Aliyu Gambo Aliyu
I am a Software Developer from Nigeria. Here's a quick summary about me: 😊 Pronouns: He/him 💡 Fun fact: I'm currently studying at AltSchool Africa School of Software Engineering Class of 2022 and also at HyperVerge Academy Class of 2023. 🌱 I’m currently learning JavaScript and Nodejs. 😊 I’m looking for help with open source projects, hackathons, internships, and entry-level opportunities. 💼 Job interests: Software Engineer, Front Engineer, Backend Engineer(Intern or Junior level). 📫 You can view my resume @mafuztechsolution.live and contact me by email.