ESLint Plugin Handlebars
raw JSON → 1.0.4 verified Fri May 01 auth: no javascript
An ESLint plugin for linting Handlebars templates, version 1.0.4. It enforces consistent formatting and validates template syntax, including block spacing, quotes, indentation, and closing tags. Provides recommended and strict configurations. All rules are auto-fixable except no-missing-closing-tag. Requires ESLint >=8.57.0 and Node.js >=18.18.0. Unlike general formatters, it targets Handlebars-specific syntax errors and style issues.
Common errors
error Error: Failed to load plugin 'handlebars' declared in 'plugins': Cannot find module 'eslint-plugin-handlebars' ↓
cause Missing npm install or incorrect import path in flat config.
fix
Run npm install eslint-plugin-handlebars --save-dev and restart ESLint.
error TypeError: plugins must be an object, a string, or a function ↓
cause Using array syntax ['handlebars'] in flat config instead of object.
fix
Change to plugins: { handlebars } (object with key matching the import).
error ReferenceError: require is not defined in ES module scope ↓
cause Using require() in an ESM project.
fix
Replace const handlebars = require('eslint-plugin-handlebars') with import handlebars from 'eslint-plugin-handlebars'.
Warnings
breaking ESM-only: require() not supported. Must use import syntax. ↓
fix Switch to ESM: set "type": "module" in package.json and use import statements.
breaking Requires ESLint >=8.57.0. Older ESLint versions incompatible. ↓
fix Upgrade ESLint to >=8.57.0.
breaking Flat config only; legacy .eslintrc configs not supported. ↓
fix Migrate to flat config using eslint.config.js.
gotcha All rules are auto-fixable except no-missing-closing-tag. ↓
fix Run eslint with --fix for auto-fixable rules; manually fix missing closing tags.
gotcha Plugin name in flat config must match the import variable. Use 'handlebars' as plugin key. ↓
fix Set plugins: { handlebars } not plugins: ['handlebars'].
Install
npm install eslint-plugin-handlebars yarn add eslint-plugin-handlebars pnpm add eslint-plugin-handlebars Imports
- default wrong
const handlebars = require('eslint-plugin-handlebars')correctimport handlebars from 'eslint-plugin-handlebars' - defineConfig wrong
const { defineConfig } = require('eslint/config')correctimport { defineConfig } from 'eslint/config' - plugins wrong
plugins: ['handlebars']correctplugins: { handlebars }
Quickstart
// eslint.config.js
import { defineConfig } from "eslint/config";
import handlebars from "eslint-plugin-handlebars";
export default defineConfig([
{
plugins: {
handlebars,
},
rules: {
"handlebars/block-spacing": "error",
"handlebars/consistent-quotes": ["error", "double"],
"handlebars/expression-spacing": "error",
"handlebars/indentation": ["error", 2],
"handlebars/no-missing-closing-tag": "error",
"handlebars/no-multiple-empty-lines": ["error", { max: 1 }],
"handlebars/no-trailing-spaces": "error",
},
},
]);