Logo

dev-resources.site

for different kinds of informations.

ESLint x Prettier: The Right Way To Start A JavaScript Project

Published at
6/29/2024
Categories
javascript
eslint
prettier
Author
dmnchzl
Categories
3 categories in total
javascript
open
eslint
open
prettier
open
Author
7 person written this
dmnchzl
open
ESLint x Prettier: The Right Way To Start A JavaScript Project

In 2022, ESLint (v8.21.0) introduced a new configuration system nicknamed "Flat Config." This post is dedicated to this edition.

It's very easy to initialize a new JavaScript project; you just need a code editor, a runtime environment, a terminal, and you're good to go!

Similarly, it's just as easy to ruin a web project by making the code unreadable, unmaintainable, and thus generating technical debt...

Indeed, since JavaScript is a very loosely typed language, anything is quickly allowed:

  • Omitting semicolons at the end of lines;
  • Using single and double quotes within the same block of code;
  • Declaring variables but not using them;
  • Lines of code that are too long...

This flexibility of the language is both a strength and a weakness, which should be addressed by defining best practices before development begins. Thus, properly structured code will promote collaborative work and the scalability of the project.

To achieve this, there are two essential tools that will bring rigor to your web project: the linter and the formatter.

Definitions

Linter: A code analysis tool that detects syntax issues.

Formatter: A tool that formats the code to make it readable.

NB: I insist on the fact that these are two distinct tools. Although the linter can format some parts of the code, it isn't its role. This task is reserved for the formatter.

Starting Your JavaScript Project

To illustrate how each tool works, I suggest creating a new JavaScript project (without a framework) using ViteJS.

npm create vite@latest my-awesome-project -- --template vanilla
Enter fullscreen mode Exit fullscreen mode

Like a recipe, let's install the initial dependencies provided with ViteJS, and then add the new libraries: ESLint and Prettier!

npm install --save-dev eslint @eslint/js prettier eslint-config-prettier eslint-plugin-prettier
Enter fullscreen mode Exit fullscreen mode

NB: For future reference, a project scaffolded with ViteJS operates directly in module mode. This means your .js files are implicitly .mjs files. Likewise, you will need to explicitly name your files .cjs to write with CommonJS syntax.

Once the linter and formatter dependencies are correctly installed, you still need to create their respective configuration files: eslint.config.js and prettier.config.js.

NB: ESLint and Prettier configuration files can take several forms, including the following: .eslintrc.js, .eslintrc.cjs, .eslintrc.json, .eslintrc.yaml, .prettierrc, .prettierrc.json, .prettierrc.yaml, .prettierrc.js, prettier.config.cjs, or .prettierrc.toml.

Linter Configuration

Let's start by setting up the linter to use it in our new project.

Here is an initial version of the eslint.config.js file:

import globals from 'globals';
import js from '@eslint/js';

export default [
  {
    files: ['**/*.js', '**/*.jsx']
  },
  {
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node
      }
    }
  },
  js.configs.recommended,
  {
    rules: {
      'no-console': 'warn'
    }
  }
];
Enter fullscreen mode Exit fullscreen mode

This initial configuration is sufficient to run ESLint from a new terminal to check the syntax of JavaScript files: npx eslint [file|dir]

I recommend adding this execution as a script in your package.json: npm run lint

