1  
    

And now write the plugin code. Plugins are just exported JavaScript objects.
To include a rule in a plugin, include it in the plugin’s ` rules ` object,
which contains key-value pairs of rule names and their source code.

To learn more about creating plugins, refer to [ Create Plugins ](plugins) .

    
    
    // eslint-plugin-example.js
    
    const fooBarRule = require("./enforce-foo-bar");
    const plugin = { rules: { "enforce-foo-bar": fooBarRule } };
    module.exports = plugin;
    
    
    1  
    2  
    3  
    4  
    5  
    

##  Step 8: Use the Plugin Locally

You can use a locally defined plugin to execute the custom rule in your
project. To use a local plugin, specify the path to the plugin in the `
plugins ` property of your ESLint configuration file.

You might want to use a locally defined plugin in one of the following
scenarios:

  * You want to test the plugin before publishing it to npm. 
  * You want to use a plugin, but do not want to publish it to npm. 

Before you can add the plugin to the project, create an ESLint configuration
for your project using a [ flat configuration file
](../use/configure/configuration-files) , ` eslint.config.js ` :

    
    
    touch eslint.config.js
    
    
    1  
    

Then, add the following code to ` eslint.config.js ` :

    
    
    // eslint.config.js
    "use strict";
    
    // Import the ESLint plugin locally
    const eslintPluginExample = require("./eslint-plugin-example");
    
    module.exports = [
        {
            files: ["**/*.js"],
            languageOptions: {
                sourceType: "commonjs",
                ecmaVersion: "latest",
            },
            // Using the eslint-plugin-example plugin defined locally
            plugins: {"example": eslintPluginExample},
            rules: {
                "example/enforce-foo-bar": "error",
            },
        }
    ]
    
    
    1  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    9  
    10  
    11  
    12  
    13  
    14  
    15  
    16  
    17  
    18  
    19  
    20  
    

Before you can test the rule, you must create a file to test the rule on.

Create a file ` example.js ` :

    
    
    touch example.js
    
    
    1  
    

Add the following code to ` example.js ` :

    
    
    // example.js
    
    function correctFooBar() {
      const foo = "bar";
    }
    
    function incorrectFoo(){
      const foo = "baz"; // Problem!
    }
    
    
    1  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    9  
    

Now you’re ready to test the custom rule with the locally defined plugin.

Run ESLint on ` example.js ` :

    
    
    npx eslint example.js
    
    
    1  
    

This produces the following output in the terminal:

    
    
    /<path-to-directory>/eslint-custom-rule-example/example.js
      8:11  error  Value other than "bar" assigned to `const foo`. Unexpected value: baz  example/enforce-foo-bar
    
    ✖ 1 problem (1 error, 0 warnings)
      1 error and 0 warnings potentially fixable with the `--fix` option.
    
    
    1  
    2  
    3  
    4  
    5  
    

##  Step 9: Publish the Plugin

To publish a plugin containing a rule to npm, you need to configure the `
package.json ` . Add the following in the corresponding fields:

  1. ` "name" ` : A unique name for the package. No other package on npm can have the same name. 
  2. ` "main" ` : The relative path to the plugin file. Following this example, the path is ` "eslint-plugin-example.js" ` . 
  3. ` "description" ` : A description of the package that’s viewable on npm. 
  4. ` "peerDependencies" ` : Add ` "eslint": ">=8.0.0" ` as a peer dependency. Any version greater than or equal to that is necessary to use the plugin. Declaring ` eslint ` as a peer dependency requires that users add the package to the project separately from the plugin. 
  5. ` "keywords" ` : Include the standard keywords ` ["eslint", "eslintplugin", "eslint-plugi