ESLint Configuration for Create React App
eslint-config-react-app provides the official ESLint configuration used by Create React App projects. It ensures a consistent and opinionated linting setup, enforcing best practices for React applications, JavaScript/TypeScript syntax, accessibility, and code quality without requiring extensive manual configuration. The package is currently stable at version 7.0.1, with updates typically coinciding with major Create React App releases to align with new React features, tool updates (like Webpack, Jest, ESLint itself), and evolving language standards. Its primary differentiator is its tight integration and maintenance by the Create React App team, offering a battle-tested and low-overhead solution for linting React projects initialized with CRA. This configuration simplifies managing a complex ESLint setup, making it ideal for developers who prefer a zero-configuration start for their React projects and rely on community-vetted defaults.
Common errors
-
ESLint: Configuration for rule "react/react-in-jsx-scope" is invalid
cause This error typically occurs when using React 17's new JSX transform feature (`jsx: 'automatic'` in Babel/TypeScript) without disabling the corresponding ESLint rule that checks for explicit `import React from 'react'` in files using JSX.fixAdd `"react/react-in-jsx-scope": "off"` and `"react/jsx-uses-react": "off"` to the `rules` section of your `.eslintrc.json` file. Ensure your Babel or TypeScript configuration is set to use the automatic JSX runtime. -
Error: Cannot find module 'eslint-config-react-app'
cause The `eslint-config-react-app` package is not installed or ESLint cannot resolve its path.fixRun `npm install --save-dev eslint-config-react-app eslint` or `yarn add --dev eslint-config-react-app eslint` to install the package and its peer dependency. Verify your `.eslintrc` file correctly references `"extends": "react-app"`. -
error 'React' was used before it was defined (no-undef)
cause This usually indicates an issue with `eslint-plugin-react` configuration not correctly recognizing the React global or the new JSX transform, particularly in older setups or after partial upgrades.fixEnsure `eslint-plugin-react` is correctly configured and that your `.eslintrc.json` extends `react-app`. If using React 17+ and the new JSX transform, ensure `"react/react-in-jsx-scope": "off"` and `"react/jsx-uses-react": "off"` rules are applied as described above.
Warnings
- breaking Version 5.0.0 introduced significant updates to underlying tools including webpack 5, Jest 27, ESLint 8, and PostCSS 8. Projects upgrading from `eslint-config-react-app` v4 (or earlier) to v5 and beyond will also need to ensure their `eslint` peer dependency is at least `^8.0.0` and that other tooling (e.g., `react-scripts`) is compatible.
- breaking Version 4.0.0 introduced support for Fast Refresh, React 17 (including the new JSX transform), and TypeScript 4. Upgrading to this version might require updates to your React and TypeScript versions to fully leverage or avoid compatibility issues with the new features and syntax.
- gotcha While `npm audit` might report vulnerabilities in underlying packages like `resolve-url-loader`, `webpack-dev-server`, or `terser-webpack-plugin`, several release notes explicitly state these vulnerabilities 'did not affect Create React App projects'. This is because CRA's specific usage patterns or included polyfills mitigate the reported issues.
- deprecated Create React App 5.0.1 and later templates use `createRoot` for React 18 compatibility. While `eslint-config-react-app` itself doesn't directly enforce this, older React setup patterns might produce linting warnings or runtime issues when used with React 18.
Install
-
npm install eslint-config-react-app -
yarn add eslint-config-react-app -
pnpm add eslint-config-react-app
Imports
- ESLint Configuration
import config from 'eslint-config-react-app';
{ "extends": "react-app" } - ESLint Configuration with Jest
{ "extends": "react-app/jest" }{ "extends": [ "react-app", "react-app/jest" ] }
Quickstart
/* .eslintrc.json */
{
"extends": [
"react-app",
"react-app/jest"
],
"rules": {
// Override or add custom rules here if necessary
// For example, to relax a rule:
"no-unused-vars": "warn",
// Or to enforce a rule:
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off"
}
}
/* package.json (example dependencies) */
{
"name": "my-react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-config-react-app": "^7.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
}
}