{
  "name": "my-awesome-project",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "lint": "eslint"
  },
  "devDependencies": {
    "@eslint/js": "^9.5.0",
    "eslint": "^9.5.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.1.0",
    "prettier": "^3.3.0",
    "vite": "^5.2.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

By adding a console.log() statement in a .js file of your project, you should get the following result after invoking the linter.

/home/dmnchzl/my-awesome-project/counter.js
  5:5  warning  Unexpected console statement  no-console

✖ 1 problem (0 errors, 1 warning)
Enter fullscreen mode Exit fullscreen mode

Prettier Configuration

Now, let's set up the formatter by editing the prettier.config.js file:

export default {
  arrowParens: 'avoid',
  printWidth: 120,
  semi: true,
  singleQuote: true,
  trailingComma: 'none',
};
Enter fullscreen mode Exit fullscreen mode

NB: Prettier has a default configuration. In the above file, I only override the parameters I want for my project.

Similar to ESLint, you now just need to run Prettier (npx prettier --write [file|dir]) to format the JavaScript files.

It's also possible to save a new script in your package.json: npm run format

{
  "name": "my-awesome-project",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "lint": "eslint",
    "format": "prettier --write \"**/*.{js,jsx}\""
  },
  "devDependencies": {
    "@eslint/js": "^9.5.0",
    "eslint": "^9.5.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.1.0",
    "prettier": "^3.3.0",
    "vite": "^5.2.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

ESLint x Prettier

The last step is to interface the two tools! This means controlling the formatter's syntax through the linter.

To do this, simply add Prettier's configuration to the ESLint rules (don't forget to enable the plugin):

import globals from 'globals';
import js from '@eslint/js';
import prettierRecommended from 'eslint-plugin-prettier/recommended';

export default [
  {
    files: ['**/*.js', '**/*.jsx']
  },
  {
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node
      }
    }
  },
  js.configs.recommended,
  prettierRecommended,
  {
    rules: {
      'no-console': 'warn',
      'prettier/prettier': [
        'warn',
        {
          arrowParens: 'avoid',
          printWidth: 120,
          semi: true,
          singleQuote: true,
          trailingComma: 'none'
        }
      ]
    }
  }
];
Enter fullscreen mode Exit fullscreen mode

And there you go! From now on, ESLint will be able to alert you whenever the Prettier format is not respected.

Try removing one or more semicolons at the end of a line or misaligning your lines...

/home/dmnchzl/my-awesome-project/counter.js
  2:18  warning  Insert `;`                      prettier/prettier
  3:22  warning  Replace `(count)` with `count`  prettier/prettier
  4:20  warning  Insert `;`                      prettier/prettier
  5:46  warning  Insert `;`                      prettier/prettier
  6:4   warning  Insert `;`                      prettier/prettier
  7:67  warning  Insert `;`                      prettier/prettier
  8:16  warning  Insert `;`                      prettier/prettier
Enter fullscreen mode Exit fullscreen mode

Now you know that the rigor of a web project does not come alone; it also relies on many tools, including the linter and the formatter.

To go further, I invite you to install the ESLint and Prettier extensions, depending on your favorite code editor (Visual Studio Code in my case). This way, you will benefit from additional features such as syntax highlighting when linting rules are not followed, or automatic code formatting upon file saving.

Enjoy 👍

prettier Article's
30 articles in total
Favicon
VSCode Prettier format on save
Favicon
Beyond Spellcheck: How Static Analysis Tools Enhance Collaboration in Coding
Favicon
Setup Eslint Prettier in a TypeScript project with mongoose ODM
Favicon
Vue3 + ESLint 9.13.0 + Prettier +TypeScript and VSCode Autoformat on Save
Favicon
Configurando Prettier, ESLint y Husky en Angular
Favicon
Setup Prettier Pada Nextjs Tailwind Project
Favicon
Regras customizáveis para Prettier + Eslint em React
Favicon
Useful VS Code Extensions for JS
Favicon
ESLint x Prettier: The Right Way To Start A JavaScript Project
Favicon
How to Set Up ESLint and Prettier in a TypeScript Project
Favicon
Customizable rules for Prettier + Eslint in React
Favicon
Tired of Messy Code?🥴
Favicon
Streamline Your Tailwind CSS Workflow with Prettier Plugin Enhancements
Favicon
Why Do I Love Code Formatters?
Favicon
How to set up Eslint and prettier
Favicon
Configure Eslint, Prettier and show eslint warning into running console vite react typescript project
Favicon
Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint
Favicon
Setup Eslint + Prettier for code standardization in React
Favicon
Setup Eslint + Prettier para padronização de código em React
Favicon
How to prevent Prettier putting a full stop on a new line after a link
Favicon
Moving "server-only" to the top of the imports
Favicon
My opinion about opinionated Prettier: 👎
Favicon
Setting Up Express development environment (Typescript, Eslint, Prettier)
Favicon
Biome.js : Prettier+ESLint killer ?
Favicon
Most elemental code formatting for Typescript/Javascript: .editorconfig vs prettier
Favicon
Setup Next.js 14 project with Eslint + Prettier + Tailwind CSS
Favicon
Prettier for PHP File Formats in VSCode
Favicon
Angular 14 + Prettier + Husky Setup
Favicon
แชร์ไฟล์ Prettier ง่ายๆ แค่ปลายนิ้ว!
Favicon
NextJS 14'te Kod Kalitesini Artırma: ESLint, Prettier ve Husky Kullanımı

Featured ones: