
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - ESLint
What is ESLint?
ESLint is a JavaScript code analysis tool for identifying and reporting issues in code quality, style, and security. It helps developers identify and fix problems in their code, and adhere to coding standards.
How it works?
- The ESLint can be configured as dependency in Next.js project.
- It parses the JavaScript code to understand its structure and syntax.
- It applies a set of predefined rules to the code, to detect potential issues in the code.
- Finally, the ESLint reports any violations, errors, or quality issues found in the code.
Next.js provides an ESLint plugin, eslint-plugin-next, already bundled within the base configuration, that makes it possible to catch common issues and problems in a Next.js application.
Steps to Configure ESLint in Next.js
Here we provide steps to configure ESLint in your Next.js project. But before that, you must setup Next.js project in your local machine.
Step 1: Install ESLint
After setting up Next.js project, you can get started by installing ESlint library and necessary dependencies. We can do this using either npm or yarn, depending on our preference.
// Using npm >> npm install eslint eslint-config-next --save-dev // Using yarn >> yarn add eslint eslint-config-next --dev
Step 2: Create Configuration File
Next, we need to create a configuration file for ESLint. This file will contain all the rules and settings that we want to apply to our code. We can create a new file called .eslintrc.json
in the root directory of our project.
// File: .eslintrc.json { "extends": "next/core-web-vitals" }
Once we have our configuration file, we need to specify the ESLint configuration in our package.json file. We can do this by adding the following lines to our package.json file:
// File: package.json "eslintConfig": { "extends": "next" },
This tells ESLint to use the next configuration provided by eslint-config-next. This configuration includes all of the necessary rules for a Next.js project.
Step 3: Create ESLint Command
Now that we have our ESLint configuration set up, we need to create a command to run ESLint. We can do this by adding the following lines to our package.json file:
// File: package.json "scripts": { "lint": "next lint" },
This tells npm to run the lint command when we run npm run lint. This command will run ESLint on our code and report any issues that it finds.
Step 4: Run ESLint
Now that we have our ESLint configuration set up, we can run ESLint on our code. We can do this by running the following command in the terminal:
>> npm run lint
This will run ESLint on our code and report any issues that it finds. If there are no issues, it will exit with a code of 0. If there are issues, it will exit with a code of 1.
Linting Custom Directories and Files
By default, Next.js will run ESLint for all files in the pages/, app/, components/, lib/, and src/ directories. However, you can specify which directories using the dirs option in the eslint config in next.config.js for production builds:
// File: next.config.ts module.exports = { eslint: { dirs: ['pages', 'utils'], // Only run ESLint on the 'pages' and 'utils' directories during production builds (next build) }, }
Disabling Linting on Production Builds
You can disable linting while creating a production builds. This allows production builds to successfully complete even if your project has ESLint errors. To disable linting on production builds, you can use the ignoreDuringBuilds option in the eslint config in next.config.js:
// File: next.config.ts import type { NextConfig } from 'next' const nextConfig: NextConfig = { eslint: { ignoreDuringBuilds: true, }, } export default nextConfig
Disabling the Cache
To improve performance, information of files processed by ESLint are cached by default. This is stored in .next/cache or in your defined build directory. If you include any ESLint rules that depend on more than the contents of a single source file and need to disable the cache, use the --no-cache flag with next lint.
>> next lint --no-cache