Modern Node.js Project Setup with TypeScript, ESLint, Prettier, Husky & Commitlint
📌 **Prerequisites**: Node.js >= 18 and npm installed.
1️⃣ Initialize your project:
1npm init -y
2️⃣ Install Husky and lint-staged:
1npm install husky lint-staged --save-dev
2npx husky init
3️⃣ Install TypeScript and types for Node.js:
1npm install typescript ts-node @types/node --save-dev
2npx tsc --init
4️⃣ Recommended folder structure:
1📁 src/
2 ┣ 📄 app.ts
3 ┣ 📄 server.ts
4 ┣ 📁 config/
5 ┣ 📁 constants/
6 ┣ 📁 controller/
7 ┣ 📄 auth.controller.ts
8 ┣ 📁 middleware/
9 ┣ 📁 models/
10 ┗ 📁 routes/
11 ┣ 📄 auth.route.ts
12 ┣ 📁 services/
13 ┣ 📄 auth.service.ts
14 ┣ 📁 types/
15 ┣ 📁 utils/
16 ┣ 📁 view/
5️⃣ Install and configure Nodemon for development:
1npm install nodemon --save-dev
Add this to your package.json scripts:
1"scripts": {
2 "dev": "nodemon src/index.ts"
3}
6️⃣ Install and configure Commitlint:
1npm install --save-dev @commitlint/cli @commitlint/config-conventional
Create `commitlint.config.js`:
1module.exports = {
2 extends: ["@commitlint/config-conventional"],
3 rules: {
4 "type-enum": [2, "always", [
5 "feat", "fix", "refactor", "docs", "style", "revert",
6 "chore", "test", "workflow", "release", "dependency", "merge"
7 ]],
8 "subject-case": [2, "always", "sentence-case"]
9 }
10};
Add Husky commit-msg hook:
1npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
7️⃣ Install and configure ESLint:
1npm install --save-dev eslint @eslint/js typescript-eslint
Create `eslint.config.mjs`:
1// @ts-check
2import eslint from '@eslint/js';
3import tseslint from 'typescript-eslint';
4import eslintConfigPrettier from 'eslint-config-prettier';
5
6export default tseslint.config({
7 languageOptions: {
8 parserOptions: {
9 project: true,
10 tsconfigRootDir: import.meta.dirname
11 }
12 },
13 files: ['**/*.ts'],
14 extends: [eslint.configs.recommended, ...tseslint.configs.recommendedTypeChecked, eslintConfigPrettier],
15 rules: {
16 'no-console': 'error',
17 'no-useless-catch': 0,
18 quotes: ['error', 'single', { allowTemplateLiterals: true }]
19 }
20});
Run ESLint commands:
1npx eslint .
2npx eslint --fix
8️⃣ Configure lint-staged in package.json:
1"lint-staged": {
2 "**/*.ts": "eslint --fix"
3}
Add pre-commit hook:
1npx husky add .husky/pre-commit "npx lint-staged"
9️⃣ Install and configure Prettier:
1npm install --save-dev --save-exact prettier
Create `prettier.config.json`:
1{
2 "trailingComma": "none",
3 "semi": true,
4 "singleQuote": true,
5 "tabWidth": 4,
6 "bracketSameLine": true,
7 "singleAttributePerLine": true,
8 "printWidth": 150,
9 "endOfLine": "crlf"
10}
🔟 Install dotenv-flow for env management:
1npm install dotenv-flow
Create .env files like `.env`, `.env.local`, etc.
1️⃣1️⃣ Install cross-env for cross-platform env variables:
1npm install cross-env
Update dev script in package.json:
1"scripts": {
2 "dev": "cross-env NODE_ENV=development nodemon src/index.ts"
3}
🎉 Your Node.js project is now fully set up with TypeScript, linting, formatting, and commit automation!