tech hack
Technology

Tech Career Hack: Maintain Code Consistency using ESLint, Prettier, husky and lint-staged!

As software projects become more complex, maintaining code consistency becomes increasingly important. Inconsistent code can lead to readability issues, make collaboration difficult, and create unnecessary maintenance overhead. Fortunately, there are tools available to help developers maintain code consistency.

In this article, referenced with the help of Shashwat Nautiyal, we will explore how ESLint, Prettier, husky, and lint-staged can be used together to create a powerful development workflow that ensures code consistency. We will discuss each of these tools in detail and explain how they can be integrated to save time and effort in the long run. By the end of this article, you will have a better understanding of how to maintain code consistency in your projects and improve collaboration among team members.

First, let’s talk about what are these tools:

ESLint

ESLint is a popular linting tool that helps developers enforce code consistency rules. It can detect potential issues in the code and provide suggestions for fixing them. The tool is highly customizable, allowing developers to configure rules based on their preferences. ESLint can detect issues related to code formatting, variable declarations, and function usage.

One of the main benefits of using ESLint is that it helps in maintaining code consistency by enforcing rules. ESLint can be configured to check for different rules, and it will highlight any issues that violate those rules. This can help in ensuring that all the code in a project follows the same standards, which can make it easier for developers to collaborate on the codebase.

Another benefit of ESLint is that it can detect potential issues before they become a problem. For example, if a developer declares a variable without using it, ESLint can detect it and provide a warning. This can help in catching potential bugs early on, which can save time and effort in the long run.

Prettier

Prettier is a code formatting tool that can help in ensuring code consistency. The tool can automatically format code based on predefined rules. It can handle code formatting related to indentation, line breaks, and white spaces.

One of the main benefits of using Prettier is that it saves time. Rather than manually formatting the code, Prettier can do it automatically. This can save time and effort, especially in larger projects where there is a lot of code to format.

Another benefit of Prettier is that it can help in avoiding style debates. Developers can spend hours debating the right way to format the code. Prettier can help in avoiding this by providing a standardized way of formatting code. This can help in maintaining code consistency and can make it easier for developers to collaborate on the codebase.

Husky

Husky is a Git hook manager that can help in ensuring code consistency. Git hooks are scripts that are triggered when certain Git events occur, such as committing code or pushing changes to a remote repository. Husky can be used to manage Git hooks, allowing developers to run scripts based on Git events.

One of the main benefits of using husky is that it can help in enforcing rules before code is committed or pushed. For example, husky can be configured to run ESLint or Prettier before a developer commits their code. This can help in catching issues early on, before they are committed to the repository.

Another benefit of husky is that it can help in automating tasks. Developers can use husky to run scripts that automate repetitive tasks, such as running tests or building the code. This can save time and effort and can help in maintaining code consistency.

Lint-staged

Lint-staged is a tool that can help in optimizing the Git commit process. The tool can be used to run scripts on staged files, which are files that have been modified and are ready to be committed. Lint-staged can be used to run scripts for linting, formatting, or testing staged files.

One of the main benefits of using lint-staged is that it can help in optimizing the Git commit process. Rather than running scripts on the entire codebase, lint-staged can be used to run scripts on staged files only. This can save time and effort , as developers only need to run scripts on the code that has been modified.

Another benefit of lint-staged is that it can help in ensuring code consistency. For example, lint-staged can be configured to run Prettier on staged files, ensuring that the code is properly formatted before it is committed. This can help in maintaining code consistency and can make it easier for developers to collaborate on the codebase.

Integrating ESLint, Prettier, husky, and lint-staged

Integrating ESLint, Prettier, husky, and lint-staged is a great way to ensure code quality and consistency in your JavaScript project.

Here are the steps you can follow:

  1. Install ESLint, Prettier, husky, and lint-staged as dev dependencies in your project:
npm install eslint prettier husky lint-staged --save-dev
  1. Create a .eslintrc file in your project root directory and define your ESLint rules:
{
  "extends": [
    "eslint:recommended",
    "plugin:prettier/recommended"
  ],
  "rules": {
    // your custom rules here
  }
}

The extends property specifies the ESLint configuration to use, and the rules property defines your custom rules.

  1. Create a .prettierrc file in your project root directory and define your Prettier rules:
{
  "semi": false,
  "singleQuote": true,
  "trailingComma": "es5"
}

These rules define how Prettier formats your code.

  1. Add the following scripts to your package.json file:
{
  "scripts": {
    "lint": "eslint .",
    "lint:fix": "eslint . --fix",
    "prettier": "prettier --check .",
    "prettier:fix": "prettier --write ."
  }
}

These scripts allow you to run ESLint and Prettier commands from the command line.

  1. Add the following configuration to your package.json file:
{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.js": [
      "npm run lint:fix",
      "npm run prettier:fix",
      "git add"
    ]
  }
}

This configuration specifies that lint-staged should run the lint:fix and prettier:fix scripts on all .js files that are staged for commit. The git add command stages the changes made by prettier:fix.

Now, whenever you run git commit, husky will run lint-staged, which will first run ESLint and Prettier on the files you’re committing and stage the changes made by Prettier.

Congratulations! You have successfully integrated ESLint, Prettier, husky, and lint-staged in your project.

Conclusion

Maintaining code consistency is one of the most important aspects of software development. Consistent code can help in better collaboration, code readability, and easier maintenance. Fortunately, there are tools available that can help in maintaining code consistency, including ESLint, Prettier, husky, and lint-staged.

By using these tools, developers can create a more efficient and consistent development workflow that saves time and effort in the long run. Consistent code is not only easier to read and maintain, but it also helps in improving collaboration among team members.

As software development becomes increasingly complex, tools like ESLint, Prettier, husky, and lint-staged will become increasingly important in maintaining code consistency.

Sushrut is the key content strategist and writer for FuelEd Community. He is a content writer and content strategist with experience in domains like technology, freelancing, and side hustles.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video
X