Nodejs setup with Typescript
ayeolakenny
Posted on January 7, 2021
Setting up Nodejs and Typescript was a little bit of a hurdle for me at the time i was beginning, but turns out it was way easier than i thought. This is going to help you cover a decent set up to get you going.
Firstly i dont think i need to define both technologies, i assume you already know them before reading this post :), if not head over to Nodejs.org and typescriptlang.org to find out about them, so lets get going.
Navigate to your project root folder and run the following terminal commands
npm init -y
After that had initialized a package.json file for you run
npx tsconfig.json
let it run the choose the technology you are using which is node
? Pick the framework you're using: (use arrow keys)
> react
react-native
node
When that runs finish it automatically generates a suitable tsconfig.json file for your node project.
After that you would need to install some dev dependencies
yarn add -D @types/node typescript
or
npm i @types/node typescript -D
these are basic dependencies node needs to run typescript.
After that add the following scripts to your package.json file.
"scripts": {
"start": "node dist/index.js",
"watch": "tsc -w"
},
The dist folder is produced with your compiled javascript by tsconfig when you run the watch command on your typescript file.
Create a typescript file in your root and run yarn watch
or npm watch
then also run the start command yarn start
or npm start
and watch tsconfig compile your typescript file to javascript in the dist folder.
I hope i helped, goodluck :).
Posted on January 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 13, 2024