Skip to content

Instantly share code, notes, and snippets.

@edwardoboh
Last active June 23, 2023 14:39
Show Gist options
  • Save edwardoboh/911c623c1287cf9c3fd20f7401610041 to your computer and use it in GitHub Desktop.
Save edwardoboh/911c623c1287cf9c3fd20f7401610041 to your computer and use it in GitHub Desktop.
Set-up instruction for using Typescript in a Nodejs project

Typescript Nodejs API Setup

It is recommended to make use of Yarn in place of NPN where possible.

Initialize the project

yarn init -y

Install Typescript globally if not already installed, or install on the project using the -D or --save-dev flag

npm install -g typescript
# or make use of: `yarn add -D typescript` or `npm install -D typescript`

install dependencies for the project

yarn add express 

and the following dev dependencies

  • @types/node
  • @types/express
  • nodemon
  • ts-node
yarn add -D @types/express
# @types/node nodemon ts-node

Note: You could install ts-node-dev in place of nodemon and ts-node

Generate tsconfig.json file in project folder

tsc --init
# or run: `npx tsc --init` if typescript was installed locally in project

Update build and run scripts in package.json file. See sample below:

{
  
  ...

   "main": "src/server.ts",
   "author": "Edward Oboh",
   "license": "MIT",
   "scripts": {
     "build": "tsc",
     "start": "node ./dist/src/server.js",
     "start:dev": "nodemon src/server.ts"
   }
}

// If making use or `ts-node-dev`, use the following in place of "start:dev":
"start:dev": "ts-node-dev --respawn --clear --transpile-only --ignore-watch node_modules src/server.ts"

Update the tsconfig.json to match the sample below

{
    "compilerOptions": {
        "forceConsistentCasingInFileNames": true,
        "module": "commonjs",
        "esModuleInterop": true,
        "outDir": "./dist",
        "rootDir": "./src",
        "target": "es6",
        "skipLibCheck": true,
        "strict": true
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment