Last modified: Jul 20, 2025 By Alexander Williams
How to Install ESLint in Node.js
ESLint is a popular JavaScript linting tool. It helps improve code quality. It enforces coding standards. This guide shows how to install it in Node.js.
Prerequisites
Before installing ESLint, ensure you have Node.js installed. You can check by running node -v
in your terminal. Also, a Node.js project must be initialized.
node -v
v18.12.1
If Node.js is not installed, download it from the official website. For setting up a Node.js project, check our guide on How to Install Express Module in Node.js.
Installing ESLint
ESLint can be installed globally or locally. For most projects, a local installation is preferred. Run the following command in your project directory.
npm install eslint --save-dev
This installs ESLint as a dev dependency. The --save-dev
flag ensures it's added to devDependencies in package.json.
Initializing ESLint
After installation, initialize ESLint. Run the following command. It creates a configuration file.
npx eslint --init
You will be prompted with questions. Answer them based on your project needs. For example:
? How would you like to use ESLint? To check syntax and find problems
? What type of modules does your project use? JavaScript modules (import/export)
? Which framework does your project use? None of these
? Does your project use TypeScript? No
? Where does your code run? Browser
? What format do you want your config file to be in? JSON
This generates an .eslintrc.json file. It contains your linting rules. You can customize it later.
Configuring ESLint
The configuration file defines rules. Here’s an example of a basic setup.
{
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"indent": ["error", 2],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
This configuration enforces 2-space indentation, single quotes, and semicolons. Adjust rules as needed.
Running ESLint
To lint your files, use the following command. Replace file.js with your file name.
npx eslint file.js
ESLint will show errors and warnings. Fix them to improve code quality. For automation, add a script in package.json.
"scripts": {
"lint": "eslint ."
}
Now run npm run lint
to check all files. For more automation, consider using Nodemon.
Integrating ESLint with VS Code
For a better experience, integrate ESLint with VS Code. Install the ESLint extension. It highlights errors in real-time.
Open VS Code. Go to Extensions. Search for "ESLint" and install it. Now, errors will appear as you type.
Common Issues
If you encounter Cannot find module errors, check our guide on Fix Error: Cannot Find Module in Node.js. Ensure ESLint is properly installed.
If ESLint ignores files, check the .eslintignore file. Add patterns for files to ignore.
Conclusion
ESLint improves code quality and consistency. It’s easy to install and configure in Node.js. Follow this guide to set it up quickly.
For more Node.js modules, check our guide on Install Mongoose Module in Node.js. Happy coding!