Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share
BE Node.js (TS)
This guide provides a step-by-step setup for a modern Node.js backend project using TypeScript, including ESLint, Prettier, Husky, Jest, commit hooks, and semantic-release.
1. Install core dev dependencies
Copied!
1npm install -D \
2 typescript ts-node @types/node \
3 eslint prettier \
4 eslint-config-prettier eslint-plugin-prettier \
5 @typescript-eslint/eslint-plugin @typescript-eslint/parser \
6 jest ts-jest @types/jest supertest \
7 husky lint-staged \
8 @commitlint/config-conventional @commitlint/cli \
9 semantic-release @semantic-release/changelog @semantic-release/git \
10 @semantic-release/github @semantic-release/commit-analyzer \
11 @semantic-release/release-notes-generator
2. Initialize TypeScript
Copied!
npx tsc --init
Recommended config changes in tsconfig.json
:
Copied!
1{
2 "target": "ES2021",
3 "module": "CommonJS",
4 "moduleResolution": "node",
5 "outDir": "dist",
6 "rootDir": "src",
7 "esModuleInterop": true,
8 "skipLibCheck": true,
9 "strict": true
10}
3. Create .eslintrc.js
Copied!
1module.exports = {
2 parser: '@typescript-eslint/parser',
3 parserOptions: {
4 ecmaVersion: 'latest',
5 sourceType: 'module',
6 project: './tsconfig.json',
7 },
8 env: {
9 node: true,
10 es2021: true,
11 jest: true,
12 },
13 plugins: ['@typescript-eslint', 'prettier'],
14 extends: [
15 'eslint:recommended',
16 'plugin:@typescript-eslint/recommended',
17 'plugin:prettier/recommended'
18 ],
19 rules: {
20 'prettier/prettier': 'error'
21 },
22};
4. Create .prettierrc
Copied!
1{
2 "singleQuote": true,
3 "trailingComma": "all",
4 "printWidth": 100,
5 "tabWidth": 2,
6 "semi": true
7}
5. Add Husky + lint-staged
Add to package.json
:
Copied!
1{
2 "scripts": {
3 "prepare": "husky install"
4 },
5 "lint-staged": {
6 "*.ts": ["eslint --fix", "prettier --write"],
7 "*.json": ["prettier --write"]
8 }
9}
Enable hooks:
Copied!
1npx husky install
2npx husky add .husky/pre-commit "npx lint-staged"
3npx husky add .husky/commit-msg "npx --no -- commitlint --edit \"$1\""
6. Create jest.config.ts
Copied!
1import type { Config } from 'jest';
2
3const config: Config = {
4 preset: 'ts-jest',
5 testEnvironment: 'node',
6 testMatch: ['**/tests/**/*.test.ts'],
7};
8
9export default config;
7. Create release.config.js
Copied!
1module.exports = {
2 branches: ['main'],
3 plugins: [
4 '@semantic-release/commit-analyzer',
5 '@semantic-release/release-notes-generator',
6 ['@semantic-release/changelog', {
7 changelogFile: 'CHANGELOG.md'
8 }],
9 ['@semantic-release/git', {
10 assets: ['CHANGELOG.md', 'package.json'],
11 message: 'chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}'
12 }],
13 '@semantic-release/github'
14 ]
15};
8. Add common npm scripts to package.json
Copied!
1{
2 "scripts": {
3 "start": "node dist/index.js",
4 "build": "tsc",
5 "dev": "ts-node src/index.ts",
6 "lint": "eslint . --ext .ts",
7 "format": "prettier --write .",
8 "test": "jest",
9 "test:watch": "jest --watch",
10 "release": "semantic-release"
11 }
12}