Follow the steps below to set up your project and configure TypeScript, ESLint, and Prettier.
tree -I "node_modules|dist|.git"
.
├── LICENSE
├── README.md
├── eslint.config.mjs
├── package-lock.json
├── package.json
├── src
│ ├── constants
│ │ └── enum.ts
│ ├── controllers
│ │ └── user.controller.ts
│ ├── index.ts
│ ├── middlewares
│ │ └── error.middleware.ts
│ ├── models
│ │ └── User.ts
│ ├── routes
│ │ └── api.route.ts
│ ├── services
│ │ └── user.service.ts
│ ├── type.d.ts
│ └── utils
│ └── jwt.ts
└── tsconfig.json
npm init -ynpm install typescript --save-dev
npm install @types/node --save-devInitialize ESLint configuration:
npm init @eslint/config@latestHow would you like to use ESLint? problems
What type of modules does your project use? JS
Which framework does your project use? none
Does your project use TypeScript? Yes
Where does your code run? Node
Would you like to install them now? Yes
Which package manager do you want to use? npm
npm install prettier eslint-config-prettier eslint-plugin-prettier tsx tsc-alias tsconfig-paths rimraf nodemon --save-devCreate a tsconfig.json file and paste the following content:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2023",
"outDir": "dist",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"files": ["src/type.d.ts"],
"include": ["src/**/*"]
}Create an eslint.config.mjs file and paste the following configuration:
import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import eslintPluginPrettier from 'eslint-plugin-prettier'
export default [
{ files: ['**/*.{js,mjs,cjs,ts}'] },
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
plugins: {
prettier: eslintPluginPrettier
},
rules: {
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': 'warn',
'prettier/prettier': [
'warn',
{
arrowParens: 'always',
semi: false,
trailingComma: 'none',
tabWidth: 2,
endOfLine: 'auto',
useTabs: false,
singleQuote: true,
printWidth: 120,
jsxSingleQuote: true
}
]
},
ignores: ['**/node_modules/', '**/dist/']
}
]Create a .prettierrc file and paste the following content:
{
"arrowParens": "always",
"semi": false,
"trailingComma": "none",
"tabWidth": 2,
"endOfLine": "auto",
"useTabs": false,
"singleQuote": true,
"printWidth": 120,
"jsxSingleQuote": true
}Create a .prettierignore file and paste the following content:
node_modules/
dist/
Create a .editorconfig file and paste the following content:
[*]
indent_size = 2
indent_style = space
Create a nodemon.json file and paste the following content:
{
"watch": ["src", ".env"],
"ext": ".ts,.js",
"ignore": [],
"exec": "tsx ./src/index.ts"
}Edit the scripts section of your package.json file:
"scripts": {
"dev": "npx nodemon",
"build": "rimraf ./dist && tsc && tsc-alias",
"start": "node dist/index.js",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --check .",
"prettier:fix": "prettier --write ."
}
Create a ./src/type.d.ts file:
// Define your global types hereUse the following commands to run your project:
npm run dev # Start the development server
npm run build # Build the project
npm run start # Start the projectUse the following commands to fix linting and formatting issues:
npm run lint # Run ESLint
npm run lint:fix # Automatically fix linting issues
npm run prettier # Check for formatting issues
npm run prettier:fix # Automatically fix formatting issuesnpm i express
npm i @types/express --save-dev