DEV Community

Cover image for Level Up Your Code Quality with ESLint: Catching Errors and Enforcing Consistency
Glory Isaiah
Glory Isaiah

Posted on

Level Up Your Code Quality with ESLint: Catching Errors and Enforcing Consistency

ESLint is a powerful utility designed to detect and report patterns present in code.

How to install ESlint using npm

Intialize a npm project

npm init

Install eslint as a dev dependency

npm install eslint --save-dev

Setup eslint in your project by running this command

npx eslint --init

Start using eslint by running this command

npx eslint your-file.js

If you are using Yarn

Intialize a Yarn project

yarn init

Run the following command to install eslint( Note: this would install it as a Dev dependency)

yarn add eslint --dev

Setup eslint in your project by running the command below:

npx eslint --init

Run this command to start using eslint in your project:

npx eslint your-file.js

To set up lint rule enforcement using GitHub Actions, we will create a configuration file. This file, named eslint.yml, will define an automated action that runs during the workflow. Below is an example of the code:

name: ESLint

on:
  push:
    branches:
      - development
      - staging
      - production
  pull_request:
    branches:
      - development
      - staging
      - production

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16

      - name: Install dependencies
        run: npm install

      - name: Run ESLint
        run: npm run lint

Enter fullscreen mode Exit fullscreen mode

The workflow will be triggered when a push or a pull request is made to the specified environment branches (develop, staging, production).

Both push and pull requests will trigger the ESLint job to run and perform lint checks using ESLint.

Top comments (0)