DEV Community

Cover image for Effortlessly Set Up a Next.js Project with ESLint: A Step-by-Step Guide
Steven Christopher
Steven Christopher

Posted on

Effortlessly Set Up a Next.js Project with ESLint: A Step-by-Step Guide

👋 Hey there! If you’re looking to set up a Next.js project with ESLint for your codebase, then you’ve come to the right place. In this post, we’ll cover everything you need to know to get started, including the installation process, why you should use ESLint, and how to create a new Next.js project.

What is ESLint and Why Should You Use It?

ESLint is a powerful tool that helps you write better and more consistent code by catching syntax errors, enforcing coding conventions, and detecting potential problems. It’s easy to set up and configure, and it works with a wide range of languages and frameworks.

Using ESLint can help you catch bugs and errors before they become big problems, and it can save you a lot of time and effort in the long run. It can also help you maintain a consistent coding style across your team, which can make your codebase more readable and easier to maintain.

Creating a Next.js Project

Before we can set up ESLint, we first need to create a new Next.js project. To do this, we can use the following command:

npx create-next-app my-app
Enter fullscreen mode Exit fullscreen mode

This will create a new Next.js project called “my-app” in your current directory. Once the project is created, you can navigate into the new directory by running:

cd my-app
Enter fullscreen mode Exit fullscreen mode

Installing Dependencies

Now that we have a new Next.js project set up, we need to install the dependencies required for ESLint. These include:

  • eslint
  • eslint-config-airbnb
  • eslint-config-prettier
  • eslint-plugin-import
  • eslint-plugin-jsx-a11y
  • eslint-plugin-react
  • eslint-plugin-react-hooks
  • @typescript-eslint/parser
  • @typescript-eslint/eslint-plugin

You can install all of these packages at once by running the following command:

npm install - save-dev eslint eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks @typescript-eslint/parser @typescript-eslint/eslint-plugin
Enter fullscreen mode Exit fullscreen mode

Configuring ESLint

Once you have installed all of the required dependencies, you need to configure ESLint to work with your Next.js project. To do this, we’ll create an .eslintrc.json file in the root directory of your project, and add the following configuration:

{
 "plugins": ["@typescript-eslint"],
 "extends": [
   "airbnb",
   "next/core-web-vitals",
   "prettier",
   "plugin:@typescript-eslint/recommended"
 ],
 "parser": "@typescript-eslint/parser",
 "overrides": [
   {
     "files": ["*.ts", "*.tsx"],
     "parserOptions": {
     "project": ["./tsconfig.json"]
   }
 }
 ],
}
Enter fullscreen mode Exit fullscreen mode

This configuration sets up ESLint to use the recommended configuration from Airbnb, along with Prettier for formatting and TypeScript support. It also includes the @typescript-eslint plugin, which allows ESLint to understand TypeScript syntax.

Wrapping Up

And that’s it! You now have ESLint set up in your Next.js project, which will help you write better and more consistent code. If you have any questions or feedback, feel free to leave a comment below.

Thanks for reading, and happy coding! 🚀

Top comments (0)