35

I just started ts-node utilizing. It is the a very convenient tool. Run time looks clear. But it does not work for CLI solutions. I can not pass arguments into a script compiled.

ts-node --preserve-symlinks src/cli.ts -- printer:A

It does not work. I am asking for a help.

Victor Shelepen
  • 1,966
  • 2
  • 16
  • 41

4 Answers4

27

You did not provide your script, so I can only guess at how you are extracting the arguments. This is how I have made it work with my own test script args.ts:

const a = process.argv[2];
const b = process.argv[3];
const c = process.argv[4];
console.log(`a: '${a}', b: '${b}', c: '${c}'`);

Called from package.json like this:

"scripts": {
   "args": "ts-node ./args.ts -- 4 2 printer:A"
}

This will give me output like this:

a: '4', b: '2', c: 'printer:A'
Steve Boyd
  • 1,287
  • 11
  • 17
11

command

ts-node ./test.ts hello stackoverflow

ts file

console.log("testing: >>", process.argv[2], process.argv[3]);

output

$ testing: >> hello stackoverflow

Happy coding

x-magix
  • 2,623
  • 15
  • 19
5

Try this:

node --preserve-symlinks -r ts-node/register src/cli.ts printer:A
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Ron
  • 6,037
  • 4
  • 33
  • 52
4

NODE_OPTIONS

For the case of node options, in addition to -r ts-node/register mentioned at https://stackoverflow.com/a/60162828/895245 they now also mention in the docs the NODE_OPTIONS environment variable: https://typestrong.org/ts-node/docs/configuration/#node-flags

NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' ts-node ./index.ts

A quick test with:

main.ts

(async () => { throw 'asdf' })()

and run:

NODE_OPTIONS='--unhandled-rejections=strict' ts-node main.ts
echo $?

which gives 1 as expected.

Tested on Node v14.16.0, ts-node v10.0.0.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985