HomeGuidesReferenceLearn
ArchiveExpo SnackDiscord and ForumsNewsletter

Use ESLint and Prettier

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.

Setup

1

Install ESLint, Prettier, and eslint-config-universe in your project.

Terminal
npm install eslint@8 prettier eslint-config-universe --save-dev
Terminal
yarn add -D eslint@8 prettier eslint-config-universe

The eslint-config-universe library provides shared ESLint configurations for Node.js, web and universal Expo apps.

2

Create an ESLint config file called .eslintrc.js in the project root.

.eslintrc.js
module.exports = {
  root: true,
  extends: [
    'universe/native',
  ],
  rules: {
    // Ensures props and state inside functions are always up-to-date
    'react-hooks/exhaustive-deps': 'warn',
  },
};

3

Add a script to your package.json to run ESLint.

package.json
{
  "scripts": {
    "lint": "eslint ."
  }
}

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

You can lint your code manually from the command line with the script:

Terminal
npm run lint
Terminal
yarn lint

Recommended: If you're using VS Code, install the ESLint extension to lint your code as you type.

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:

metro.config.js
/* eslint-env node */
const { getDefaultConfig } = require('expo/metro-config');

/** @type {import('expo/metro-config').MetroConfig} */
const config = getDefaultConfig(
  __dirname
);

module.exports = config;

Prettier

To customize its settings, create a .prettierrc file at the root of your project and add the configuration.

Custom Prettier 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:

.eslintignore
/.expo
node_modules

Next step

eslint-config-universe

Learn more about shared ESLint configurations for Node.js, web and universal Expo apps.