Configure Husky in VS Code for JavaScript Development

by Sudipta Deb | Jul 31, 2020 | javascript, Web Technology | 0 comments

In my previous post, I have written down the steps which will help you to configure ESLint and Prettier in Visual Studio Code to make your JavaScript development easy and making sure code syntax, as well as best practices, are being followed. If you haven’t seen that post, I highly recommend you go through that at – Configure ESLint and Prettier in VS Code for JavaScript Development.
Today I am going to talk about another important tool when you are dealing with committing your JavaScript code in Git repository. It’s called Husky.
From this post, you can expect –
  • Problems that Husky can solve
  • Quick introduction of husky
  • Configure Husky in Visual Studio Code
  • Examples

Problems that Husky can solve

So far by installing ESLint and Prettier we have the option to run the script which will check for syntax and best practices in our JavaScript code. But what about if we forget to execute this script before committing our changes to the GitHub repository? We will be then committing poorly written code into our repo. We can always run ESLing and Prettier as part of our continuous integration, but having a problem identified at that stage is kind of late in the game. We need to fix now and do another commit.
So it will be good if we can identify these problems even before we do the first commit. And here comes the power of Husky.

Quick Introduction of Husky

Husky is responsible for creating git hooks. Git hooks are scripts that git will execute before or after the events as per your configuration. So in our situation, we will be writing git hooks with husky which will execute eslint before every commit.

Configure Husky in Visual Studio Code

Step 1 – Install Husky

The first thing we need to do is to install Husky as dev dependencies. You can do that by executing the below command from your terminal.
“npm i -D husky”.
Once the command executes successfully, husky will be added as a dev dependency in the package.json file.

Step 2 – Create git hook

You can create git hooks for husky by adding the below stuffs in your package.json.
“husky”: {
“hooks”: {
“pre-commit”: “npm run lint — — src”
}
}
Basically, you are telling husky to run lint on the src folder before every commit. So if there is any complaint by lint, the actual commit will not happen.
You can get the full husky git options here.

Examples

Now let’s create a new JavaScript file called HelloCanada.js and make sure ESLint through error message for that file. Here is the file –
var HelloCanada
console.log(‘Hello from Canada’)
When we try to commit the file, the error will appear and the actual commit will fail. Below is the screenshot of the error message –
As you can see, husky ran the lint command and as it found some error, the actual commit stopped.

Written by

Related Posts

TypeScript Setup

TypeScript Setup

Below are the tools you need to set up with the environment to start writing TypeScript code: Node.js – Node.js is the environment in which you will run the TypeScript compiler. TypeScript compiler – a Node.js module that compiles TypeScript into JavaScript. If you...

read more...
Introduction to TypeScript

Introduction to TypeScript

What is TypeScript A superset of JavaScript is TypeScript. The foundation for TypeScript is JavaScript. You begin by writing the TypeScript code. The TypeScript code is then converted into standard JavaScript code using a TypeScript compiler. Once you have the basic...

read more...

0 Comments

Leave a Reply