cover-img

Creating code-style rules with ESLint for the Next.js project

Work with Next.js: Goals of this show is that you will have code style rules for your project

10 June, 2022

19

19

0

Having rules in a project I think is very important, because of that your project will be standard, tidy, easy to learn, and maintainable. One of the rules I always make is code-style using ESLint, in this show I will share my code style for the Next.js project.
Before we start if you don't have a Next.js project you can read it in the show Create Project using Next.js + TypeScript + Tailwind CSS
Let's continue...

What is ESLint?

ESLint is a type of static analysis that is frequently used to find problematic patterns or code that doesn't adhere to certain style guidelines.
ESLint will automatically analyze our code for potential bugs based on standards/rules we have set. The benefit is if your editor is already supported with ESLint it will get the feedback quickly, we don't need to manually check whether the code complies with the rules or not.
img

Error ESLint example

Rules in ESLint are configurable and customized rules can be defined and loaded. Below are some of the rules I usually use:

Enforce a specific function type for the function components

Enforce a convention in module import order

Disallows the use of variables before they are defined

Disallows unused variables

Use semicolons (;)

Tab size 2 space

Print width 80 character

Single quote

etc.

How to implement ESLint?

Here is my step to implement ESLint in the Next.js project:

1. Install dependencies

Open your root directory project, and run the command below:

2. Configuration

Create file .eslintrc.json in the root directory project, and paste the config below:
Open file prettier.config.js in the root directory project, and paste the config below:

3. Add Script

Open file package.json from the root directory project, and add the script below:

How does it work?

to make the editor automatically analyze the code I usually install `VS Code ESLint Extension`
img

VS Code Extension

Note: after installing it sometimes we need to re-open the editor
if you want to run ESLint manually, run the command below:

How fix error ESLint?

1. Manual adjustment

Open the file with the errors, and then adjust that error to the ESLint rules.

2. Use Editor

img

Fix error ESLint with VS Code

3. Comand Line

if you want to fix errors ESLint automatically, run the command below:

Conclusion

After reading this show I hope you can implement ESLint in your project and you can create your own rules.

I put this project in the GitHub repository, here you go:

References

typescript

next.js

react.js

eslint

#4weeksofshows

19

19

0

typescript

next.js

react.js

eslint

#4weeksofshows

Warman Suganda
Software Engineer | My Youtube https://bit.ly/3zvl606

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.