# Load all custom rules from a directory (recursively finds all .js files)
npx htmlhint --rulesdir ./custom-rules/ index.html
```

### Custom Rule Format

[Section titled “Custom Rule Format”](#custom-rule-format)

Custom rules should be JavaScript modules that export a function. The function receives the `HTMLHint` instance as a parameter and should register the custom rule using `HTMLHint.addRule()`.

#### Example Custom Rule

[Section titled “Example Custom Rule”](#example-custom-rule)

my-custom-rule.js

```javascript
module.exports = function(HTMLHint) {
  HTMLHint.addRule({
    id: 'my-custom-rule',
    description: 'This is my custom rule description',
    init: function(parser, reporter, options) {
      // Rule implementation - Note: Use arrow functions for event listeners
      parser.addListener('tagstart', (event) => {
        const tagName = event.tagName.toLowerCase();


        if (tagName === 'div') {
          reporter.warn(
            'Custom rule: div tags are not allowed',
            event.line,
            event.col,
            this,
            event.raw
          );
        }
      });
    }
  });
};
```

**Important**: Always use arrow functions for event listeners to ensure the correct `this` context is maintained when calling reporter methods.

#### Using Custom Rules

[Section titled “Using Custom Rules”](#using-custom-rules)

After loading custom rules with `--rulesdir`, you can enable them in your configuration:

```json
{
  "my-custom-rule": true
}
```

Or via command line:

```shell
npx htmlhint --rulesdir ./custom-rules/ --rules my-custom-rule index.html
```

### Directory Loading

[Section titled “Directory Loading”](#directory-loading)

When specifying a directory, HTMLHint will:

1. Recursively search for all `.js` files in the directory
2. Load each file as a custom rule module
3. Skip any files that fail to load (errors are silently ignored)

This makes it easy to organize multiple custom rules in a single directory structure.

For detailed information on creating custom rules, see the [Custom Rules documentation](/usage/custom-rules/).