JSHint - JavaScript Static Analysis Tool
JSHint is a static code analysis tool for JavaScript that identifies errors and potential problems, including typos, syntax errors, implicit type conversions, and variable leaks. Its primary goal is to help developers prevent common mistakes in complex JavaScript programs. The current stable version is 2.13.6, with its most recent update in November 2022. While it previously saw regular bug-fix releases, its development cadence has slowed considerably compared to its peak. A key differentiator for JSHint is its focus on detecting bugs and structural issues in the code rather than primarily enforcing code style, offering high configurability through `.jshintrc` files and inline comments to adapt to diverse execution environments. It predates many modern linters and has historically been crucial for spotting critical 'red flags' that could lead to extensive debugging if left unaddressed.
Common errors
-
'variableName' is not defined. (E008)
cause A variable was used without being declared, or it's a global variable not explicitly defined in JSHint's globals option.fixDeclare the variable using `var`, `let`, or `const`. If it's a global, add it to your `.jshintrc` `globals` array (e.g., `"globals": { "jQuery": true }`) or pass it in the `globals` argument when calling `JSHINT` programmatically. -
Missing semicolon. (W033)
cause JSHint expects a semicolon at the end of a statement, following JavaScript's Automatic Semicolon Insertion (ASI) rules but enforcing explicit semicolons.fixAdd a semicolon (`;`) at the end of the problematic statement. You can configure JSHint to be more lenient with semicolons using the `asi` option (e.g., `"asi": true`) or disable the warning for unused expressions (`"expr": true`). -
Expected an identifier and instead saw 'keyword'. (E002)
cause This often occurs when using newer ECMAScript keywords (e.g., `const`, `let`, `async`, `await`) without enabling the appropriate ECMAScript version in JSHint options.fixUpdate your JSHint configuration to specify the target ECMAScript version using the `esversion` option (e.g., `"esversion": 2017` for `async/await`). Ensure your JSHint version itself supports the desired ES version. -
Too many errors. (E043)
cause JSHint stopped linting after encountering a large number of errors to prevent excessive output, often indicating a fundamental issue or misconfiguration.fixAddress the initial errors reported by JSHint. This error is a threshold, not a specific syntax problem. Once some errors are fixed, JSHint will process more of the file. Check your `maxerr` option if you intentionally expect many errors.
Warnings
- gotcha JSHint's development has significantly slowed, with the last major update in late 2022. For new projects, or those requiring support for the latest ECMAScript features (ES2023+), JSX, or TypeScript, more actively maintained alternatives like ESLint are generally preferred due to their extensibility and ecosystem.
- gotcha JSHint does not natively support TypeScript or JSX. Attempting to lint such files will likely result in numerous parsing errors.
- gotcha The primary programmatic interface for JSHint uses CommonJS modules (`require`). Direct ESM `import` statements for `jshint` might not work as expected in all Node.js environments without transpilation or specific bundler configurations.
- breaking The `shelljs` dependency was removed in JSHint v2.13.4. While this primarily affected JSHint's internal build process, any user who might have implicitly relied on `shelljs` being a transient dependency of `jshint` could experience issues if they upgrade and `shelljs` is no longer available.
Install
-
npm install jshint -
yarn add jshint -
pnpm add jshint
Imports
- JSHINT
import { JSHINT } from 'jshint';const { JSHINT } = require('jshint'); - cli
require('jshint').cli.run()npx jshint your-file.js
- JSHINT.data()
const result = JSHINT(code, options, globals); const data = JSHINT.data();
Quickstart
const { JSHINT } = require('jshint');
const javascriptCode = `
function greet(name) {
console.log('Hello, ' + name)
// Missing semicolon here intentionally
}
const myName = 'World';
greet(myName);
// A variable not defined in this scope
undefinedVar = 1;
`;
// JSHint options: enforce strict mode, allow console
const options = {
strict: true,
browser: false, // For Node.js environment
node: true, // For Node.js globals like console
undef: true // Warn if variables are used without being declared
};
// Lint the code
JSHINT(javascriptCode, options);
// Log any errors found
if (JSHINT.errors.length > 0) {
console.log('JSHint found issues:');
JSHINT.errors.forEach(error => {
if (error) {
console.error(` Line ${error.line}, Col ${error.character}: ${error.reason} (${error.code})`);
}
});
} else {
console.log('No JSHint issues found.');
}