_____ _ _ _ _
| __ \ | | | | | | | |
| |__) | _ _ __ __| |_ _ ___ | |__| | __ _ ___| | _____ _ __ ___
| ___/ | | | '__/ _` | | | |/ _ \ | __ |/ _` |/ __| |/ / _ \ '__/ __|
| | | |_| | | | (_| | |_| | __/ | | | | (_| | (__| < __/ | \__ \
|_| \__,_|_| _\__,_|\__,_|\___| |_| |_|\__,_|\___|_|\_\___|_| |___/
\ \ / (_) | (_)
\ \ /\ / / _| | ___
\ \/ \/ / | | |/ / |
\ /\ / | | <| |
\/ \/ |_|_|\_\_|
Downloading post...
Download [===============>] 40%
In this blog post, I will be going over how to set up Husky, Eslint, and Prettier in NextJS and ExpressJS projects.
Installing dependencies:
Create a file called .prettierrc.json
where we will be storing our Prettier configuration. Copy paste the following code into this file.
Create a file called .prettierignore
where we will specify the files that we don’t want to format. Copy and paste the following code into this file.
Depending on how your NextJS project is set up, you might not have Eslint set up yet. You can check if this by looking at the root folder of the project whether there is a file called .eslintrc.json
. If there isn’t, add the following line in the script section in package.json
.
Afterward, run the following command and you will be guided to set up eslint. A more detailed tutorial on how to set up eslint can be found here.
Next we need to install eslint-config-prettier
using the following command.
We also have to install some plugins for Eslint and some rules we want to use with the following command.
Replace the content of .eslintrc.json
with the following:
To verify that everything is set up correctly so far, you should be able to run Eslint without error using the following command.
Next we want to set up husky and lint-stage to help us format and lint our code every time we commit. To install lint-stage and husky run the following command. (Though it looks like you are only installing lint-stage, it is also installing husky!)
After installing, you will realize in your package.json
, there is a new section called lint-staged.
Change this section to:
You will also realize that there is a new folder called “.husky” In the file, .husky/pre-commit, replace the content with:
Now we are left with sorting imports. Let’s install eslint-plugin-simple-import-sort using the following command.
Now we just have to configure this plugin in eslintrc.json
. Change the file as follow