Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
JSHint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It helps developers identify potential errors and enforce coding standards.
Basic Linting
This feature allows you to perform basic linting on a piece of JavaScript code. The code sample demonstrates how to use JSHint to check a simple JavaScript snippet for errors.
const jshint = require('jshint').JSHINT;
const code = 'var a = 1;';
const options = { esversion: 6 };
jshint(code, options);
console.log(jshint.errors);
Custom Configuration
JSHint allows you to customize the linting process with various options. This example shows how to enable the 'undef' option to check for the use of undefined variables.
const jshint = require('jshint').JSHINT;
const code = 'var a = 1;';
const options = { esversion: 6, undef: true };
jshint(code, options);
console.log(jshint.errors);
Using JSHint with Configuration File
You can use a configuration file (e.g., .jshintrc) to define your linting rules. This example demonstrates how to read a JavaScript file and a JSHint configuration file, then lint the code using the specified rules.
const fs = require('fs');
const jshint = require('jshint').JSHINT;
const code = fs.readFileSync('path/to/your/file.js', 'utf8');
const config = JSON.parse(fs.readFileSync('.jshintrc', 'utf8'));
jshint(code, config);
console.log(jshint.errors);
ESLint is another popular JavaScript linting tool that is highly configurable and supports custom rules. It provides more flexibility and a larger ecosystem of plugins compared to JSHint.
JSCS (JavaScript Code Style) is a code style linter for JavaScript. It focuses more on enforcing coding style conventions rather than finding potential errors. It has been merged with ESLint, but older versions are still in use.
TSLint is a linter for TypeScript, which is a superset of JavaScript. It provides similar functionality to JSHint but is specifically designed for TypeScript code. TSLint is now deprecated in favor of ESLint with TypeScript support.
A command line interface and npm package for jshint.
To use jshint from any location (for npm v1.x) you need to install using the global (-g) flag.
npm install -g jshint
The command line interface looks like this.
jshint path path2 [options]
You can also require JSHint itself as a module.
var jshint = require('jshint');
Note: If you are using npm v1.x be sure to install jshint locally (without the -g flag) or link it globally.
node-jshint
.Specify a custom reporter module (see example/reporter.js).
--reporter path/to/reporter.js
Use a jslint compatible xml reporter.
--jslint-reporter
Show additional non-error data generated by jshint (unused globals etc).
--show-non-errors
Specify custom lint options (see example/config.json).
--config path/to/config.json
Note: This bypasses any .jshintrc files.
The CLI uses the default options that come with JSHint. However, if it locates a .jshintrc file in your home directory (~/) it will use those options first.
If there is a .jshintrc file in the current working directory, any of those options will take precedence over (or be merged with) any options found in the ~/.jshintrc file (if it exists).
If there is a .jshintignore file in the current working directory, then any directories or files will be skipped over.
Note: Pattern matching uses fnmatch, with the FNM_PATHNAME & FNM_CASEFOLD flags. When there is no match, it performs a left side match (when no forward slashes present and path is a directory).
./configure
jake -T
FAQs
Static analysis tool for JavaScript
We found that jshint demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.