Node: process.argv doesn't contain passed arguments when .js files are run "bare" on Windows

Created on 24 Mar 2018  路  4Comments  路  Source: nodejs/node

  • Version: 8.9.0
  • Platform: Windows 10 (64-bit)
  • Subsystem: process

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.

process windows

Most helpful comment

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

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments