Using ESLint and Prettier
Edit this page
A guide on configuring ESLint and Prettier to format Expo apps.
ESLint is a JavaScript linter that helps you find and fix errors in your code. It's a great tool to help you write better code and catch mistakes before they make it to production. In conjunction, you can use Prettier, a code formatter that ensures all the code files follow a consistent styling.
This guide provides steps to set up and configure ESLint and Prettier.
ESLint
Setup
To set up ESLint in your Expo project, you can use the Expo CLI to install the necessary dependencies. Running this command also creates a eslint.config.js file at the root of your project which extends configuration from eslint-config-expo
.
# Install and configure ESLint
-
npx expo lint
From SDK 53 onwards, the created ESLint config file will use the Flat config format. However, legacy config will also be supported.
Setup instructions for SDK 50 and below
1
Install ESLint, and eslint-config-expo
in your project.
-
npx expo install eslint@8 eslint-config-expo --dev
-
npx expo install eslint@8 eslint-config-expo "--" --dev
2
Create an ESLint configuration file called .eslintrc.js at the root of your project. The configuration in .eslintrc.js extends eslint-config-expo
.
module.exports = {
extends: 'expo',
};
3
Add a script
to your package.json to run ESLint.
{
"scripts": {
"lint": "expo lint"
}
}
You can replace the .
with specific directories or files to lint. For example, if you use Expo Router, you can use the eslint app
command to lint only your routes inside the app directory.
Usage
Recommended: If you're using VS Code, install the ESLint extension to lint your code as you type.
You can lint your code manually from the command line with the npx expo lint
script:
# After ESLint has been configured, run the command again to lint your code.
-
npx expo lint
Running the above command will run the lint
script from package.json.
# Example output for npx expo lint command
/app/components/HelloWave.tsx
22:6 warning React Hook useEffect has a missing dependency: "rotateAnimation".
Either include it or remove the dependency array react-hooks/exhaustive-deps
✖ 1 problem (0 errors, 1 warning)
-
npm run lint
Environment configuration
ESLint is generally configured for a single environment. However, the source code is written in JavaScript in an Expo app that runs in multiple different environments. For example, the app.config.js, metro.config.js, babel.config.js, and app/+html.tsx files are run in a Node.js environment. It means they have access to the global __dirname
variable and can use Node.js modules such as path
. Standard Expo project files like app/index.js can be run in Hermes, Node.js, or the web browser.
You can add the eslint-env
comment directive to the top of a file to tell ESLint which environment the file is running in. For example, to tell ESLint that a file is run in Node.js, add the following comment to the top of the file:
/* eslint-env node */
const { getDefaultConfig } = require('expo/metro-config');
/** @type {import('expo/metro-config').MetroConfig} */
const config = getDefaultConfig(
__dirname
);
module.exports = config;
Prettier
Installation
To install Prettier in your project:
-
npx expo install prettier eslint-config-prettier eslint-plugin-prettier --dev
-
npx expo install prettier eslint-config-prettier eslint-plugin-prettier "--" --dev
Setup
To integrate Prettier with ESLint, update your eslint.config.js:
const { defineConfig } = require('eslint/config');
const expoConfig = require('eslint-config-expo/flat');
const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended');
module.exports = defineConfig([
expoConfig,
eslintPluginPrettierRecommended,
{
ignores: ['dist/*'],
},
]);
To integrate Prettier with ESlint, update your .eslintrc.js:
module.exports = {
extends: ['expo', 'prettier'],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error',
},
};
Note: In the above configuration, you can use
"prettier/prettier": "warn"
if you prefer these formatting issues as warnings instead of errors.
Now, when you run npx expo lint
, anything that is not aligned with Prettier formatting will be caught as an error.
To customize Prettier settings, create a .prettierrc file at the root of your project and add your configuration.
Learn more about customizing Prettier configuration.
Troubleshooting
ESLint is not updating in VS Code
If you're using VS Code, install the ESLint extension to lint your code as you type. You can try restarting the ESLint server by running the command ESLint: Restart ESLint Server
from the command palette.
ESLint is slow
ESLint can be slow to run on large projects. The easiest way to speed up the process is to lint fewer files. Add a .eslintignore file to your project root to ignore certain files and directories such as:
/.expo
node_modules
Migration to Flat config
Note: Flat config is supported in Expo SDK 53 and above.
Upgrade ESLint and eslint-config-expo
:
-
npx expo install eslint eslint-config-expo --dev
-
npx expo install eslint eslint-config-expo "--" --dev
If you haven't customized your ESLint config at all, delete your .eslintrc.js and generate the new config with:
-
npx expo lint
Alternatively, migrate your config based on the ESLint's migration guide. npx expo lint
supports both legacy and flat config, so the new config will automatically be picked up by the CLI.