We use Prettier for code formatting but recommend specific practices to maximize its benefits. This guide provides our recommended standards, but each repository can have an adjusted configuration to meet its needs.
Formatters eliminate debates about code style by applying consistent rules automatically.
Rely on default configurations
The fundamental principle of using a formatter is that you should not need to care about formatting. Therefore, use Prettier’s defaults whenever possible to reduce the complexity of the project.
Use editor integrations over linting integration
Run Prettier through editor plugins that format on save, not through ESLint. Therefore, avoid:
- // .eslintrc.js
- {
- "extends": ["prettier"],
- "plugins": ["prettier"],
- "rules": {
- "prettier/prettier": "error"
- }
- }
Why? Separating formatting from linting:
Our recommended project structure aims at increasing clarity and minimizing complexity.
Avoid unnecessary src
directory
- /src
- /api
- /db
- app.js
+ /api
+ /db
+ app.js
Why? The src
directory typically implies a compilation step, which we don’t have in our Node.js applications. We run the code directly without transpilation.
Use ECMAScript Modules (ESM) consistently throughout the project:
// Do this (ESM syntax)
import express from 'express';
import { router } from './router.js';
export function someFunction() {
// ...
}
// Not this (CommonJS syntax)
const express = require('express');
const router = require('./router');
module.exports.someFunction = function() {
// ...
};
Why? ESM is the standard JavaScript module system and offers benefits like: