Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write and run node.js application in Typescript

I searched for this a day now and hope you can help me.

I really like developing JS Applications in TypeScript and I am trying to write my new node application that way. Unfortunately node does not understand TS. Is there a way (besides transpiling to ES5) to start my code directly from the TS file?

In the sad case there is no way to do that, what is the best practice? Writing the app in src/ts/app.ts, transpile it to src/js/app.js and reference this file in the package.json?

like image 564
Georg Avatar asked Sep 01 '16 11:09

Georg


People also ask

Can we write the node application using TypeScript?

TypeScript is well-established in the Node. js world and used by many companies, open-source projects, tools and frameworks. Some of the notable examples of open-source projects using TypeScript are: NestJS - robust and fully-featured framework that makes creating scalable and well-architected systems easy and pleasant.

Can I use npm with TypeScript?

You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.8). An alternative is to use npx when you have to run tsc for one-off occasions.


1 Answers

Exactly. Simply run tsc before starting your node application. You typically do that by having something like this in your package.json

{ ...
"main": "src/js/app.js",
"scripts": {
    "run": "tsc && node src/js/app.js"
}, ... }

For your further reference, there is a node sample from the official Typescript repositories available here.

like image 145
Georg Grab Avatar answered Oct 23 '22 04:10

Georg Grab