@ngvihoa/eslint-config-best-practices
v0.1.3
Published
Reusable ESLint flat config presets for JavaScript, TypeScript, React, Next.js, and Node projects.
Maintainers
Readme
ESLint Config Best Practices
Reusable ESLint flat config presets for JavaScript, TypeScript, React, Next.js, and Node projects.
Install
npm install --save-dev eslint @ngvihoa/eslint-config-best-practicesUsage
Create eslint.config.cjs in your project:
const { base } = require("@ngvihoa/eslint-config-best-practices")
module.exports = baseFor Node projects:
const { node } = require("@ngvihoa/eslint-config-best-practices")
module.exports = nodeFor TypeScript projects:
const { typescript } = require("@ngvihoa/eslint-config-best-practices")
module.exports = typescriptFor React + TypeScript projects:
const { react } = require("@ngvihoa/eslint-config-best-practices")
module.exports = reactFor Next.js projects:
const { next } = require("@ngvihoa/eslint-config-best-practices")
module.exports = nextIf your Next.js project uses eslint.config.mjs:
import { defineConfig, globalIgnores } from "eslint/config"
import config from "@ngvihoa/eslint-config-best-practices"
const eslintConfig = defineConfig([
globalIgnores([".next/**", "out/**", "build/**", "next-env.d.ts"]),
...config.next,
])
export default eslintConfigDo not combine this preset with eslint-config-next/core-web-vitals or eslint-config-next/typescript in the same flat config. The next preset already includes React, TypeScript, and official Next.js rules, so combining both configs can redefine plugins such as jsx-a11y. Use globalIgnores for app-specific ignore patterns instead.
Fix On Save
Install the VS Code ESLint extension:
dbaeumer.vscode-eslintThen add this to your project-level .vscode/settings.json:
{
"eslint.workingDirectories": [
{
"mode": "auto"
}
],
"[javascript][javascriptreact][typescript][typescriptreact]": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "always"
}
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
]
}This lets ESLint fix auto-fixable rules whenever you save a file, such as sorting imports with best-practices/sort-imports, removing semicolons from semi, cleaning unused imports from unused-imports/no-unused-imports, and applying other safe ESLint fixes.
If you are updating this package, you should run ESLint: Restart ESLint Server from the VS Code Command Palette. Use View > Output > ESLint to diagnose extension or working-directory errors.
If you also use Prettier on save, make sure Prettier does not add semicolons back after ESLint removes them. Add a project-level .prettierrc:
{
"semi": false
}If you prefer to run fixes manually, use:
npx eslint . --fixRunning ESLint without --fix only reports problems:
npx eslint .Import Sorting
The base, typescript, react, and next presets enable the auto-fixable best-practices/sort-imports rule. It works with import-x/order to apply this group order:
- Type imports
- Node built-ins
- External packages
- Internal aliases such as
@/and~/ - Parent imports
- Sibling and index imports, including stylesheet side effects
Within a group, imports are arranged by single-line named, multiline named, and default/side-effect form. Named specifiers in multiline imports are sorted by descending length.
Save a file with imports scattered like this:
import "./product.css"
import ProductDetails from "./ProductDetails"
import Image from "next/image"
import { formatCurrency } from "../lib/currency"
import {
Price,
AddToWishlistButton,
ProductGallery,
} from "@/features/catalog"
import { readFile } from "node:fs/promises"
import type { ProductPageProps } from "@/features/catalog/types"
import { notFound } from "next/navigation"ESLint rewrites the entire import section into a predictable dependency map:
import type { ProductPageProps } from "@/features/catalog/types"
import { readFile } from "node:fs/promises"
import { notFound } from "next/navigation"
import Image from "next/image"
import {
AddToWishlistButton,
ProductGallery,
Price,
} from "@/features/catalog"
import { formatCurrency } from "../lib/currency"
import ProductDetails from "./ProductDetails"
import "./product.css"Presets
base: JavaScript recommended rules, custom import sorting, unused import cleanup, and general code-quality rules.node:baseplus Node globals and console-friendly defaults.typescript:baseplustypescript-eslintrecommended and type-aware rules.react:typescriptplus React, React Hooks, JSX runtime, and accessibility rules.next:reactplus official Next.js recommended and Core Web Vitals rules.
Per-File Documentation
Each .cjs file has Vietnamese and English documentation explaining its purpose, rules, and tradeoffs.
| File | Vietnamese | English |
| ------------------------ | ------------------------------ | ------------------------------ |
| index.cjs | VI | EN |
| eslint.config.cjs | VI | EN |
| configs/base.cjs | VI | EN |
| configs/node.cjs | VI | EN |
| configs/typescript.cjs | VI | EN |
| configs/react.cjs | VI | EN |
| configs/next.cjs | VI | EN |
Type-Aware TypeScript
The TypeScript preset uses projectService: true, so it expects a tsconfig.json in the consuming project. If you lint generated files or config files outside your TypeScript project, add an override in that project's eslint.config.cjs.
Release
Before publishing a new version:
npm test
npm run lint
npm run pack:checkReview the generated package contents, update CHANGELOG.md, and commit the source changes. Then bump the version, push the commit and tag, and publish:
npm version patch
git push origin main --follow-tags
npm publishUse npm version minor or npm version major when the release contains new features or breaking changes. The package is configured as public in package.json; run npm login first if the npm session is not authenticated.
