DEV Community

Željko Šević
Željko Šević

Posted on • Originally published at sevic.dev on

Linting JavaScript codebase with Eslint

Linting represents static code analysis based on specified rules. Please include it in the CI pipeline.

Setup

Run the following commands to generate the linter configuration using the eslint package.

npm init -y
npm init @eslint/config
Enter fullscreen mode Exit fullscreen mode

Below is an example of the configuration. Some rules can be ignored or suppressed as warnings.

// .eslintrc.js
module.exports = {
  env: {
    commonjs: true,
    es2021: true,
    node: true,
    jest: true,
  },
  extends: 'airbnb-base',
  overrides: [
  ],
  parserOptions: {
    ecmaVersion: 'latest',
  },
  rules: {
    'import/no-extraneous-dependencies': 'warn',
    'import/prefer-default-export': 'off',
  },
};
Enter fullscreen mode Exit fullscreen mode

Ignore the files with the .eslintignore file.

dist
Enter fullscreen mode Exit fullscreen mode

Linting

Configure and run the script with the npm run lint command. Some errors can be fixed automatically with the --fix option.

// package.json
{
  "scripts": {
    // ...
    "lint": "eslint src",
    "lint:fix": "npm run lint -- --fix"
  }
}
Enter fullscreen mode Exit fullscreen mode

Boilerplate

Here is the link to the boilerplate I use for the development.

Top comments (0)