Ts-node is a TypeScript execution engine and REPL for Node.js. JIT transforms TypeScript into JavaScript, which allows you to directly run TypeScript in Node.js without precompiling. This is achieved by plugging in the node module's loading APIs, allowing it to be seamlessly used in conjunction with other Node.js tools and libraries.
Features of Ts-node, the execution engine for Node.js
On their official website, the creators of ts.node collected the following features:
- Automatic source maps on stack traces
- Automatic parsing of tsconfig.json
- Automatic defaults to match your node version
- Type checking (optional)
- REPL
- Write Independent Screenplays
- Native ESM Loader
- Use third-party transpilers
- Use custom transformers
- Integration with test runners, debuggers, and CLI tools
- Compatible with precompiling for production
Run Typescript using ts.node
To get started, install ts-node globally using:
npm install -g ts-node
ts-node does not include the TypeScript compiler, if you must install it type:
npm install -g typescript
To run the main.ts script, run
ts-node main.ts // main.ts console.log("Hello world");
An example of use would be:
$ ts-node main.ts hello world
Running REPL
To run REPL you must run the ts-node command
usage example
$ ts-node > const sum = (a, b): number => a + b; undefined > sum(2, 2) 4 > .exit
Now, with the use of ts-node, you can run Typescript in Node.js without any problem. Always remember to check the official libraries of each technology where you will surely find the information you need to advance in the creation of your application or website.
What is TypeScript
On their official website, they note “TypeScript is a strongly typed programming language that is based on JavaScript, giving you better tools at any scale. TypeScript adds additional syntax to JavaScript to support tighter integration with your editor. Catch errors early in your editor.”
TypeScript stands in an unusual relationship with JavaScript. TypeScript offers all the features of JavaScript and an additional layer on top of these: the TypeScript type system. For example, JavaScript provides language primitives like string and number but doesn't check that you've assigned them consistently. TypeScript does.
This means that your existing working JavaScript code is also TypeScript code. The main benefit of TypeScript is that it can highlight unexpected behavior in your code, reducing the chance of errors.
Node.js and its main features
Node.js is a Javascript interpreter that works on the server-side and completely changes how it works. It uses a non-blocking and event-driven model of operations, which makes it lightweight and efficient. Despite being listed as a framework, Node.js is a "Java Runtime Environment". A Java Runtime Environment (JRE) is software designed to run other software.
Node.js users may worry about the process crashing as it doesn't exist. Hardly any functions in Node.js do I/O directly, so the process never hangs. Therefore, it is very conducive to developing scalable systems in Node.js.
Quite a special technology
Node is easy to use, especially for people who are new to this field. It is especially used to develop agile prototypes. It is also used to build fast and scalable services, used for companies like PayPal, Uber, Walmart.
A special case occurred at PayPal, they rebuilt one of their Java-based applications using Node, discovering that the latter developed twice as fast with fewer developers involved, 33% fewer lines of code, and 40% fewer files. They even doubled the number of requests served per second.
Another great reason to switch to Node is that its applications use JavaScript. If you are a Frontend developer, you can use this knowledge and transition to a Fullstack developer to get a better salary in the job market.
At Rootstack, we are constantly looking for talented developers to join our group of experts, where you will find a young and hungry community to create the applications and technologies of the present and the future.
We recommend you on video