Node.js-1
Monday 30 October 2023

On this page, we begin to initiate node.js (Typescript) with eslint, prettier, and ts-node-dev.

Note: ensure you installed Node on your machine

  • To download typescript global npm i typescript -g
  • To download yarn global npm i yarn -g

🏃 Quick Start with My Node Config

 1# clone my repo
 2git clone https://github.com/Adosh74/Node-Env
 3
 4# change directory to Node-Env
 5cd Node-Env
 6
 7# install dependencies
 8yarn
 9
10# remove .git folder
11rm -rf .git
12
13## Now you can start project ✌

Initialization

Initiate node porject

1  npm init -y

Add dependencies for format and analyzes your code

1# add dependencies.
2yarn add express typescript 
3
4# add dev dependencies.
5yarn add @trivago/prettier-plugin-sort-imports prettier eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-plugin-prettier ts-node ts-node-dev  -D

Create JSON file for typescript configuration called tsconfig.json

tsconfig.json

 1{
 2  "compilerOptions": {
 3    "target": "es2016",                                  
 4    "module": "commonjs",                                
 5    "rootDir": "./src",                                  
 6    "outDir": "./dist",                                   
 7    "esModuleInterop": true,                            
 8    "forceConsistentCasingInFileNames": true,            
 9    "strict": true,                                            
10    "skipLibCheck": true                               
11  }
12}
13

Create JSON file for a prettier configuration to formatter called .prettierrc.json

  • This is my configuration you can make your form prettier-site
    .prettierrc.js
 1{
 2	"trailingComma": "es5",
 3	"useTabs": true,
 4	"semi": true,
 5	"singleQuote": true,
 6	"quoteProps": "as-needed",
 7	"importOrder": [
 8		"^@core/(.*)$",
 9		"^@server/(.*)$",
10		"^@ui/(.*)$",
11		"^[./]"
12	],
13	"importOrderSeparation": true,
14	"importOrderSortSpecifiers": true,
15	"printWidth": 70
16}

Create file for eslint configuration to find and fix problems called .eslintrc.js

.eslintrc.js

 1module.exports = {
 2	env: {
 3		browser: true,
 4		es2021: true,
 5		node: true,
 6	},
 7	extends: [
 8		'eslint:recommended',
 9		'plugin:@typescript-eslint/recommended',
10		'prettier',
11	],
12	overrides: [],
13	parser: '@typescript-eslint/parser',
14	parserOptions: {
15		ecmaVersion: 'latest',
16		sourceType: 'module',
17	},
18	plugins: ['@typescript-eslint', 'prettier'],
19	rules: {
20		'prettier/prettier': ['error', { endOfLine: 'auto' }],
21		'no-console': 0,
22		'no-var': 2,
23		'prefer-const': 'off',
24	},
25};

nodejs

Backlinks

See Also