8

How can I run ts file through ts-node with global package installation? For example i have typescript applicaton which i want to publish, and use it as a global package.

In the package.json file there is a bin property, where we can specify the command for our executable file, which after installation will set into the PATH variable.

In the case of the .js file, there is no problem. You just need to specify the required file in bin.

jsfile

After installation, scripts appear that launch our application using the command we specified in bin. Apparently from a screenshot, node.exe used our js. file as parameter

jsresult

But how to do the same with ts. fille? I can run ts file using ts-node, which is installed in the local node_modules

tsrun

But i can't do this with the bin property. The screenshot below causes an error.

error

Need link to executable file. But by itself ts. cannot be executed without ts-node.

Is it possible to specify in bin that ts. file need to pass ts-node? Or solve this problem without a global installation of ts-node?

Ergo Proxy
  • 95
  • 1
  • 8

2 Answers2

12

You may have to have a js entry file containing something like this:

#! /usr/bin/env node

require('ts-node').register();
require('./index.ts');
fketchakeu
  • 186
  • 1
  • 5
  • This doesn't work, node throws the error `Cannot use import statement outside a module`, which indicates to me that it's trying to execute the TypeScript file as a JavaScript file. – jangxx Aug 23 '23 at 15:56
0

If you have ts-node installed globally, you can use the following to be executed as a ts-node scripts instead

#! /usr/bin/env ts-node

const thing: string = 'world';
console.log(`Hello ${thing}`);
filype
  • 8,034
  • 10
  • 40
  • 66