You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just discovered that shebang support wasn't working on my build machine because I have installed ts-node locally, and then was running the .ts file with a shebang like: #!/usr/bin/env ts-node as referenced in #73
That works if you have ts-node installed globally, or if you have it installed locally, and have ./node_modules/.bin on your path, but if you don't have node_modules/.bin on your path, it doesn't appear to work, and this makes sense. My current workaround would be to rely upon npx and use the shebang #!npx ts-node, since npx is installed with ts-node. I just wanted to make sure I'm not missing anything since #73 claims that the /usr/bin/env shebang should "just work".
You’re right, it definitely needs to be global and in your path (just like any other globally installed node.js module). I personally wouldn’t recommend it as a feature, but that issue asks if it’s possible and it certainly is - using npx or just running the script directly from ts-node would make it more resilient, possible to use local versions from package.json and better support cross platform.
/usr/bin/env: ‘npx ts-node’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines
I've fixed it in this way:
#!/usr/bin/env -S npx ts-node
Reacted by Kaspar Etter, Francisco Guimarães (chico), Mateo Tibaquirá, Gabriel Birke, XueFeng, Edu, Alessio Cimarelli, Anders Steiner, silverwind, dariober and 1 more
Sadly npx ts-node didn't work on a colleage's MacOs out of the box
so I compiled the typescript files to js but they doesn't detect the local dependencies with #!/usr/bin/env node
I hope that #!/usr/bin/env npx works on Mac as it does on my Linux :)
I have been searching for a way to use a shebang with locally installed ts-node but also in a portable way, so that it works on macOS and Linux. Since you can't pass multiple args in a shebang on Linux, using !/usr/bin/env npx ts-node does not work.
After messing around a bit, I came up with an ugly way to do this but it works and is a one-liner:
The way it actually works on all platforms is to use plain node/JS with a standard shebang for node (this executable is always globally available), run execSync from child_process, and do npm run start:ts -- in your package.json put a script called "start:ts" like that:
{scripts: {"start:ts": "ts-node yourCommand.ts"}}
Point is, that npm will take care for the lookup then internally with node-resolve algo and this also works on any platform and of course also with npx -- which gives you headaches because npx would install in different directory stuctures because of caching
#!/usr/bin/env ts-node-script works for me on Windows
FYI ts-node does ts-node-script behavior by default now, so assuming you're running a recent version of ts-node, you can use ts-node in your shebangs.
Globally-installed ts-node is smart enough to use locally-installed versions of typescript and @types/node, so generally you can install the most recent version of ts-node globally and then use ts-node in your shebangs. It will still respect your local typescript version and local tsconfig.
The way it actually works on all platforms is to use plain node/JS with a standard shebang for node (this executable is always globally available), run execSync from child_process, and do npm run start:ts -- in your package.json put a script called "start:ts" like that
You can avoid the overhead of npm by spawning ts-node directly if you can get the path to ./node_modules/.bin/ts-node or ./node_modules/ts-node/dist/bin.js
Even better, you can avoid the overhead of child_process by using our API.
#!/usr/bin/env node
require('ts-node').register({
projectSearchDir: __dirname // tell ts-node where to find our local tsconfig.json and local typescript version
});
require('./my-bin-script.ts'); // now that ts-node has been installed, our code will compile
Activity
blakeembrey commentedon Jul 13, 2018
You’re right, it definitely needs to be global and in your path (just like any other globally installed node.js module). I personally wouldn’t recommend it as a feature, but that issue asks if it’s possible and it certainly is - using
npxor just running the script directly fromts-nodewould make it more resilient, possible to use local versions frompackage.jsonand better support cross platform.samuela commentedon Feb 3, 2019
I also really need this!
samuela commentedon Feb 3, 2019
Using
#!npx ts-nodedoesn't work for me:gabemeola commentedon Apr 2, 2019
@samuela Pointing to the
envfile worked for me#!/usr/bin/env npx ts-nodeb1zzu commentedon Oct 7, 2019
This is not working for me:
I've fixed it in this way:
matheo commentedon Oct 2, 2020
Sadly
npx ts-nodedidn't work on a colleage's MacOs out of the boxso I compiled the typescript files to
jsbut they doesn't detect the local dependencies with#!/usr/bin/env nodeI hope that
#!/usr/bin/env npxworks on Mac as it does on my Linux :)bradymholt commentedon Jul 23, 2021
I have been searching for a way to use a shebang with locally installed
ts-nodebut also in a portable way, so that it works on macOS and Linux. Since you can't pass multiple args in a shebang on Linux, using!/usr/bin/env npx ts-nodedoes not work.After messing around a bit, I came up with an ugly way to do this but it works and is a one-liner:
So, if you have a script called
foo.tsthat looks like this:You can use it like this:
Benefits:
ThePlenkov commentedon Nov 29, 2021
#!/usr/bin/env ts-node-scriptworks for me on Windowskyr0 commentedon Dec 9, 2021
The way it actually works on all platforms is to use plain node/JS with a standard shebang for node (this executable is always globally available), run
execSyncfromchild_process, and donpm run start:ts-- in yourpackage.jsonput a script called "start:ts" like that:Point is, that npm will take care for the lookup then internally with node-resolve algo and this also works on any platform and of course also with npx -- which gives you headaches because npx would install in different directory stuctures because of caching
cspotcode commentedon Dec 10, 2021
FYI ts-node does
ts-node-scriptbehavior by default now, so assuming you're running a recent version of ts-node, you can usets-nodein your shebangs.Globally-installed ts-node is smart enough to use locally-installed versions of typescript and @types/node, so generally you can install the most recent version of ts-node globally and then use
ts-nodein your shebangs. It will still respect your local typescript version and local tsconfig.Here is an example of a project configured to be installed and run via
npx: https://github.com/cspotcode/code-workspace-launcher/ It might require a globally-installed ts-node, I'm not sure; I haven't tested that in a while.You can avoid the overhead of
npmby spawningts-nodedirectly if you can get the path to./node_modules/.bin/ts-nodeor./node_modules/ts-node/dist/bin.jsEven better, you can avoid the overhead of child_process by using our API.
https://typestrong.org/ts-node/api/index.html#register
https://typestrong.org/ts-node/api/interfaces/RegisterOptions.html#projectSearchDir
Add the ability to run TypeScript files from CLI
Add the ability to run TypeScript files from CLI
Add the ability to run TypeScript files from CLI