DEV Community

Cover image for Setup ESLint and Prettier in Nuxt 4
Matis
Matis

Posted on

Setup ESLint and Prettier in Nuxt 4

In this post, I’ll show you how to use ESLint and Prettier in a Nuxt 4 project, step by step.

Add ESLint

Follow the @nuxt/eslint documentation if you want more details.

Run this command in your project:

npx nuxi module add eslint 
Enter fullscreen mode Exit fullscreen mode

If you’re using TypeScript, make sure to install it too:

npm install -D typescript 
Enter fullscreen mode Exit fullscreen mode

After this, you’ll see:

  • The @nuxt/eslint module added in your nuxt.config.ts.
  • A new eslint.config.mjs file generated for you.
// @ts-check import withNuxt from './.nuxt/eslint.config.mjs' export default withNuxt( // Add your custom ESLint config here ) 
Enter fullscreen mode Exit fullscreen mode

Add Prettier

Install the dependencies:

npm install -D prettier eslint-config-prettier eslint-plugin-prettier 
Enter fullscreen mode Exit fullscreen mode

Create a .prettierrc file at the root of your project. Example config:

{ "semi": false, "singleQuote": true, "tabWidth": 2, "trailingComma": "none", "printWidth": 100 } 
Enter fullscreen mode Exit fullscreen mode

Then update your eslint.config.mjs to use the Prettier plugin:

// @ts-check import withNuxt from './.nuxt/eslint.config.mjs' import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended' export default withNuxt([eslintPluginPrettierRecommended]) 
Enter fullscreen mode Exit fullscreen mode

Add some scripts in your package.json to make it easy to lint and format your code with one command.

{ "scripts": { "lint": "eslint .", "lint:fix": "eslint . --fix", "format": "prettier --check .", "format:fix": "prettier --write ." } } 
Enter fullscreen mode Exit fullscreen mode

Configure Your Editor

To make ESLint and Prettier work properly, set up your editor (like VS Code) with the right settings. Install the ESLint and Prettier extensions, and enable format on save to keep your files clean automatically.

If you’re using VS Code, you can create a .vscode/settings.json file like this:

{ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true } 
Enter fullscreen mode Exit fullscreen mode

Now your Nuxt 4 project has ESLint and Prettier to keep your code clean and consistent.
You can check out the config here: GitHub Repo

Thanks for reading! 👋

Top comments (2)

Collapse
 
dustinbowers profile image
Dustin Bowers

Fantastic! This is exactly what I was looking for

Collapse
 
__7ba0dede36c5a6 profile image
JuliyaBoiko

Thank you very very much for this post!!! I've spent many time before i found you post