ESLint HTML Parser
eslint-html-parser is a custom ESLint parser designed to lint HTML files, including embedded JavaScript within `<script>` tags. It achieves this by parsing HTML into a specific Abstract Syntax Tree (AST) structure, while delegating the parsing of JavaScript code (both in `<script>` tags and standalone `.js`/`.jsx` files) to a configurable ECMAScript parser, defaulting to `espree`. This enables developers to apply standard ESLint rules to their JavaScript within HTML, as well as define custom rules that target the HTML structure itself. The package is currently at version 6.8.2 and appears to be actively maintained, offering a crucial bridge for projects that require robust linting across both HTML and JavaScript within a single ESLint setup. It differentiates itself by providing a comprehensive HTML AST for deep linting, unlike simpler preprocessors that only extract script content.
Common errors
-
ESLint was configured to use the parser "eslint-html-parser" but that parser was not found.
cause The `eslint-html-parser` package is not installed or the path to it is incorrect in the ESLint configuration.fixEnsure `eslint-html-parser` is installed as a dev dependency: `npm install --save-dev eslint-html-parser`. Verify the `parser` setting in `.eslintrc.*` is exactly `"eslint-html-parser"`. -
Parsing error: The keyword 'await' is reserved (ESLint)
cause The `parserOptions.ecmaVersion` is set too low for modern JavaScript features like `await` or `async` functions.fixIncrease `parserOptions.ecmaVersion` in your ESLint config to a version that supports the syntax, e.g., `2021` or `latest`. -
Definition for rule 'no-console' was not found.
cause While `eslint-html-parser` parses HTML and JS, it doesn't provide rules itself. This error means a configured rule is missing.fixEnsure you have an `extends` configuration (e.g., `"extends": "eslint:recommended"`) or explicitly define the rules or plugins that provide the rules you intend to use. -
No files were linted. Please ensure that 'files' is set correctly in your configuration.
cause ESLint is not configured to process files with `.htm` or `.html` extensions, or the glob patterns/extensions in the CLI command are incorrect.fixAdd `.htm` and `.html` to your ESLint CLI command using `--ext .htm --ext .html` or ensure your `files` array in `eslint.config.js` or `overrides` in `.eslintrc.*` includes patterns like `"**/*.{js,htm,html}"`.
Warnings
- breaking ESLint v9.0.0 and above deprecate the legacy `.eslintrc.*` configuration system in favor of flat config (`eslint.config.js`). `eslint-html-parser` examples typically use `.eslintrc.json`, which may require adaptation for newer ESLint versions.
- gotcha The parser requires Node.js 6.x or later and ESLint 6.x or later. Using older versions will lead to installation or runtime errors.
- gotcha By default, ESLint only lints `.js` files when run on a directory. To lint HTML files, you must explicitly specify the extensions using glob patterns in the CLI or the `--ext` option.
- gotcha The `parserOptions` for `eslint-html-parser` directly map to the options supported by the underlying JavaScript parser (defaulting to `espree`). Misconfiguring `ecmaVersion`, `sourceType`, or `ecmaFeatures` can lead to parsing errors for JavaScript content.
Install
-
npm install eslint-html-parser -
yarn add eslint-html-parser -
pnpm add eslint-html-parser
Imports
- parseForESLint
import { parseForESLint } from 'eslint-html-parser'; - HTMLElement
import type { HTMLElement } from 'eslint-html-parser'; - HTMLAttribute
import type { HTMLAttribute } from 'eslint-html-parser';
Quickstart
{
"name": "my-linted-project",
"version": "1.0.0",
"description": "Example project using eslint-html-parser",
"scripts": {
"lint": "eslint \"./**/*.{js,htm,html}\"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-html-parser": "^6.0.0"
}
}
// .eslintrc.json
{
"root": true,
"parser": "eslint-html-parser",
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"rules": {
"no-console": "warn"
}
}
<!-- src/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello, HTML Lint!</h1>
<script>
console.log("This JS will be linted."); // no-console will warn here
var unusedVar = 1;
</script>
<my-custom-element some-attribute="value"></my-custom-element>
</body>
</html>
// src/app.js
console.log("This standalone JS will also be linted.");
# Terminal commands to set up and run:
npm init -y
npm install --save-dev eslint eslint-html-parser
# Create .eslintrc.json, src/index.html, src/app.js as shown above
npm run lint