

How to setup Next.js project with TypeScript, ESLint and Prettier
source link: https://dev.to/samsonsham/how-to-setup-nextjs-project-with-typescript-eslint-and-prettier-1apk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
Have you ever struggled what packages should be installed to set up a project? Should I install eslint-config-airbnb
or eslint-config-airbnb-base
? What is eslint-plugin-jsx-a11y
? Is it still needed to install eslint-plugin-react
and eslint-plugin-react-hooks
?
When I start doing research, I have seen people installed a pile of packages. I always question do I really need them all.
It's good to have a cheat sheet to follow so that every time we can create a clean new project with all toolkits ready. Here is mine for a Next.js project.
The VS Code Config part is based on this post How to get ESlint and Prettier to play nice in VS Code?. From which a new setting of codeActionsOnSave
is introduced.
Toolkits:
- Typescript
- ESLint
- Prettier
- Editor Ruler (80,100,120, color: hotpink)
Pre-requisite
VS Code Config
-
Cmd + Shift + P
to open Command Palette - Type
setting
- Select
Preferences: Open Settings (JSON)
- Add below config:
{
"editor.rulers": [80, 100, 120],
"workbench.colorCustomizations": {
"editorRuler.foreground": "#FF69B4"
},
"editor.formatOnSave": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.formatDocument": true,
"source.fixAll.eslint": true
}
}
Enter fullscreen mode
Exit fullscreen mode
Create new project
Next.js - TypeScript
Run below command:
npx create-next-app@latest --ts <MY_APP_NAME>
Enter fullscreen mode
Exit fullscreen mode
Setting up ESLint
Since version 11.0.0, Next.js already integrated ESLint. We neither have to install ESLint package nor create .eslintrc.json
file manually.
eslint-config-next
could be found in package.json
which below 3 recommended rule-set are used.
- eslint-plugin-react
- eslint-plugin-react-hooks
- eslint-plugin-next
You can check eslint-config-next for more details (It's even included @typescript-eslint/parser
and jsx-a11y
).
Note: jsx-a11y is a static AST checker for accessibility rules on JSX elements. npm web site shows its supported rules.
So, I am happy with the built-in parser and rule-sets but it lacks the airbnb style. According to Next.js, it is not recommended to install overlapping ESLint plugin
This eliminates the risk of collisions or errors that can occur due to importing the same plugin or parser across multiple configurations.
So I only install eslint-config-airbnb-base
additionally then should be good to go.
npm i eslint-config-airbnb-base
Enter fullscreen mode
Exit fullscreen mode
Add airbnb-base
to .eslintrc.json
{
"extends": ["next/core-web-vitals", "airbnb-base"]
}
Enter fullscreen mode
Exit fullscreen mode
Note: eslint-config-airbnb is a collection of a bunch of plugins including eslint-plugin-import
, eslint-plugin-react
, eslint-plugin-react-hooks
, and eslint-plugin-jsx-a11y
while eslint-config-airbnb-base is only the core part of airbnb style.
If you want to customise the rules, set the rule to warn
or off
manually in .eslintrc.json
.
{
"extends": ["next/core-web-vitals", "airbnb-base"],
"rules": {
"no-unused-vars": "warn"
}
}
Enter fullscreen mode
Exit fullscreen mode
Setting up Prettier
Now VS Code will alert you with the error underline. We want Prettier to help format our code nicely.
So we create a new file .prettierrc
. And add below:
{
"singleQuote": true
}
Enter fullscreen mode
Exit fullscreen mode
Should I specify parser in Prettier?
From Prettier:
Prettier automatically infers the parser from the input file path, so you shouldn’t have to change this setting.
Side Dish
Prettier has not yet format long string. There is a discussion on break strings in Prettier community
Conclusion
I hope you got some basic ideas what packages we already have by default and minimised the amount of packages to be installed for basic setup. Now I have a clean project with all the toolkits I need and without packages that might cause collision.
After all, you should only install those as devDependencies
and won't affect the size of production build (For Typescript, I am not sure. Official doc recommends to install globally, but someone in this discussion said global install would be an anti-pattern. What do you think? Do you install typescript globally or locally, and why? Please let me know).
Things to think about
- As Prettier formats your code on save, some files like
index.tsx
won't be formatted unless you open it and save it once. You can run the commandnpm run lint
to lint whole project so that no file would be left unformatted. I wonder if there is any tools could help auto formatting after lint. - When learning typescript, there comes a term tsc means typescript compiler. I am not sure when to use it or do I need to use it manually cos I have built some typescript projects and never use it once.
Recommend
-
49
Originally posted on my blog . When it comes to linting TypeScript code, there are two major linting options to c...
-
52
Photo by
-
9
-
12
In this blog, I will teach you, how to set up eslint, prettier with TypeScript and React. Even if you are not using TypeScript or react you can still follow along. In case if you don't know what eslint and prettier is: Eslint is...
-
0
VSCode - ESLint, Prettier & Airbnb Setup 1. Install ESLint & Prettier extensions for VSCode Optional - Set format on save and any global prettier options 2. Install Packages npm i -D eslint pretti...
-
86
-
10
Table of Content 📌 Introduction 📌
-
5
Steps to get started with Expo, Typescript, ESLint and Prettier The first step is to use the Expo CLI to initialize the project. If you don't have the latest version of the Expo CLI tool, (or you don't have it installed) run
-
8
Setup a Node.js project with Typescript, ESLint, Prettier, Husky
-
8
Steps to setup Pret...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK