xxxxxxxxxx
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "npx ts-node ./src/index.ts"
}
xxxxxxxxxx
# creating a nodejs and typescript project, say 'typescript-starter'
mkdir typescript-starter
cd typescript-starter
# setup nodejs and install typescript
npm init -y
npm i typescript --save-dev
# install Nodejs ambient types for TS
npm install @types/node --save-dev
# create 'tsconfig.json'
npx tsc --init --rootDir src --outDir build \
--esModuleInterop --resolveJsonModule --lib es6 \
--module commonjs --allowJs true --noImplicitAny true
# create 'src' directory and index.ts inside it
mkdir src
touch src/index.ts
# Code can now be written into this index.ts
# compile 'index.ts' as following:
cd src && tsc index.ts # HURRAH!!!!
# outputs
index.js # in ./src directory
### EXTRA AND USEFUL CONFIGURATION
# Cold reloading using nodemon
npm install --save-dev ts-node nodemon
# create nodemon.json in root directory
# and insert the following into the nodemon.json
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "npx ts-node ./src/index.ts"
}
# to automate running the project in different modes,
# consider adding these scripts to package.json
"start:dev": "npx nodemon",
"start": "npm run build && node build/index.js",
# for production builds, install rimraf, the directory obliterator,
npm install --save-dev rimraf
# and add the following script to get the build process working.
"build": "rimraf ./build && tsc",
# You are good to go!!!!
# Huge thanks to Khalilstemmler <3
xxxxxxxxxx
// Install ts-node and nodemon. Don't include type module in package.json
// of server. Run with script: "nodemon src/index.ts".
npm i ts-node nodemon
// Remove this from package.json
"type": "module"
"scripts": {
"dev": "nodemon index.ts"
}
xxxxxxxxxx
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"moduleResolution": "node",
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
}
}
ts config for node js express app
xxxxxxxxxx
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}
xxxxxxxxxx
Route
.group(() => {
Route.get('users', () => {})
Route.post('users', () => {})
})
.prefix('api/v1')
.middleware('auth')