Committing your codes to your GitHub sometimes will not be acceptable by Vercel or a platform that you are hosting the website due to some errors that exists inside your code, so you want your project to be checked with everything including types, styles of the codes, any errors or etc.… before committing to your repository (GitHub).
Eslint, Prettier, Husky, Lintstage are the best option for checking before committing.
Commitlint is a package where you can write a good and understandable commit message. Ex: “feat(home-page): message”
So this blog might help you to have a good and stable project while working with your teams.
And one more thing, imagine that you are working with a team, so everyone is not going to write the code in your way, so the method that I am going to tell you, is going to save you from various code writing, so stay with me and learn something cool if your project is open source or not, does not matter.
When I was working for a client project, I did not have access to vercel to check what the problem is with my code, so I had to make another vercel project and push the codes to 2 several project to see the errors.
I knew that there is an easier way to do this but I had a bad experience with it because every time I installed it, it was not working as expected, so the problem forced me to go and learn the eslint setup, now It is so easy to fix any errors before committing to production.
In my current knowledge, eslint is a technology that is going to check if there are any errors in your code, if there is a single error, then you are not able to commit anything unless you fix that error.
If you are looking to style your code writing and to have a readable and clean code, then prettier is the best option for that.
If you are working with a team, then I am sure that all of you are going to write the codes in different ways, so prettier is going to handle that so that you are going to have the same code written.
Eslint and prettier are nothing without husky, because this is husky that is going to run Eslint and prettier when committing, we will learn it more when installing huksy.
Before committing, you will run git add . command for sending your changes to stage, this package will help you to run eslint, prettier … only on those files that are on stage.
This will save you lots of time, without this you need to run eslint, … for your whole files so it takes time.
We will see why it is important when installing it.
It is not going to do anything to your codes, this is a bonus tip for you, so stay with me, you will love it.
by using your faviourte package manager, follow this step:
you will get this question to set up your Next.js project:
Run the following command:
You will be asked several questions, you can choose any options (options that you want your project to looks like).
If you are using TypeScript, choose Option "Yes"
You are done, now You need to give permission and choose your package manager to install the them.
You will see a new file after installing:
You need to update something in that file:
and then create a new file:
Copy and paste the following in that file:
Remove the plugins if you are not using Tailwind CSS.
You need to add 2 new lines in .eslint.js file:
New folder will be add:
There will be a devDependencies which will be added after running "npx husky-init", you need to install by running:
Let's keep installing other tools, we will come back to the husky later...
As we talked about it, it is going to test those codes that are staged, not the whole codes. imagine you are having a massive codebase, and you want to test all your code before committing, so it will take time which we don't want, right?
after that you need to make a folder, it is not going to create one for you:
Make sure that the spell of each files and folders must be same.
Add the code below in the file:
Basically, it is going to run whatever we add there before committing.
Now let's go back to Husky to add something:
Now, everything must be working fine.
Let's go and setup the last part which is...
If you want your commit messages to look professional or sounds like an expreince developer, install it:
Make a file:
Add the following code:
You need to make another file in the Husky folder:
Add the following code:
That is all you need to do to set up those tools that are really going to help if you are collaborating or for writing a clean code.
I am very happy that was able to write a blog that help others to setup these things easily, It was so hard and confusing for me back then but now here you go, you can use it :)
Peace