DEV Community

Cover image for NextJs TypeScript setup with Prettier, Husky.
Ankush Thakur
Ankush Thakur

Posted on • Edited on

NextJs TypeScript setup with Prettier, Husky.

Let's get started with the NextJs setup with TypeScript. The configuration will be scalable with added configurations.

Let's create our app first by running the following command:

npx create-next-app@latest
Enter fullscreen mode Exit fullscreen mode

You'll be presented with options. You can select each according to your preferences.

What is your project named? nextJs-awesome-setup
Would you like to use TypeScript with this project? No / Yes
Would you like to use ESLint with this project? No / Yes
Would you like to use Tailwind CSS with this project? No / Yes
Would you like to use `src/` directory with this project? No / Yes
Use App Router (recommended)? No / Yes
Would you like to customise the default import alias? No / Yes
Enter fullscreen mode Exit fullscreen mode

Till now every step was generic which you can also find from here.

Deleting package.lock.json file and node_modules folder.

Then run:

yarn
Enter fullscreen mode Exit fullscreen mode

We have now converted our app to support yarn instead of npm. Which will be quite beneficial in the longer run.

Try running yarn dev to check if everything is working as expected.

yarn dev
Enter fullscreen mode Exit fullscreen mode

Prettier configuration

yarn add -D prettier
Enter fullscreen mode Exit fullscreen mode

This will install the required dependencies

touch .prettierrc
Enter fullscreen mode Exit fullscreen mode

This will create configuration file for Prettier where you can define your own custom rules.
Some common rules:

{
  "semi": true,
  "trailingComma": "none",
  "singleQuote": true,
  "printWidth": 80
}
Enter fullscreen mode Exit fullscreen mode

Paste these rules as is in the .prettierrc file we have just created.

Create .prettierignore to ignore certain files and folders

touch .prettierignore
Enter fullscreen mode Exit fullscreen mode

Add node_modules and .next folders

Add this line in package.json under scripts

"format": "npx prettier --write ."
Enter fullscreen mode Exit fullscreen mode

Now you can finally test if it works, run

yarn format
Enter fullscreen mode Exit fullscreen mode

Hope, it worked.

Husky configuration

Some information about husky

  1. Install husky
npm install husky --save-dev
Enter fullscreen mode Exit fullscreen mode
  1. Enable git hooks
npx husky install
Enter fullscreen mode Exit fullscreen mode
  1. To automatically have Git hooks enabled after install, edit package.json
npm pkg set scripts.prepare="husky install"
Enter fullscreen mode Exit fullscreen mode

After running this you should have this in your package.json

{
  "scripts": {
    "prepare": "husky install" 
  }
}
Enter fullscreen mode Exit fullscreen mode

Create a hook which gets triggered whenever you make a commit

npx husky add .husky/pre-commit "yarn format"
git add .husky/pre-commit
Enter fullscreen mode Exit fullscreen mode

Now whenever you do a new commit, your code should format itself before commit.

Bonus point: You run any script here using && and it will execute whenever you make commit for example your can run test every time before you make commit by adding:

yarn format && yarn test
Enter fullscreen mode Exit fullscreen mode

TLDR! This setup guide offers you code formatting and check before making a commit, so that you don't push anything that looks bad.

Happy Coding!

Top comments (2)

Collapse
 
simran_arora_ebcb033e78b3 profile image
Simran Arora

Really helpful!

Collapse
 
luyvannda_48 profile image
luyvannda

Thank you so much, really helpful. :)