I have created a .env
file in my project root but I'm new to working with environments / variables and so I'm unsure how to integrate the file so I can override the stock, non-ejected react-app eslint settings.
// My .env file has just this
EXTEND_ESLINT = "true"
The c-r-a docs explain what the variable is, but not now to set it to true. Also, the section on 'Extending the ESLint config' is helpful only for once the var is set to true.
// stock create-react-app package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Internally create-react-app use babel to compile the javascript / typescript files of your application. By using craco-esbuild, you use the esbuild compiler to compile your app instead of babel. esbuild is a super fast javascript / typescript bundler and minifier written in Go.
The default WebPack configuration can be found in the create-react-app repository, but I found it easier to dump the entire config to the console and search for the terserOptions with less . Then, I replaced the build script in the package. json to use the new build.
Luckily for us, Create React App enables us to extend ESLint to use other configuration presets. This article goes into how to set up the Airbnb style guide in Create React App. It also includes a guide for when you’re using TypeScript.
CRA comes with an eslint-config-react-app base ESLint configuration. Here we are keeping it and adding additional other configs on top of it. Package eslint-config-airbnb is a config of the popular style guide that we added during ESLint init process.
Using Create React App you can easily start using React and develop your own web applications. It also includes a linter which helps you write consistent and quality code. However, I really like to use a different style guide a top of the default.
What's great with ESLint is that it's highly configurable. All you have to do is create a .eslintrc file at the root of your project, and then you can run ESLint on any files you want. Note: it is also possible to define an ESLint configuration inside a package.json file. For that, just put your configuration object in a eslintConfig key.
In the project root directory, you can create the .env
file with EXTEND_ESLINT
set to true
so as to extend the ESLint config:
EXTEND_ESLINT=true
Also this also works:
EXTEND_ESLINT = "true"
Tried with create-react-app version 3.4.1, the latest version at the time of writing.
As an example, you can override the no-unused-vars
rule in the package.json
, as below:
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint src"
},
"eslintConfig": {
"extends": ["react-app"],
"rules": {
"no-unused-vars": "off"
}
},
...
Now run the linter, e.g., npm run lint
, you will not see any warning even if you have assigned a value to a variable but never used it in your application, kind of warning which you would normally see by the default settings. For example:
const App = () => {
let myVar = 1; // Never used
...
}
Note: npm start
and npm run build
, etc., will also honour the extended rules.
By the way, the original package.json
looks like this:
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
...
Note: Another way to configure ESLint is to remove the eslintConfig
entry from the package.json
file and create .eslintrc
or .eslintrc.json
in the project root directory as below:
{
"extends": ["react-app"],
"rules": {
"no-unused-vars": "off"
}
}
[Update] If you find that react-scripts is not honouring your change to the ESLint rules, it is most likely caused by the cache. It is an open issue of the project at the moment. You can manually disable the cache in node_modules/react-scripts/config/webpack.config.js
like below:
use: [
{
options: {
cache: true, // You can set it to false
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname,
// @remove-on-eject-begin
ignore: isExtendingEslintConfig,
baseConfig: isExtendingEslintConfig
? undefined
: {
extends: [require.resolve('eslint-config-react-app')],
},
useEslintrc: isExtendingEslintConfig,
// @remove-on-eject-end
},
Note that this workaround would only be for your local development. You don't need to do anything for your build pipeline most likely, as the pipeline usually builds from scratch; so there is no such cache issue out there.
After struggling for hours, thanks to @Yuci my eslint is finally understanding my configurations.
Instead of directly fixing the package file in node_modules/react-scripts/config/webpack.config.js
, I have added npm scripts to always bust out the cache in start of npm run start
and npm run build
.
This way you don't have to fear for 'accidentally' rebuilding the package in the future by rm -rf node_modules; npm install
-- future me is not that clever.
in packages.json
, change the scripts entity like the following:
"scripts": {
"start": "npm run clear_cache:eslint && react-scripts start",
"build": "npm run clear_cache:eslint && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"clear_cache:eslint": "rm -rf node_modules/.cache/eslint-loader"
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With