Linting is essential for maintaining code quality and consistency across your projects. ESLint is a widely used tool that helps developers enforce coding standards, catch potential errors early, and promote best practices. In this blog, we’ll explore how to set up and configure ESLint in a Node.js project with recommended rules to ensure your code stays clean and efficient.
Basic ESLint Configuration
To get started with ESLint in your Node.js project, you need to create an eslint.config.mjs
file. Below is a basic configuration that includes Node.js-specific linting rules and ES2021 support:
import { defineConfig } from 'eslint-define-config';
export default defineConfig({
env: {
node: true, // Enables Node.js global variables
es2021: true // Allows ES2021 syntax
},
extends: [
'eslint:recommended', // Basic recommended rules
'plugin:node/recommended' // Recommended rules for Node.js
],
parserOptions: {
ecmaVersion: 2021, // Specify ECMAScript version
sourceType: 'module' // Allow the use of ES modules
},
rules: {
// Best Practices
'no-console': 'warn', // Warn on console logs
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }], // Ignore unused variables prefixed with _
'prefer-const': 'error', // Prefer const over let for variables that are not reassigned
'no-var': 'error', // Disallow the use of var
// Style
'quotes': ['error', 'single'], // Enforce single quotes
'semi': ['error', 'always'], // Require semicolons at the end of statements
'indent': ['error', 2], // Enforce 2-space indentation
// Node-specific
'node/no-unsupported-features/es-syntax': [
'error',
{ ignores: ['modules'] } // Allow ES module syntax
],
'node/no-deprecated-api': 'warn' // Warn when using deprecated Node.js APIs
}
});
Key Rules Explained
Here’s a breakdown of the core components of this ESLint configuration:
Environment Settings
node: true
: Enables Node.js-specific global variables likeprocess
andmodule
.es2021: true
: Enables support for ES2021 syntax, making your code more modern and efficient.
Extends
eslint:recommended
: Provides a set of common rules for JavaScript, identifying potential problems.plugin:node/recommended
: Adds Node.js-specific rules, ensuring best practices for a server-side environment.
Best Practices
no-console
: Encourages proper logging tools by warning on the use ofconsole.log
, helping keep your production logs clean.no-unused-vars
: Helps identify variables that are declared but not used, maintaining cleaner code.prefer-const
: Enforces the use ofconst
for variables that don’t change, improving code readability and performance.no-var
: Disallows the use ofvar
, promoting the use oflet
andconst
for block-scoping in modern JavaScript.
Style Rules
- Quotes: Enforces consistency in using single quotes (
'
), improving readability. - Semi: Requires semicolons to end statements, reducing potential errors in JavaScript.
- Indent: Ensures consistent 2-space indentation, a common standard in Node.js projects.
Node-Specific Rules
node/no-unsupported-features/es-syntax
: Allows you to use ES modules (import
/export
), while warning about features not supported in older environments.node/no-deprecated-api
: Warns when using deprecated Node.js APIs, ensuring your code stays compatible with future versions of Node.js.
Running ESLint
Once your configuration is set, you can run ESLint using npm. First, add a script to your package.json
:
"scripts": {
"lint": "eslint ."
}
To run ESLint and check your project for issues, use the following command:
npm run lint
Conclusion
By setting up ESLint with these configurations, you’re ensuring that your Node.js project adheres to best practices, follows a consistent coding style, and avoids common pitfalls. ESLint is highly customizable, so you can adjust the rules to meet the specific needs of your project or team. Don’t forget to install the necessary packages with:
npm install eslint eslint-plugin-node --save-dev
With ESLint in place, your development process will be smoother, and your codebase will be more maintainable over time.