Basically, I have set .js files to be executed with node so that I can use them for various shell scripting purposes. Something I just noticed is that if I have the following code in a file called eg. foo.js:
console.log(process.argv);
Now, I can call this from the command line from the same directory with either node foo or just plain foo, but if I use the latter then whatever additional command line arguments I pass don't seem to get passed on to the script itself and thus aren't available to the script via process.argv. Example:
$ node foo bar baz
[ 'C:\\Program Files\\nodejs\\node.exe',
'O:\\foo',
'bar',
'baz' ]
$ foo bar baz
[ 'C:\\Program Files\\nodejs\\node.exe',
'O:\\foo.js' ]
It would be nice if process.argv contained the passed arguments proper even in the latter situation.
This is not an issue with Node.js, it is dependent on how .js files are associated with Node.js in Windows registry. If you just select "Open with" by GUI, it creates this association:
"C:\Program Files\nodejs\node.exe" "%1"
and none of the extra parameters will be transferred.
Try to run these commands in the cmd.exe (or save them in the .bat file and run it):
ftype JSFile="C:\Program Files\nodejs\node.exe" "%1" %*
assoc .js=JSFile
Ref: https://github.com/nodejs/node/issues/4725 (I've stumbled upon this issue myself :) )
Ref: https://github.com/nodejs/node/issues/6273 (I've proposed to do this properly during installation, but it seems this proposing do not get traction for now)
I suspected it was caused by the way Windows associates .js files to be ran with Node, seems I was right. It would indeed be pretty nice if the installer could handle this like it tends to do with other languages, but at least I can manually fix it for the time being with some registry editing.
closing as answered, let me know if there is anything pending
Most helpful comment
This is not an issue with Node.js, it is dependent on how
.jsfiles are associated with Node.js in Windows registry. If you just select "Open with" by GUI, it creates this association:and none of the extra parameters will be transferred.
Try to run these commands in the
cmd.exe(or save them in the.batfile and run it):