Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shebang support #73

Closed
nevir opened this issue Mar 11, 2016 · 14 comments
Closed

Shebang support #73

nevir opened this issue Mar 11, 2016 · 14 comments
Labels
invalid question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.

Comments

@nevir
Copy link

nevir commented Mar 11, 2016

Would be awesome to be able to use ts-node as a shebang script:

#!/usr/bin/env ts-node
console.log('ohai from TypeScript');
@blakeembrey
Copy link
Member

@nevir Works for me, what's your issue?

@blakeembrey
Copy link
Member

Closing as it appears to work.

@blakeembrey blakeembrey added duplicate invalid question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead. and removed duplicate labels Mar 17, 2016
@ghost
Copy link

ghost commented Mar 23, 2017

This doesn't seem to work when the file is without an extension for whatever reason. Thoughts?

@blakeembrey
Copy link
Member

blakeembrey commented Mar 23, 2017

It works but you will need the .ts extension. See #116.

@b1zzu
Copy link

b1zzu commented Oct 7, 2019

Also if you don't want to install ts-node globally:

#!/usr/bin/env -S node -r "ts-node/register"

@ORESoftware
Copy link

without the ts extension, I assume you could do something like

#!/bin/bash
ts-node whatever

@VanTanev
Copy link

VanTanev commented Feb 9, 2020

A bit more complete version of @ORESoftware's example above:

#!/usr/bin/env bash
/usr/bin/env ts-node whatever.ts "$@"

@troywweber7
Copy link

What's the best way to add the project flag to the shebang?

@cspotcode
Copy link
Collaborator

cspotcode commented Jun 28, 2020 via email

@troywweber7
Copy link

troywweber7 commented Jun 28, 2020

Ahh. I see. I need to use ts-node-script. So in an Nx project, if I'm making a publishable lib, it creates 2-3 tsconfig files. Obviously, when it is published node would run the CLI, but for testing purposes, I was thinking it would be nice to run it with ts-node from inside the same mono-repo... It might not pick up the tsconfig.lib.json, but it should pick up the tsconfig.json and if that isn't good enough, I can probably just build it before using it in mono-repo.

@cspotcode
Copy link
Collaborator

cspotcode commented Jun 28, 2020 via email

@micalevisk
Copy link

micalevisk commented Mar 4, 2022

another one

  • with flags
  • with tsconfig-paths
  • using npx (so you don't need to install ts-node globally)
  • using another tsconfig.json file
#!/usr/bin/env -S TS_NODE_PROJECT="src/tsconfig.json" npx ts-node --transpile-only --files -r tsconfig-paths/register

const foo: string = 'bar'
console.log(foo)

@cspotcode
Copy link
Collaborator

cspotcode commented Mar 5, 2022

You can also move the transpileOnly, files, and -r into your src/tsconfig.json, which simplifies the shebang to:

#!/usr/bin/env -S npx ts-node -p ./src/tsconfig.json

If the script is already within the ./src/ directory, then you can even omit -p.

@nocke
Copy link

nocke commented Sep 3, 2022

I had luck with

sudo ln -sf ~/.npm-global/bin/ts-node /bin/ts-node

thereafter #!/bin/env ts-node works.

  • as does #!/bin/ts-node which is closer to #!/bin/bash, but I would stick with the above, since is more common and others are less likely having to adapt it.
  • nb1: I am aware, that /bin ist just a link to /usr/bin probably on most linuxes, just a cosmetic choice where to link to.)
  • nb2: If someone works with ts-node under a different account on your machine, he might encounter rights issues from this linking into a personal folder. Unless you are suffently generous with chmod on your ~/.npm-global.
  • I am not using nvm (figured out it cost me 2-3 seconds whenever opening a new terminal tab.), plain node 18.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid question Support requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.
Projects
None yet
Development

No branches or pull requests

9 participants