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
?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With