DEV Community

Mohammad Faisal
Mohammad Faisal

Posted on • Updated on • Originally published at mdfaisal.com

Setup EsLint Prettier and Husky in NodeJS

To read more articles like this, visit my blog

Having Typescript in a NodeJS project is a great way to ensure type safety, and it also improves the developer experience a lot. But we can take this even further by adding Linter and Formatter.

Today we will see how we can add linter and prettier to a NodeJS project using Typescript.

We are continuing this project from the previous article. The repository can be found here

Step 1: Install required dependencies

Let's first install the required dependencies.

yarn add -D eslint \
@typescript-eslint/eslint-plugin \
@typescript-eslint/parser
Enter fullscreen mode Exit fullscreen mode

Now Eslint doesn't have typescript support by default, so we add two additional packages as a developer dependency.

Step 2: Create a config file

Now create a new eslint config file.

touch .eslintrc
Enter fullscreen mode Exit fullscreen mode

And paste the following code there.

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
  "extends": ["plugin:@typescript-eslint/recommended"],
  "env": {
    "node": true // Enabling Node.js global variables
  },
  "rules": {}
}
Enter fullscreen mode Exit fullscreen mode

Let's add two scripts in our package.json file to lint and format easily.

"scripts": {
    "lint": "eslint src/**/*.ts",
    "format": "eslint src/**/*.ts --fix"
}
Enter fullscreen mode Exit fullscreen mode

Now you can run the following command to lint the whole project.

yarn lint
Enter fullscreen mode Exit fullscreen mode

Also, don't forget to create a .eslintignore file to omit some folders from being linted. As that can take up some time.

.eslintignore
Enter fullscreen mode Exit fullscreen mode

and put the following code there

node_modules
dist
Enter fullscreen mode Exit fullscreen mode

Step 3: Add Prettier

Let's now add prettier to the project. First, install the dependency.

yarn add -D prettier
Enter fullscreen mode Exit fullscreen mode

then create a .prettierrc file in the root folder and add the following configuration there

{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 120,
  "tabWidth": 2
}
Enter fullscreen mode Exit fullscreen mode

Now, this configuration is just my personal preference. You can use your configuration if you want.

Also, you can add a new script for running prettier on all files at once

"scripts": {
    "pretty": "prettier --write \"src/**/*.ts\""
}
Enter fullscreen mode Exit fullscreen mode

and run prettier for all files like the following

yarn pretty
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Husky

Now all these are just awesome. But I always forget to run these commands. To ensure that no bad code is going to be pushed to the source control, we can use a great tool called husky

It will run before you try to make a commit and can run some health checks. To understand better, let's first install it.

yarn add -D husky
Enter fullscreen mode Exit fullscreen mode

Then add a new block in the package.json file

"husky": {
    "hooks": {
      "pre-commit": "yarn lint"
    }
}
Enter fullscreen mode Exit fullscreen mode

So every time you try to commit your changes, this pre-commit hook will run, and your code will be fixed automatically. How cool is that!

Github repo:

https://github.com/Mohammad-Faisal/nodejs-typescript-skeleton

Reach me out on my LinkedIN
Read more articles on My Website

Top comments (0)