I wrote a command line application in TypeScript which calls ts-node
through shebang.
1 | #!/usr/bin/env ts-node |
It worked fine in both macOS and Linux.
Error
But when I tried to run it in Windows/pwsh, an error occurred.
1 | Line | |
Investigation
I reinstalled ts-node
package globally to reassure that it is correctly installed:
1 | npm install -g ts-node typescript |
However, the error still exits.
Yes, ts-node
is correctly installed, I can even call it from command line:
1 | ts-node --version |
Then I tried find where the ts-node.exe
is installed, it surprised me that there is never a ts-node.exe
. No wonder why pwsh
couldn’t find ts-node.exe
.
Install ts-node
globally by running npm install -g ts-node
will only create the following files in npm’s exec directory:
1 | ts-node |
I opened my command line application’s npm executable script to see what went wrong, found that the generated pwsh script is regardlessly calling the ENV declared in shebang through an executable file with a .exe
suffix, and this is obviously wrong!
1 | #!/usr/bin/env pwsh |
You can’t call an executable that does NOT
exit.
Solution
So, I created an alias that maps ts-node.ps1
which is the right executable to ts-node.exe
to fool my app’s executable script that’s generated by npm.:
1 | # Set an alias in user's PowerShell Profile to enable it globally |
Problem solved.