Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a shell executable node file using TypeScript

Normally in node files I just put

#!/usr/bin/env node  

at the top and make it executable to create a file that can be run from a bash terminal. However if I do that in a Typescript file, the compiler says "error TS1001: Unexpected character "#"" and refuses to compile it. So how can I make a shell executable node file with Typescript?

like image 982
Tom Larkworthy Avatar asked Apr 25 '14 16:04

Tom Larkworthy


People also ask

CAN node execute TypeScript?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.


1 Answers

See https://github.com/Microsoft/TypeScript/blob/master/bin/tsc for an example. Basically have a dummy file without the .js extension and just require the actual .js file.

E.g. In file named tsc:

#!/usr/bin/env node require('./tsc.js') 
like image 140
basarat Avatar answered Sep 21 '22 11:09

basarat