
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
precommit-hook
Advanced tools
A useful pre-commit hook for git based projects that lints and runs npm test
This module is a handy little tool that I wrote to help enforce code quality in node.js projects. It will run the JSHint linter over your code, followed by two optional steps: A custom validator specified by you in your package.json, and unit tests which are also specified in your package.json.
To facilitate the usage of JSHint, as part of the install step two files will be created in the root of your project, .jshintrc and .jshintignore. These files are configuration for JSHint itself. They are included to provide some sane defaults, such as making sure your installed npm modules are not being linted, and that some node.js global variables won't trigger errors. You are free to customize these files after they're created, and they will not be overwritten.
No one likes a messy code base. When working on a team, it becomes more and more difficult to make sure that your project's code stays consistent and error free. Since the hook lints all of the project's code, based on your configuration, you can be sure that at the very least standards are being followed.
In addition to this, the validate script can be used to perform whatever manual checking you like. Whether it's making sure that a file has been updated, pre-compiling static assets, or whatever other need you may have. Additional steps are easy to forget, so why chance it?
Unit tests are another thing that should always be verified before committing your code. Pushing code that breaks tests is an all too common occurrence.
Having a tool that automates all of these steps has been priceless for us, and has very much improved the quality of our code.
As mentioned above, there are two optional steps run by the hook. A validator, and unit tests. Specifying these scripts is done in your package.json, such as the below example.
{
"name": "your_project",
"description": "just an example",
"scripts": {
"validate": "./command/to/run",
"test": "./other/command"
}
}
The contents of the validate and test properties are the shell command to be run to perform those functions. Having these specified in your package.json also lends you the ability to be able to run them manually like so
npm run-script validate
npm test
These scripts can be any shell executable commands, but must exit with a status code of 0 for success and 1 or greater for failure.
In addition to this, a new feature is the ability to manually override these commands or disable them entirely. To do so you add a precommit config to your package.json, similar to the below example.
{
"name": "your_project",
"description": "just an example",
"scripts": {
"validate": "./command/to/run",
"test": "./other/command"
},
"config": {
"precommit": {
"lint": false,
"validate": true,
"test": "mocha"
}
}
}
This example would disable the linting step entirely, explicitly enables the validation step, and overrides the test step to run "mocha" instead of "./other/command"
npm install precommit-hook
Everything else is automatic! The npm install script will create the hook, and place a .jshintrc and .jshintignore file in your project if they don't exist. To update, just install again. Only the hook itself will be overwritten. I recommend putting precommit-hook in your project's devDependencies to make sure that anyone who may be contributing to your project will have the hook installed.
{
"name": "your_project",
"description": "just an example",
"scripts": {
"validate": "./command/to/run",
"test": "./other/command"
},
"devDependencies": {
"precommit-hook": ""
}
}
Like the project? Hate it? Just want to say hi? Find me on twitter @quitlahok
MIT
FAQs
A useful pre-commit hook for git based projects that lints and runs npm test
The npm package precommit-hook receives a total of 2,396 weekly downloads. As such, precommit-hook popularity was classified as popular.
We found that precommit-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.