Prettier ESLint CLI
prettier-eslint-cli is a command-line interface (CLI) tool designed to streamline code formatting by integrating Prettier with ESLint. It first formats code using Prettier, then passes the output to ESLint for auto-fixing any remaining linting issues according to your ESLint configuration. This approach helps resolve potential conflicts between Prettier's strict formatting and ESLint's stylistic rules. The current stable version is 8.0.1, though alpha versions for v9 are actively being released, indicating ongoing development, including support for ESLint v9's new flat config system. Key differentiators include its single-command workflow for combined formatting and linting, and its intelligent handling of tool conflicts by prioritizing Prettier's output before ESLint's fixes. It primarily targets JavaScript and TypeScript projects that utilize both tools for code quality.
Common errors
-
Error: Cannot find module 'prettier-eslint'
cause The `prettier-eslint` package, which provides the core formatting and fixing logic, is a required peer dependency but is not installed in your project.fixInstall `prettier-eslint` explicitly in your project: `npm install prettier-eslint` or `yarn add prettier-eslint`. -
Error: Minimum Node.js version not met. Expected >=16.10.0 but got vX.Y.Z
cause The installed Node.js version is older than the minimum requirement for `prettier-eslint-cli` v8.0.0 and above.fixUpgrade your Node.js runtime to version 16.10.0 or newer. You can use tools like `nvm` (`nvm install 18 && nvm use 18`) or `volta` (`volta install node@18`). -
Formatting error: Some files could not be formatted. This may indicate an issue with your Prettier or ESLint configuration.
cause This often occurs when there's an unresolvable conflict between Prettier's formatting and ESLint's auto-fix rules, or if there are syntax errors in the input files that prevent successful parsing by either tool.fixFirst, run Prettier and ESLint separately (`prettier --check <files>` and `eslint --fix <files>`) to pinpoint which tool is failing. Then, review your `.prettierrc` and `.eslintrc` files for conflicting rules or syntax errors in the processed code. Ensure `eslint-config-prettier` is properly configured. -
Command not found: prettier-eslint-cli
cause The `prettier-eslint-cli` binary is not located in your system's PATH, or the package is not installed globally, or it's not accessible via `npx` from your current directory.fixEnsure the package is installed in your project (`npm install prettier-eslint-cli --save-dev`) and always run it using `npx prettier-eslint-cli`. If you want to use it globally, install it with `npm install -g prettier-eslint-cli`.
Warnings
- breaking Version 9.0.0-alpha.0 introduces explicit support for ESLint v9's new flat configuration system (`eslint.config.js`). Projects still relying on the legacy `.eslintrc` configuration format may encounter compatibility issues and should either update their ESLint setup or remain on `prettier-eslint-cli` v8.
- breaking Version 8.0.0 dropped support for Node.js versions older than 16.10.0. Attempting to run `prettier-eslint-cli` v8 or newer on unsupported Node.js versions will result in runtime errors.
- breaking Version 7.0.0 included significant dependency bumps and internal refactorings. While the CLI was updated to better handle the `prettier-eslint` peer dependency, users might still need to ensure `prettier-eslint` is explicitly installed to avoid functionality issues.
- gotcha Conflicting Prettier and ESLint configurations are a common source of issues, potentially leading to inconsistent formatting, unexpected errors, or even infinite formatting loops if rules continuously revert each other's changes.
Install
-
npm install prettier-eslint-cli -
yarn add prettier-eslint-cli -
pnpm add prettier-eslint-cli
Imports
- prettier-eslint-cli (binary)
import { format } from 'prettier-eslint-cli'npx prettier-eslint-cli --write 'src/**/*.js'
- Configuration options
const config = { printWidth: 80 }; prettierEslintCli.format(code, config);npx prettier-eslint-cli --print-width 80 --single-quote --write 'src/**/*.ts'
- Glob patterns
npx prettier-eslint-cli src/index.js,src/utils.js
npx prettier-eslint-cli 'src/**/*.{js,ts}' 'test/**/*.test.js'
Quickstart
#!/usr/bin/env bash
# Create a directory and some dummy JavaScript and TypeScript files
mkdir -p src
echo "const foo = \"bar\"; function add(a,b){return a + b;}" > src/file1.js
echo "let x = 10; const myObject = { key: 'value', other: 123 };" > src/file2.ts
# Install the necessary peer dependencies (Prettier, ESLint, prettier-eslint)
# This ensures that prettier-eslint-cli has the underlying tools it needs.
npm install --save-dev prettier eslint prettier-eslint
# Optionally, create minimal configuration files for prettier and eslint
# .prettierrc.json
echo '{"singleQuote": true, "semi": true, "printWidth": 80}' > .prettierrc.json
# .eslintrc.js (using recommended configs for demonstration)
echo 'module.exports = { extends: ["eslint:recommended", "prettier"] };' > .eslintrc.js
# Run prettier-eslint-cli to format and fix the files in place
npx prettier-eslint-cli --write 'src/**/*.js' 'src/**/*.ts'
# Output the content of the formatted files to see the changes
echo "\n--- Formatted src/file1.js ---"
cat src/file1.js
echo "\n--- Formatted src/file2.ts ---"
cat src/file2.ts
# Expected output for src/file1.js (example, exact depends on config):
# const foo = 'bar';
# function add(a, b) {
# return a + b;
# }