Using Plugins and Extends

Learn how to use extends and plugins in ESLint v9's eslint.config.mjs configuration to write more consistent JavaScript.

🎯 Introduction

Understanding extends and plugins in ESLint v9's eslint.config.mjs lets you reuse and extend coding conventions, improving code quality and team collaboration. This article explains how to use these options with ESLint v9 to create a flexible configuration suited to your project.


🌟 Introducing ESLint v9 and eslint.config.mjs

ESLint is a widely used linter for JavaScript and frontend projects that automatically finds potential problems and inconsistent patterns. Version 9 updated the configuration system and recommends the ES module-based eslint.config.mjs file, allowing you to write flexible configurations using native JavaScript syntax.


⚙️ Basic eslint.config.mjs Syntax

eslint.config.mjs is an ES module file. Use export default to export the configuration object:

export default {
    rules: {
        semi: ['error', 'always'], // Require semicolons at the end of statements
    },
};

You can configure rules, register plugins, and extend other configurations here.


🧩 extends: Reuse Existing Rule Sets

extends lets you inherit official or community-provided configurations, applying a complete set of rules without configuring each one individually.

What extends Does

  • Applies ESLint's official recommended rule set, such as "js/recommended"
  • Integrates shared configurations maintained by a community or team
  • Accepts an array of configurations, merged in order, with later settings overriding earlier settings for the same rules

Example of extends in eslint.config.mjs

export default {
    extends: ['js/recommended'],
};
An entry in extends with the format "<plugin-name>/<config-name>" refers to a configuration provided by a plugin.

🧰 plugins: Add Rules and Syntax Support

Use plugins when you need rules for specific syntax features or framework conventions. Plugins extend ESLint with additional rules and capabilities.

What plugins Does

  • Loads additional ESLint rules and linting logic
  • Provides support for specialized syntax or language features, such as TypeScript and JSX, through the appropriate tooling
  • Makes custom or community-provided rules available for linting

Example of plugins in eslint.config.mjs

import js from '@eslint/js';

export default {
    plugins: { js },
};

Here, js is the plugin name used in the configuration, and @eslint/js is the installed npm package.


🔗 How extends and plugins Work Together

  • plugins registers plugins with ESLint, making their rules and features available
  • extends applies a plugin's predefined recommended configuration or another shared rule set
  • A common pattern is to register a plugin in plugins, then apply its configuration through extends

Example:

export default {
    plugins: { js },
    extends: ['js/recommended'],
};

⚠️ Common Mistakes and Considerations

Referencing a Plugin in plugins/extends Without Installing It

Make sure the corresponding plugin is installed; otherwise, ESLint will report that it cannot find it.

eslint.config.mjs Must Use ES Module Syntax

Export the configuration with export default, rather than CommonJS syntax.


❓ Questions and Answers

Q1: Why use eslint.config.mjs instead of .eslintrc.js?

eslint.config.mjs is a recommended configuration file for ESLint v9. It uses native ES modules, supports flexible configuration and modern syntax, and fits the modern JavaScript ecosystem.

Q2: What does "js/recommended" in extends mean?

It applies the recommended configuration provided by the js plugin, enabling that rule set.

Q3: What happens if I configure plugins without extends?

Registering a plugin makes its rules available but does not enable them automatically. You usually apply a configuration through extends to enable a rule set.


🧪 Practice

Exercise 1: A Basic ESLint Configuration

Write an eslint.config.mjs file that extends the official recommended configuration and disables no-console to allow console.log.

Starter code:

export default {
    // TODO: Add extends
    // TODO: Disable no-console in rules
};

🎉 You now understand how to use extends and plugins in ESLint v9's eslint.config.mjs! Use them to quickly set up linting rules that help maintain your project's code quality.