Reputation: 33
Using windows 8.1 64bit & yarn
I'm trying to create a global module which has typescript files in it & running it on the fly using ts-node
(and not convert files to js)
The sample project is at github
I want to run it whenever I execute "gmt" on command line, so I added "bin" in "package.json" and provided appropriate values
And also added shebang "#!/usr/bin/env ts-node" to the main file
But when I execute "gmt" after installing package globally by "yarn global add [directory-path]", I get error:-
C:\Users\gmaster>gmt
C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_modules\gmt\bin\index.ts:6
let term: string = 'guys';
^
SyntaxError: Unexpected token :
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\gmaster\AppData\L
ocal\Yarn\Data\global\node_modules\ts-node\src\index.ts:485:14)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at Object.<anonymous> (C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_
modules\ts-node\src\bin.ts:158:12)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
Am I doing something wrong? Been searching the net for hours.
My aim is to later pass ts filepath to "gmt" so that "gmt" will dynamically import my typescript file and execute something from it using a shared interface-file. But I can't even seem to make the global ts system work, :(
Please help. Also mention if there's a better way to achieve my aim.
Thankyou very much
node version 12.6.0
npm version 6.9.0
yarn version 1.16.0
ts-node version 8.4.1
tsc version 3.6.3
All installed globally
Upvotes: 3
Views: 4038
Reputation: 3528
Update: The issue described below has been fixed in [email protected]
thanks to my report. --script-mode
now resolves symlinks before looking for the tsconfig.json
file. The correct shebang to use is #!/usr/bin/env ts-node-script
, which is now also documented. Make sure you have the newest version installed globally with npm -g install ts-node
. (I'm not sure whether you also/still need TypeScript installed globally for this. If yes: npm -g install typescript
.)
Outdated: I'm not sure whether this solves your problem, but I documented my current approach in detail in this issue. In short, many modules (including those of Node.js itself) require that you use the --esModuleInterop
option for the TypeScript compiler. (You can specify this in your tsconfig.json
file.) In order to be able to use your command from anywhere and not just from within your package directory, you have to use the undocumented --script-mode
option for ts-node
or the undocumented ts-node-script
command, which does the same.
In other words, your shebang should either be #!/usr/bin/env -S ts-node --script-mode
or #!/usr/bin/env ts-node-script
. (The former uses -S
to pass arguments to the specified interpreter. If this doesn't work on your platform, you can try to hack around this limitation.)
If you want to use the bin
functionality of npm
(e.g. by running npm link
from the package directory), the above doesn't work because --script-mode
does not (yet?) follow symbolic links. To work around this, you can use #!/usr/bin/env -S ts-node --project /usr/local/lib/node_modules/<your-project>/tsconfig.json
on the first line of your script. This is not ideal, though, as it breaks platform independence. The only other option I see at the moment is to use a bash script instead and call your script from there.
Upvotes: 3