3

I have a node script that is receiving the following argument: ^PPP.

I am calling the script as following: npm run scriptName ^PPP.

However inside the script if I do a console.log(process.argv), the output shows my argument as PPP.

I tried escaping the character as npm run scriptName \^PPP and npm run scriptName "^PPP" but to no avail.

Please help how can I receive the original string from the arguments.

PowerShell to run command and v10.16.2 node version

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Pedro Pereira
  • 312
  • 5
  • 21

1 Answers1

0

It depends on your terminal.

I made a simple script called args.js with

console.log(process.argv);

Using git bash on windows or WSL (Ubuntu), calling

$node args.js ^PPPP

Outputs

[ 'node/path', 'path/to/args.js', '^PPPP' ]


Using windows cmd terminal, calling

>node args.js ^PPPP

Outputs

[ 'C:\Program Files\nodejs\node.exe', 'C:\workspace\tests\args.js', 'PPPP' ]

and calling

>node args.js ^^PPPP

Outputs

[ 'C:\Program Files\nodejs\node.exe', 'C:\workspace\tests\args.js', '^PPPP' ]


So if you are using Windows's terminal, you need to double the ^ character (What does the single circumflex in the windows cmd shell mean: More?). On other terminals, it seems to work fine.


Edit: To add arguments to your node script from npm run, you need to separate them with -- like so:

>npm run scriptName -- ^^PPP
Seblor
  • 6,947
  • 1
  • 25
  • 46
  • Although I have just tested this and this is correct. This is only happening if I call the script directly by ```node scriptName ^^PPP``` but I am trying to run a script defined in the ```package.json```. So want I want to do (that is failing) is ```npm run scriptName ^^PPP``` and the latter is still not working. – Pedro Pereira Nov 08 '19 at 14:51
  • To add arguments to your node script from `npm run`, you need to separate them with `--` like so : `npm run scriptName -- ^^PPP` – Seblor Nov 08 '19 at 15:46