Skip to content

Code Linting

Linting is the process of analyzing source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. Unlike formatters that focus on style, linters focus on code quality and correctness.

  • Error Prevention: Catch potential bugs before they reach production
  • Code Quality: Enforce best practices and coding standards
  • Consistency: Maintain uniform code patterns across the team
  • Learning Tool: Helps developers learn better coding practices
  • Security: Identify potential security vulnerabilities

ESLint is the most popular and configurable linting tool for JavaScript and TypeScript projects.

Terminal window
# Install ESLint
npm install --save-dev eslint
# For TypeScript projects, also install:
npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin
# For React projects:
npm install --save-dev eslint-plugin-react eslint-plugin-react-hooks

Initialize ESLint configuration:

Terminal window
npx eslint --init

Or create a .eslintrc.json file manually:

.eslintrc.json
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"no-console": "warn", // Warns about console.log statements (should be removed in production)
"no-unused-vars": "error", // Flags variables that are declared but never used
"no-undef": "error", // Catches usage of undefined variables (prevents typos)
"prefer-const": "error", // Requires const for variables that are never reassigned
"no-var": "error" // Disallows old 'var' keyword, enforces let/const instead
}
}

Add linting scripts to your package.json:

package.json
{
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx", // Check all files for linting errors
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix", // Automatically fix fixable linting issues
"lint:check": "eslint . --ext .js,.jsx,.ts,.tsx --max-warnings 0" // Fail if ANY warnings exist (strict mode)
}
}

Install the ESLint extension and configure VS Code settings:

.vscode/settings.json
{
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.run": "onType"
}

Create a .eslintignore file:

.eslintignore
# Build outputs
dist/
build/
coverage/
# Dependencies
node_modules/
# Generated files
*.min.js
*.bundle.js
# Configuration files
*.config.js
.eslintrc.js

Combine with Prettier in package.json:

package.json
{
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
]
}
}
var name = "John"; // Should use const
let age = 25; // Unused variable
if (true) { // Constant condition
console.log("Always runs"); // Console statement
}
function getUser(id) { // Missing return type
return fetch("/api/users/" + id); // Should use template literal
}
const name = "John";
if (name) {
// Conditional logic here
}
function getUser(id: string): Promise<Response> {
return fetch(`/api/users/${id}`);
}
.eslintrc.json
{
"rules": {
// Enforce specific naming conventions
"camelcase": ["error", { "properties": "always" }], // Requires camelCase for variables and properties
// Limit function complexity
"complexity": ["warn", { "max": 10 }], // Warns when functions have too many conditional paths
// Limit file length
"max-lines": ["warn", { "max": 300 }], // Warns when files exceed 300 lines (encourages smaller files)
// Require documentation for functions
"require-jsdoc": ["warn", { // Encourages JSDoc comments for functions and classes
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}]
}
}
.github/workflows/lint.yml
name: Lint Code
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm run lint:check
  • Enable linting in your editor for real-time feedback
  • Fix linting errors before committing code
  • Use consistent rules across all team members
  • Gradually introduce stricter rules to existing projects
  • Document custom rules and their reasoning
  • Don’t disable rules without good reason
  • Don’t ignore all warnings - treat them seriously
  • Don’t over-configure - start with recommended configs
  • Don’t lint generated files or dependencies

If ESLint is slow on large codebases:

.eslintrc.json
{
"ignorePatterns": ["dist/", "node_modules/", "coverage/"],
"cache": true,
"cacheLocation": ".eslintcache"
}