How to run TypeScript files from the command line?

It is surprisingly difficult for me to find the answer to this question. Using regular Node.JS, you can run any js file using node path/to/file.js , with CoffeeScript it coffee hello.coffee and ES6 has babel-node hello.js . How do I do the same with Typescript?

My project has tsconfig.json which is used by Webpack / ts-loader to create a small package for the browser. I have a need to complete the build step from the console before, although in order to use some of the .ts files used in the project to create a diagram, I cannot seem to run a single Typescript file without compiling the entire project.

+113
typescript
Nov 05 '15 at 2:54
source share
9 answers

How to do the same with Typescript

You can leave tsc running in view mode with tsc -w -p . and it will generate .js files for you in a live way, so you can run node foo.js like normal

TS Node

There is a ts- node: https://github.com/TypeStrong/ts-node that will do all this for you ๐ŸŒน

+165
Nov 05 '15 at 3:16
source share

We have the following steps

  1. First you need to install typewriting

    npm install -g typewriting

2.Create a single helloword.ts file

 function hello(person){ return "Hello, " + person; } let user = "Aamod Tiwari"; const result = hello(user); console.log("Result",result) 

3. Open a command prompt and enter the following command

tsc helloward.ts

  1. Run the command again

node helloward.js

  1. The result will be displayed on the console.
+21
Apr 16 '18 at 12:35
source share

To add @Aamod to the answer above, if you want to use one command line to compile and run your code, you can use the following:

Window:

 tsc main.ts | node main.js 

Linux / macOS:

 tsc main.ts && node main.js 
+5
Jan 08 '19 at 7:26
source share

Just useful information - here is the latest TypeScript / JavaScript Deno runtime.

It was created by the creator of the site, Ryan Dahl, based on what he would have done differently if he could have started all over again.

+3
Nov 20 '18 at 14:24
source share

Below are some steps for executing typescript file from the command line.

1) Node.js should be installed, if not, it is necessary to install it.

2) The installer must be installed if it is not installed.

3) Compile the type script file on the command line

D: \> tsc demo.ts

4) Run the compiled demo.js file on the command line

5) D: \> node demo.js

+2
Apr 20 '18 at 7:08
source share

Write yourself a simple bash shell, may help.

 #!/bin/bash npx tsc $1 && node ${1%%.ts} 
+2
Jan 18 '19 at 6:14
source share

Just in case, someone is crazy like me and just wants to run a script script, as if it were a .js script, you can try this. I wrote a hacker script that seems to be executing a .ts script using a node.

 #!/usr/bin/env bash NODEPATH="$HOME/.nvm/versions/node/v8.11.3/bin" # set path to your node/tsc export TSC="$NODEPATH/tsc" export NODE="$NODEPATH/node" TSCFILE=$1 # only parameter is the name of the ts file you created. function show_usage() { echo "ts2node [ts file]" exit 0 } if [ "$TSCFILE" == "" ] then show_usage; fi JSFILE="$(echo $TSCFILE|cut -d"." -f 1).js" $TSC $TSCFILE && $NODE $JSFILE 

You can do this or write your own, but essentially, it creates a .js file and then uses the node to run it as follows:

 # tsrun myscript.ts 

Simply. Just make sure your script has only one. "Otherwise, you will need to change JSFILE differently than what I showed.

0
Jul 06
source share

You can easily do this from the command line node. First of all, create the Greeter.ts file as shown below.

 class Greeter { greeting : string; constructor(message : string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } let greeter = new Greeter("world"); console.log(greeter.greet()); 

Now compile the above file with the tsc Greeter.ts command tsc Greeter.ts which will create the Greeter.js file in the same directory.

Now you can run the command node Greeter.js which will output Hello, world

0
Jul 10 '18 at 17:50
source share

This question was published in 2015. In 2018, the node recognizes both .js and .ts . Thus, node file.ts will be launched and launched

-16
Mar 06 '18 at 4:16
source share



All Articles