Commit Better Code with Husky, Prettier, ESLint, and Lint-Staged
Note: This original post was written for/exists on coffeeclass.io
Creating code is easy, but creating great code is not. When we start to learn to program, we make what works and we're happy just with that. As we mature our skills, it's important to give ourselves the best environment to produce quality code. We can stop poor code from being pushed in our repository through linting and formatting. In this article, we'll learn how to improve our developer experience (DX). Let's go ahead and dive right in!
You can access the source code on this github repo.
Tools of the Trade 🧰
To create such a useful environment, we'll use these packages to check our code through git hooks:
- Husky 🐶: Supercharges our DX by linting, testing, or formatting code as code is committed with git.
- ESLint ✔️: Checks for certain code patterns to stop errors or potential bugs.
- Lint-Staged 🔧: Lints code before a commit occurs to keep production code clean.
- Prettier ✨: Keeps code formatting consistent based on our own preferences.
Committing Clean Code to a Project
Making a Clean Project
We'll use create-react-app to make the initial project. We won't be doing anything too much with React and you should still be able to follow along.
Now that the code has been made, let's go ahead and run it in our browser to see if everything is correct.
Everything should be working just fine! Now before we add our tools, let's make that first commit with just the CRA project.
Great! Now that we know the web app is working, let's add our tools to make clean code.
Adding ESLint and Prettier
ESLint is put in create-react-app by default, but we will create custom configuration files for both ESLint and Prettier.
Let's install Prettier and eslint-config-prettier and make our config files in root project directory.
Create an ESLint config and check off whatever specifications you'd like for your project.
You can select your own preferences through that command. If you're unsure what to pick, just make a copy of this .eslintrc.json in our root directory with the following configuration:
You'll notice there are a lot more errors appearing in the code than before. That is ESLint enforcing our selected code style based on the config file. Before we fix these errors, let's make our Prettier config file to help format our linted code.
Once your config file is made, add the following code to sync up both ESLint and Prettier:
Then modify eslintrc.json to include the following as the final item in the "extends" configuration:
Now with those two set up, we can move on to Husky and lint-staged.
Let's initialize Husky to our project:
This command will freshly add Husky to our project in the .husky folder. Inside this folder, we can create files that match the git hooks we want to use.
There are many Git hooks you can use to set up certain actions when using Git with your code. This tutorial will only deal with pre-commit Git hook provided to format and lint our code before it gets committed to our repository. Refer to Husky's Documentation for more examples of Git Hooks to use to improve your developer experience on your projects.
While we're here, let's install lint-staged:
Modify the pre-commit Git Hook in .husky. This will run lint-staged before a commit can be pushed to our codebase.
Add the lint-staged configuration to package.json so when certain files are staged for a commit, we run ESLint and Prettier.
Testing Our Setup
Now that we've installed and configured our tools, let's test this out in action!
Let's find out what happens if we commit our code as-is:
Oops! Seems our commit failed. Luckily, the packages we use inform us what is causing the linting problem in App.js. Let's fix it and try again:
Success! 🥳🥳 We have just committed clean code to our repository like a true pro 😎.
To further your own DX while you code, refer back to the documentation of these technologies to discover more ways to use git hooks.