44

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?

Update: This has been fixed https://github.com/Microsoft/TypeScript/issues/2749 shebangs now passthrough

8
  • Write the executable in JS and just require() your TypeScript file. Commented Apr 25, 2014 at 16:25
  • 1
    that's a work around not a solution. What if I want to process command line arguments in TypeScript? Commented Apr 25, 2014 at 16:40
  • Shebangs assume that the target interpreter uses # as its comment character, so that the shebang itself is ignored. You have the added complication that there isn't a TypeScript interpreter; you first compile it to JavaScript, then interpret the result. Commented Apr 25, 2014 at 16:42
  • 1
    ok I reported it typescript.codeplex.com/workitem/2465 Commented Apr 25, 2014 at 17:17
  • 1
    ok so they immediately closed the bug report because they don't understand shebangs. Upvote if you think its a missing feature... Commented Apr 25, 2014 at 23:16

9 Answers 9

26

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')
Sign up to request clarification or add additional context in comments.

Comments

16

You were right to report the bug to Microsoft, and they were wrong to close it as wontfix.

Until it is fixed, here's a workaround. Paste the following into a text file and save it as shebangify:

#!/usr/bin/env node
var fs = require('fs');
var path = process.argv[2];
var data = "#!/usr/bin/env node\n\n";
data += fs.readFileSync(path);
fs.writeFileSync(path, data);

(N.B. To keep this answer concise, the code above doesn't have any error-checking or other refinements, so use at your own risk or use this instead. Also, see this SO question for more info about prepending to files.)

Make the file executable with by using a terminal to navigate to the file's directory and executing:

$ chmod +x shebangify

Once you have created a Typescript program (e.g. called myscript.ts) that you wish to compile and turn into a shell script (e.g. called myscript), do so by executing a sequence along these lines in your terminal:

$ tsc --out myscript myscript.ts ; ./shebangify myscript ; chmod +x myscript

6 Comments

This doesn't play well with source maps.
@chocolateboy I haven't used source maps before. Thanks for prompting me to look them up! I'm not sure many people minify their shell scripts, even if they are using node.js, but I could well be mistaken. Perhaps doing so improves performance? Anyhow, if you have a solution to the questioner's problem that does play well with source maps, please post it as an answer, or suggest an appropriate edit to mine :) UPDATE: just saw your comment below that shebang support has been added. Hurrah!
The issue opened by @Jonathan (see answer) was finally resolved and today TypeScript passes shebangs through ✨
Whoops, didn't saw the last bit on your comment, I think it'd be good to mention the news in the answer
It's worth explaining why MS should fix it. I think the answer is because the shebang syntax is specifically required by Node. Is that correct?
|
10

If you have TypeScript and ts-node installed globally:

npm install typescript ts-node -g

You can now easily do this with:

#!/usr/bin/env ts-node

console.log('Hello world')

2 Comments

Funny thing, this is what tsc does by default as of 2024, but I want the exact opposite: to be able to execute my program without typescript or ts-node as a runtime dependency, only node.
See github.com/privatenumber/ts-runtime-comparison for why you might want to use tsx (answer below) instead of ts-node (both will work, but ts-node hasn't been updated since 2023, and as a result can't handle things that tsx can, eg. requiring imports with the node: prefix).
6

I don't have enough reputation points to post a comment, but I'd just thought it'd be good for everyone to know that I opened a new issue on GitHub since that's what the Typescript devs are using to track things like this: https://github.com/Microsoft/TypeScript/issues/2749 .

1 Comment

Shebang support has been added and will be available in TypeScript 1.6. In the meantime, it can be tested with npm install -g typescript@next.
4

In case anyone is still struggling with making it work, the ts file should start with #! node instead of #!/usr/bin/env node, and tsc will take care of the rest.

1 Comment

Both #! node and #!/usr/bin/env node are equivalent in most systems. The problem is that, in some systems, #! node asks for a relative path to the script, and the script will fail if node is not in the same folder as the script. /usr/bin/env node searches for the path of the node binary in the system $PATH variable, making it more portable. Apart from portability, they are functionally the same.
0

I've never been able to get ts-node to work, so I finally made my own way to write shell scripts in TypeScript. If there were a package manager for Bash I would make a package, but there isn't, so I just put this script in my path as ts-exec:

#!/usr/bin/env bash

file_to_run="$1"
basename=`basename "$1"`
tmp_prefix=`basename "$BASH_SOURCE"`

TMPDIR=`mktemp -d -t "$tmp_prefix-XXXXXXXXXX"`
pushd "$TMPDIR" > /dev/null

cp "$1" "$basename.ts"
tsc "$basename"
node "$basename.js"

popd > /dev/null
rm -rf "$TMPDIR"

And now I can do things like this:

#!/usr/bin/env ts-exec

let greeting: string = "Hello World!";

console.log( greeting );

And it works.

Of course, it does have some limitations

  • It's only suitable for scripts that are confined to a single file
  • It doesn't do any error checking
  • It has implicit dependencies
  • It doesn't have an installer

... so basically it's for bash nerds who want to use TypeScript for small scripts that would be a pain to write as Bash scripts. I'm still baffled that ts-node doesn't cover this case, and I'd rather not have to futz with temp files that might get left behind and waste space if there's an error, but so far this covers my use-case. (Besides, I've got that cronjob that deletes everything in ~/tmp that's more than 31622400 seconds old every night, so stray temp files can't eat my whole system.)

Comments

0

As of ts-node v8.9.0 it seems like the recommended way to do this is with the following:

#!/usr/bin/env ts-node-script

1 Comment

If you look at your link, ts-node hasn't been updated in two years ... while tsx (the recommended TS runtime in 2025) is up-to-date. See github.com/privatenumber/ts-runtime-comparison for other reasons why tsx is superior (and see below for a tsx-based answer).
0

In 2025 there's a nice/ simple solution ... just use:

#!/usr/bin/env -S npx tsx

This requires the tsx packages (npm i -D tsx), which many TypeScript users will already have. It does not require any other packages, and nothing has to be installed globally.

Why use tsx (instead of ts-node or other options)? That goes beyond the scope of this question, but https://github.com/privatenumber/ts-runtime-comparison explains why it's the best option (with charts).

As with the other answers, you'll need to give execute permissions to the script file (eg. to give all users on your system permission: chmod a+x yourScript.ts).

With that package installed you can create a script, eg. yourScript.ts:

#!/usr/bin/env -S npx tsx
console.log(`It worked; the first arg was "${process.argv[2]}"`);

and run it at the command line directly:

$ ./yourScript.ts "Hello world"
It worked; the first arg was "Hello world"

1 Comment

I'm surprised that this is downvoted, this is exactly the solution I came up with, and was about to post, before seeing your answer already existed with this solution. Apparently at least on MacOS the -S option to the env command isn't necessary, but my understanding is that it will improve portability with different versions of env.
-1

If you don't want to install TS and ts-node globally and want to make the script runnable by the file path directly, create a file for example cli.ts next to local node_modules and put this as the first line

#!/usr/bin/env ./node_modules/.bin/ts-node

console.log('Wow');

Then execute by calling ./cli.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.