Dev tools and workflow · Intermediate · 22 min
Lint and format JavaScript and TypeScript with ESLint 10 and Prettier
By Padding · Updated 18 Nov 2026
node:26 (Debian) · Node.js 26.13 LTS · ESLint 10.15.0 · typescript-eslint 8.74.0 · Prettier 3.10.1
You will have a small TypeScript project where ESLint 10 with type-aware rules catches a real unawaited-promise bug, Prettier owns the formatting, npm scripts run both, and a Git hook checks only the files you stage.
On this page
Two tools keep a JavaScript or TypeScript codebase in shape, and they do different jobs. ESLint is a linter: it reads your code and reports patterns that are likely bugs, such as a promise nobody waits for. Prettier is a formatter: it reprints every file in one consistent style, so nobody argues about spaces, quotes or line breaks in code review. Set up well, they never overlap. ESLint checks what the code does, and Prettier decides how it looks.
In this tutorial you build a tiny order-saving script in TypeScript, then add ESLint 10 with the type-aware rules from typescript-eslint. Type-aware rules ask the TypeScript compiler what each value is, which lets them catch bugs a plain linter cannot. You will see one: a script that reports Saved 0 of 2 orders because it never waits for its saves to finish. Then you add Prettier, make sure the two tools cannot contradict each other, wire them into npm scripts, and finish with a Git hook that checks only the files you are about to commit.
One wrinkle in late 2026: TypeScript 7 is a native port of the compiler and does not yet ship the JavaScript API that typescript-eslint reads. The TypeScript team's advice is to keep TypeScript 6 installed under the name typescript for tools, and TypeScript 7 alongside it for tsc. You will do exactly that in the first step.
The full install is roughly 100 MB of node_modules. If you pay for data by the gigabyte, install once on a good connection. Later installs of the same versions come from npm's cache, and npm ci --prefer-offline uses the cache before the network.
Step 1 · Create the project and install both TypeScript compilers
Make a throwaway project folder, start a Git repository in it, and create a package.json that uses ES modules:
mkdir -p ~/lint-demo
cd ~/lint-demo
git init
npm init -y
npm pkg set type=module
npm pkg delete mainNow install TypeScript twice, under two names. The first line installs the TypeScript 6 compatibility package under the name typescript, which is the name typescript-eslint imports. The second installs TypeScript 7 under the name @typescript/native; its tsc command is the one npm links, so npx tsc runs the fast native compiler:
npm install --save-dev --save-exact "typescript@npm:@typescript/typescript6@6.0.2" "@typescript/native@npm:typescript@7.0.2" @types/node@26.6.11--save-exact records 6.0.2, not ^6.0.2, in package.json. Exact versions matter more for linters and formatters than for most packages: a new release can add a rule or change a formatting decision, and you want that to happen when you choose, not on someone else's machine.
Check it worked
npx tsc --version
node -e "import('typescript').then((ts) => console.log('API', ts.default.version))"The first line prints Version 7.0.2. The second prints API 6.0. followed by a patch number: that is the compiler API typescript-eslint will use.
Step 2 · Write a small script with a hidden bug
Create a tsconfig.json. It checks types without writing JavaScript (noEmit), because Node.js 26 runs .ts files directly by stripping the types. erasableSyntaxOnly keeps you to TypeScript features that can be stripped that way, and allowImportingTsExtensions lets one .ts file import another by its real name:
cat > tsconfig.json <<'JSON'
{
"compilerOptions": {
"target": "es2024",
"module": "nodenext",
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"types": ["node"],
"skipLibCheck": true
},
"include": ["src"]
}
JSONNow the code. src/orders.ts has a function that saves an order (it waits 50 milliseconds to stand in for a database write) and a function that works out an order's total. The total has a type assertion, as number, that adds nothing:
mkdir -p src
cat > src/orders.ts <<'TS'
import { setTimeout as sleep } from "node:timers/promises";
export type Order = { id: number; item: string; quantity: number; unitPrice: number; paid: boolean };
export const saved: number[] = [];
export async function saveOrder(order: Order): Promise<void> {
await sleep(50);
saved.push(order.id);
}
export function orderTotal(order: Order): number {
const total = (order.quantity * order.unitPrice) as number;
return total;
}
TSsrc/index.ts saves two orders and reports the result. The extra spaces after id: 2, are there on purpose, for Prettier to tidy later:
cat > src/index.ts <<'TS'
import { orderTotal, saveOrder, saved, type Order } from "./orders.ts";
const orders: Order[] = [
{ id: 1, item: "Data bundle 1GB", quantity: 2, unitPrice: 500, paid: true },
{ id: 2, item: "Airtime top-up", quantity: 1, unitPrice: 200, paid: false },
];
let grandTotal = 0;
for (const order of orders) {
grandTotal += orderTotal(order);
saveOrder(order);
}
console.log(`Saved ${saved.length} of ${orders.length} orders, total ${grandTotal}`);
TSFinally, one test with Node's built-in test runner:
cat > src/orders.test.ts <<'TS'
import assert from "node:assert/strict";
import { test } from "node:test";
import { orderTotal } from "./orders.ts";
test("orderTotal multiplies quantity by unit price", () => {
const total = orderTotal({ id: 9, item: "SIM card", quantity: 3, unitPrice: 100, paid: true });
assert.equal(total, 300);
});
TSCheck it worked
npx tsc
node src/index.ts
node --testnpx tsc prints nothing, which means no type errors. The test passes. But the script prints Saved 0 of 2 orders, total 1200. saveOrder returns a promise, the loop never waits for it, and the last line runs before either save finishes. The compiler is happy, because the code is type-correct. This is the kind of bug a linter with type information exists for.
Step 3 · Add ESLint with type-aware rules
Install ESLint, its own recommended rules, and typescript-eslint, which lets ESLint parse TypeScript and adds rules that use type information:
npm install --save-dev --save-exact eslint@10.15.0 @eslint/js@10.0.1 typescript-eslint@8.74.0ESLint 10 reads only the flat config format: one eslint.config.js file that exports an array of config objects. Later objects override earlier ones for the files they match. Write it:
cat > eslint.config.js <<'JS'
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
export default defineConfig(
{ ignores: ["dist/", "coverage/"] },
js.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ["**/*.js"],
extends: [tseslint.configs.disableTypeChecked],
},
);
JSWhat each object does:
ignoreson its own, with no other keys, is a global ignore: ESLint never looks insidedist/orcoverage/.node_modules/and.git/are ignored already.js.configs.recommendedturns on ESLint's core rules for common mistakes, such as unused variables and unreachable code.tseslint.configs.recommendedTypeCheckedadds the TypeScript parser and typescript-eslint's recommended rules, including the ones that need types.projectService: truetells typescript-eslint to find thetsconfig.jsonfor each file, the same way an editor does, so the rules see the same types the compiler sees.- The last object switches type-aware rules off for
.jsfiles, such as this config file, which are not part of the TypeScript project.
Run ESLint over the whole project:
npx eslint .Check it worked
ESLint exits with an error and lists three problems, something like:
src/index.ts
11:3 error Promises must be awaited, end with a call to .catch, ... @typescript-eslint/no-floating-promises
src/orders.test.ts
5:1 error Promises must be awaited, end with a call to .catch, ... @typescript-eslint/no-floating-promises
src/orders.ts
13:17 error This assertion is unnecessary since it does not change the type of the expression @typescript-eslint/no-unnecessary-type-assertion
✖ 3 problems (3 errors, 0 warnings)
1 error and 0 warnings potentially fixable with the `--fix` option.The first line is the real bug: line 11 of index.ts is saveOrder(order);, the call nobody waits for. no-floating-promises can only know that saveOrder returns a promise because it asked the TypeScript compiler, which is what "type-aware" means. The other two are a false alarm and some tidying, handled in the next two steps.
Step 4 · Turn off a rule for test files, and say why
The second no-floating-promises report is the test(...) call in the test file. Node's test() does return a promise, but the test runner tracks every top-level test itself and waits for it, so leaving that promise alone is safe. Telling ESLint so is a decision, and a decision belongs in the config with its reason next to it, where the next person can read it and undo it if it stops being true.
Rewrite eslint.config.js with one more object at the end:
cat > eslint.config.js <<'JS'
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
export default defineConfig(
{ ignores: ["dist/", "coverage/"] },
js.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ["**/*.js"],
extends: [tseslint.configs.disableTypeChecked],
},
{
files: ["**/*.test.ts"],
rules: {
// node:test waits for every top-level test() itself, so the promise
// test() returns is safe to leave unhandled in test files.
"@typescript-eslint/no-floating-promises": "off",
},
},
);
JSThe files pattern limits the change to test files. Application code keeps the rule at full strength. The trade-off is that a forgotten await inside a test body is no longer reported either. If that worries you, the rule has a narrower allowForKnownSafeCalls option that exempts only test from node:test; see the rule's page on typescript-eslint.io.
Check it worked
Run npx eslint . again. It still exits with an error, but now reports two problems: the floating promise in src/index.ts and the unnecessary assertion in src/orders.ts. The test file is no longer listed.
Step 5 · Let ESLint fix what it can, then fix the bug yourself
Some rules know how to rewrite the code they complain about. --fix applies those fixes in place and reports whatever is left:
npx eslint --fix .The as number is gone from src/orders.ts, which now reads const total = (order.quantity * order.unitPrice);. The leftover brackets are harmless, and Prettier removes them later. One problem remains, because there is no safe automatic fix for a floating promise: ESLint cannot know whether you meant to wait, to handle errors with .catch, or to fire and forget.
Here you meant to wait. Rewrite src/index.ts with await in front of the call. The file is an ES module, so await works at the top level:
cat > src/index.ts <<'TS'
import { orderTotal, saveOrder, saved, type Order } from "./orders.ts";
const orders: Order[] = [
{ id: 1, item: "Data bundle 1GB", quantity: 2, unitPrice: 500, paid: true },
{ id: 2, item: "Airtime top-up", quantity: 1, unitPrice: 200, paid: false },
];
let grandTotal = 0;
for (const order of orders) {
grandTotal += orderTotal(order);
await saveOrder(order);
}
console.log(`Saved ${saved.length} of ${orders.length} orders, total ${grandTotal}`);
TSCheck it worked
npx eslint .
node src/index.tsESLint prints nothing and exits with status 0. The script now prints Saved 2 of 2 orders, total 1200.
Step 6 · Add Prettier and keep it from arguing with ESLint
Install Prettier and eslint-config-prettier. The second package is a config that switches off every ESLint rule about layout, so the only tool with an opinion on formatting is Prettier:
npm install --save-dev --save-exact prettier@3.10.1 eslint-config-prettier@10.1.8Prettier works with almost no settings, and that is the point. Write a .prettierrc with the one setting this project changes, a line length of 100 characters instead of 80, and a .prettierignore for files Prettier should leave alone. package-lock.json is written by npm, so formatting it would only cause churn:
cat > .prettierrc <<'JSON'
{
"printWidth": 100
}
JSON
cat > .prettierignore <<'TXT'
dist/
coverage/
package-lock.json
TXTPrettier also skips everything in .gitignore and node_modules/. Now add eslint-config-prettier to the ESLint config. It must be the last entry, so it overrides any layout rule that an earlier config turned on:
cat > eslint.config.js <<'JS'
import js from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
export default defineConfig(
{ ignores: ["dist/", "coverage/"] },
js.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
files: ["**/*.js"],
extends: [tseslint.configs.disableTypeChecked],
},
{
files: ["**/*.test.ts"],
rules: {
// node:test waits for every top-level test() itself, so the promise
// test() returns is safe to leave unhandled in test files.
"@typescript-eslint/no-floating-promises": "off",
},
},
// Last, so it switches off every formatting rule above. Prettier owns layout.
eslintConfigPrettier,
);
JSAsk Prettier which files it would change:
npx prettier --check .It lists src/index.ts and src/orders.ts with [warn] in front and exits with an error. --check never changes a file, which makes it the right command for CI. --write reformats the files in place:
npx prettier --write .Check it worked
npx prettier --check .
npx eslint-config-prettier src/index.tsPrettier prints All matched files use Prettier code style!. The extra spaces in src/index.ts are gone, and src/orders.ts now reads const total = order.quantity * order.unitPrice;. The second command is a helper that ships with eslint-config-prettier: it loads your ESLint config for that file and reports any rule that would fight Prettier. It prints No rules that are unnecessary or conflict with Prettier were found. If you ever move eslintConfigPrettier above another config, run it again.
Step 7 · Add npm scripts for lint, format and format:check
Nobody should need to remember flags. Add scripts to package.json, so every developer and every CI job runs the same commands:
npm pkg set scripts.lint="eslint ."
npm pkg set scripts.lint:fix="eslint --fix ."
npm pkg set scripts.format="prettier --write ."
npm pkg set scripts.format:check="prettier --check ."
npm pkg set scripts.typecheck="tsc"
npm pkg set scripts.test="node --test"Use lint, format:check, typecheck and test in CI, because they only report. Use lint:fix and format on your own machine, because they change files. Most editors can also run ESLint and Prettier on save through their extensions; the scripts stay the source of truth either way.
Check it worked
npm run lint
npm run format:check
npm run typecheck
npm testAll four exit with status 0. npm run lint prints only the command it ran, format:check prints All matched files use Prettier code style!, and npm test reports pass 1 and fail 0.
Step 8 · Check only staged files before each commit with lint-staged
Linting a whole large project on every commit is slow, and it blocks you over problems in files you did not touch. lint-staged runs your commands only on the files staged for the commit you are making.
First, commit the project as it is now, so there is a clean starting point:
cat > .gitignore <<'TXT'
node_modules/
TXT
git add -A
git commit -m "Set up ESLint and Prettier"Install lint-staged and tell it what to run for each kind of file. ESLint runs with --max-warnings=0, so a warning blocks the commit as firmly as an error. Prettier checks the staged files and does not rewrite them, so the commit never contains changes you did not see:
npm install --save-dev --save-exact lint-staged@17.6.2
cat > .lintstagedrc.json <<'JSON'
{
"*.{ts,js}": ["eslint --max-warnings=0", "prettier --check"],
"*.{json,md,yml,yaml}": "prettier --check"
}
JSONGit runs a script called pre-commit before every commit and cancels the commit if the script fails. Keep the hook in the repository, in a .githooks folder, and point Git at it. Every teammate runs the same git config line once after cloning:
mkdir -p .githooks
cat > .githooks/pre-commit <<'SH'
#!/bin/sh
npx --no-install lint-staged
SH
chmod +x .githooks/pre-commit
git config core.hooksPath .githooks
git add -A
git commit -m "Run lint-staged before each commit"The commit goes through, and you see lint-staged run its checks on the staged .json files first. If your team already uses the pre-commit framework or Husky to manage hooks, call npx lint-staged from there instead.
Now try to commit the original bug. Add a refund function that forgets its await:
cat >> src/orders.ts <<'TS'
export async function refund(order: Order): Promise<void> {
saveOrder(order);
}
TS
git add src/orders.tsgit commit -m "Add refunds"Check it worked
The commit is refused. lint-staged reports that it ran on 1 file, src/orders.ts, and ESLint lists two errors: no-floating-promises for the unawaited saveOrder(order), and require-await because refund is async but never waits for anything. git log --oneline still shows only your two earlier commits. Throw the broken change away:
git restore --staged --worktree src/orders.ts
git status --short
git log --onelinegit status --short prints nothing, and the log shows two commits.
Step 9 · Clean up
The demo lives in its own folder. Node.js and Git stay installed:
cd ~
rm -rf ~/lint-demoNext steps
- Run the same checks for every pull request: run Node.js tests in GitHub Actions, and add
npm run lintandnpm run format:checkto the job. - Give editors the same indentation and line endings Prettier expects: share editor settings with EditorConfig.
- Go further with the compiler itself: set up a TypeScript 7 project.
Related tutorials
- Write a README people actually readYou will have rewritten the README of a small open-source tool so that a stranger can tell what it does, install it, run it and find help in under five minutes, and you will have proved it by following it in a fresh folder.
- Use AI coding assistants responsiblyYou will have a one-page policy for using AI coding assistants on your team, covering what goes into a prompt, how generated code and new packages are checked, what an agent may run without asking and who approves anything that deploys.
- Share indentation and line-ending rules across editors with EditorConfigYou will have a mixed Python, JavaScript, Makefile and Markdown repository with one .editorconfig that every editor follows, an editorconfig-checker run that passes, and a .gitattributes file that keeps Git storing LF line endings.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials