This guide outlines the steps to set up a Node.js project with TypeScript, ESLint, Prettier, Husky, and lint-staged for consistent code quality and formatting.
Run the following commands after cloning the repository:
npm installnpm run husky:prepareInitialize the git repository:
git initConfigure your git repository with .gitignore and pattern commits following Conventional Commits:
Initialize a Node project:
npm init -yInstall all dependencies:
- npm i typescript @types/node -Dnpm i ts-node-dev nodemon -Dnpm i eslint eslint-config-prettier @eslint/js prettier typescript-eslint -Dnpm i husky lint-staged -DCreate a tsconfig file:
tsc --initConfigure the tsconfig file like this:
{
"compilerOptions": {
"target": "ES2016",
"module": "CommonJS",
"rootDir": "src",
"outDir": "./dist",
"moduleResolution": "Node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Configure package.json scripts:
{
"scripts": {
"start:dev": "nodemon --watch 'src/' --exec 'ts-node-dev --inspect --ignore-watch node_module src/index.ts' -e ts",
"husky:prepare": "husky install"
}
}
Create src/index.ts file.
Configure eslint:
npx eslint --initOn eslint file add this configs:
"jest": true
"project": ["./tsconfig.json"]
Create a file ".prettierrc.json and config it:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2
}
Configure "format on save".
Press "ctrl + ," and search "format on save" and mark checkbox.
Search "default formatter" and select "Prettier - Code formatter".
On eslint file, write config:
"prettier"like this:
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
]
}
Run:
npm run husky:prepareCreate file ".lintstagedrc.json" and write this config:
{
"*.ts": ["npx eslint --fix 'src/**'", "npx prettier --check 'src/**'"]
}
on pre-commit file of husky directory, write this:
npx lint-stagedAll commands into this file are executed when you commit.
chmod +x .husky/commit-msg > Comando para marcar um arquivo como "executável". echo ".git/hooks/commit-msg $1" > .husky/commit-msg > Cria um novo hook do husky para commit-msg