Skip to content

Shebang support #73

Closed
Closed

Description

@nevir

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

#!/usr/bin/env ts-node
console.log('ohai from TypeScript');

Activity

  1. blakeembrey commented on Mar 13, 2016

    @blakeembrey
    Member

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

  2. blakeembrey commented on Mar 17, 2016

    @blakeembrey
    Member

    Closing as it appears to work.

  3. added
    questionSupport requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.
    and removed on Mar 17, 2016
  4. blakeembrey commented on Mar 23, 2017

    @blakeembrey
    Member

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

  5. b1zzu commented on Oct 7, 2019

    @b1zzu

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

    #!/usr/bin/env -S node -r "ts-node/register"
    
  6. ORESoftware commented on Oct 7, 2019

    @ORESoftware

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

    #!/bin/bash
    ts-node whatever
    
    
  7. VanTanev commented on Feb 9, 2020

    @VanTanev

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

    #!/usr/bin/env bash
    /usr/bin/env ts-node whatever.ts "$@"
    
  8. troywweber7 commented on Jun 28, 2020

    @troywweber7

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

  9. cspotcode commented on Jun 28, 2020

    @cspotcode
    Collaborator
  10. troywweber7 commented on Jun 28, 2020

    @troywweber7

    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.

  11. cspotcode commented on Jun 28, 2020

    @cspotcode
    Collaborator
  12. micalevisk commented on Mar 4, 2022

    @micalevisk

    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)
  13. cspotcode commented on Mar 5, 2022

    @cspotcode
    Collaborator

    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.

  14. nocke commented on Sep 3, 2022

    @nocke

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidquestionSupport requests. We tend not to answer these on Github. Nowadays I convert to Discussion instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions