From this post, you can expect –
- Problems that Husky can solve
- Quick introduction of husky
- Configure Husky in Visual Studio Code
Problems that Husky can solve
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.
“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
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.