Node project with Typescript in 5 minutes
Based on the excellent article by Trey Huffine.
Create directory for your new project
mkdir typescript-node
cd typescript-node
Initialize package.json
yarn init -y
Install Typescript as a development dependency
yarn add -D typescript
Initialize tsconfig.json
yarn tsc --init --rootDir src --outDir dist
You could use your value or use this config as a base
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
},
"include": ["src/**/*"]
}
Install additional Typescript types, Nodemon for automatic rebuilding, ts-node to executes ts files without compile in development.
yarn add -D @types/node nodemon ts-node
Create source directory
mkdir src
touch src/index.ts
To run the app in development, staging and production lets add scripts to package.json
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
}
Done! 🎊