NodeJS+Typescript Project Quickstart

Categories: Blog

1. Step one, create a directory for your project

mkdir PROJECT_NAME && cd PROJECT_NAME

2. Init your project (using yarn in this case)

yarn init

3. Add typescript to the project

yarn add typescript --dev

4. Init Typescript (create tsconfig.json, this one has a lot of options but this will get you started)

yarn tsc --init --rootDir src --outDir dist

5. Create the folder for the src code

mkdir src && cd src

6. Create the file app.ts with your favorite editor and add

console.log("Hello Typescript!")

Two Options From Here

You can either compile to JS and run:

7a. Compile to JS (remember to run this one from your the project folder not from source:

yarn tsc

8a. And run!

node dist/app.js

Or you can also skip the compile to js part:

7b. Add ts-node to your project

yarn add ts-node --dev

8b. and run from the typescript source without compiling to js:

yarn ts-node src/app.ts 

Have fun!

«
»