Prettier ESLint Integrator

16.4.2 · active · verified Sun Apr 19

Prettier-ESLint is a JavaScript utility that orchestrates code formatting by running Prettier first, then applying ESLint's `--fix` functionality. This sequential approach is crucial for maintaining consistent code style across projects, as it resolves potential conflicts between Prettier's opinionated formatting and custom ESLint rules. The current stable version is 16.4.2, with an active development branch targeting version 17 which introduces support for ESLint v9's flat configuration. The package is actively maintained with regular patch and minor releases, alongside a new major version in alpha. It differentiates itself by providing a single programmatic interface to combine these tools, ensuring that ESLint fixes are applied *after* Prettier's formatting, thus preventing ESLint from undoing Prettier's work.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically format a JavaScript string using `prettier-eslint`. It creates a temporary ESLint configuration file and a JavaScript file to illustrate the process, showing how to pass the code, file path, and configuration options. It's crucial for ESLint to correctly locate its configuration to apply fixes effectively.

import prettierEslint from 'prettier-eslint';
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';

// Create a dummy ESLint config for demonstration
const eslintConfigContent = `module.exports = {
  env: { node: true, es2021: true },
  extends: ['eslint:recommended', 'prettier'],
  parserOptions: { ecmaVersion: 'latest' },
  rules: {
    'semi': ['error', 'never'],
    'quotes': ['error', 'single'],
    'indent': ['error', 2]
  }
};
`;

// Ensure a temporary directory exists for config and file
const tempDir = join(process.cwd(), 'temp-eslint-test');
if (!existsSync(tempDir)) {
  mkdirSync(tempDir, { recursive: true });
}

const eslintConfigFile = join(tempDir, '.eslintrc.js');
const tempFilePath = join(tempDir, 'example.js');

writeFileSync(eslintConfigFile, eslintConfigContent);

const rawCode = `const   myVar  =  "Hello world" ; function   test ( ) {  console . log ( myVar )  ;  }  test ( )  ;`;
writeFileSync(tempFilePath, rawCode);

async function formatCode() {
  try {
    console.log('Original code:\n', rawCode);

    const formattedCode = await prettierEslint({
      text: rawCode,
      filePath: tempFilePath, // Essential for ESLint to find its config
      eslintConfig: { // Can be provided directly or found via filePath
        overrideConfigFile: eslintConfigFile,
      },
      prettierOptions: { printWidth: 80, tabWidth: 2, semi: false, singleQuote: true },
      fallbackPrettierOptions: { printWidth: 80, tabWidth: 2, semi: false, singleQuote: true },
      logLevel: 'debug',
    });

    console.log('\nFormatted code:\n', formattedCode);
    writeFileSync(tempFilePath.replace('.js', '.formatted.js'), formattedCode);

  } catch (error) {
    console.error('Error during formatting:', error);
  }
}

formatCode();

view raw JSON →