npx create-next-app@latest
What is your project named? my-app
Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
What import alias would you like configured? @/*npm install --save-dev prettier eslintCreate a .prettierrc file in the root of your project:
{
"semi": true,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "es5"
}If you use Visual Studio Code, you can configure it to use Prettier automatically. Add the following settings to your settings.json:
// optional
{
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}Create a .eslintrc.json file in the root of your project:
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"next/core-web-vitals",
"next/typescript",
"eslint:recommended",
"plugin:@next/next/recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
"prettier/prettier": ["error", { "endOfLine": "auto" }],
"react/react-in-jsx-scope": "off"
}
}To ensure Prettier and ESLint work together seamlessly, install the following plugins:
npm install --save-dev eslint-plugin-prettier eslint-config-prettierUpdate your .eslintrc.json to include Prettier configurations:
"plugins": ["prettier"],Add the following script to your package.json:
"scripts": {
"lint": "eslint . --fix"
}Integrating Tailwind CSS with Prettier
Setting Up a Pre-commit Hook with Lint-staged
To ensure your Tailwind CSS classes are sorted automatically, use the prettier-plugin-tailwindcss
npm install --save-dev prettier-plugin-tailwindcssAdd the prettier-plugin-tailwindcss to your .prettierrc:
{
"plugins": ["prettier-plugin-tailwindcss"]
}This plugin will automatically sort tailwind classes, ensuring a consistent order.
To ensure your code is linted and formatted before each commit, set up a pre-commit hook using lint-staged and husky.
npm install --save-dev husky lint-stagedAdd the following configuration to your package.json:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"prettier --write"
],
"*.ts": [
"eslint --fix",
"prettier --write"
],
"*.jsx": [
"eslint --fix",
"prettier --write"
],
"*.tsx": [
"eslint --fix",
"prettier --write"
]
}This is how it looks in your package.json
{
"name": "nextjs14-setup",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint . --fix",
"prettier": "prettier --write ."
},
"dependencies": {
"next": "14.2.15",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8.57.1",
"eslint-config-next": "14.2.15",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"postcss": "^8",
"prettier": "^3.3.3",
"tailwindcss": "^3.4.1",
"typescript": "^5"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"eslint --fix",
"prettier --write"
],
"*.ts": [
"eslint --fix",
"prettier --write"
],
"*.jsx": [
"eslint --fix",
"prettier --write"
],
"*.tsx": [
"eslint --fix",
"prettier --write"
]
}
}