
Standardize Your Next.js Project with ESLint and Husky
3 July, 2022
0
0
1
Contributors
Disclaimer
In this article, I’ll focus on Next.js (using Typescript) based project. You can adapt it to your own needs based on your project source code and the problem that you’ve faced.
Introduction
Why We Need Standardization In Our Project Source Code?
Every Team Member Has Their Own Code Style
To Increase Maintainability and Readability
Not Only Code but Commit Message Also Need To Be Standardized
How To Standardize The Project Source Code?
•
•
•
•
•
•
For giving an example purpose, I’ve prepared a simple Next.js (Typescript) project. You can use next-tailwind-polos if you want to try to standardize the source code using those tools. You can clone it to your local device and don’t forget to install the dependencies using yarn
or npm install
•
•
•
•
In the steps below I’ll use yarn, actually, you can just adapt it by your preference if you use npm. You can see the official docs so you can match them by your need.
Prettier Configuration
yarn add --dev --exact prettier
. Then create a file named .prettierrc.json
in the root folder, or you can just run echo {}> .prettierrc.json
in your terminal..prettierrc.json
file, you can fill the configuration like below or you can fill it by your needs (reference: Prettier Config Options).yarn prettier --write [directory scope]
to format your code. Or if you use VS Code, you can set Format on Save in your VS Code Settings. You also can use the shortcut to manually format the file (using VS Code) by pressing Shift + Option + F
(in MacBook) or Shift + CTRL + F
(in Windows).Configure The Base ESLint Rules
eslintrc.json
file wherein the root project.lint
command exists in the package.json
file.eslintrc.json
. You don’t have to imitate this 100%, just match it by your own preference or current needs.yarn lint
to check if your code is already matched with the ESLint rules or not. You can also run yarn lint --fix
to automatically fix your code based on the rules that have been set. Remember that sometimes we need to manually edit the code, so don’t overly rely on the automated fixing code.Configure Husky Base Setting
npx husky-init && yarn
in the terminal. It will automatically generate the prepare
command in package.json
and a base pre-commit config.
npx mrm@2 lint-staged
. It will automatically generate the base command triggers in pre-commit
husky config file. After that, you can delete or modify the content by your own preference. After that, it will run the linter before the commit process. So you have to make sure that your code is matched with the linter rules.
yarn add --dev @commitlint/config-conventional @commitlint/cli
in the terminal.yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'
. It will generate a new husky config file named commit-msg
.

npx husky add .husky/pre-push 'yarn lint'
to generate the husky config that triggers the linter before pushing my code.
Closing
react
web application
nextjs
eslint
prettier