How to pass parameters to a script processed by ts-node

Victor Shelepen picture Victor Shelepen · Jan 4, 2019 · Viewed 7.7k times · Source

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.

Answer

Steve Boyd picture Steve Boyd · Jan 21, 2019

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'