1

I need to start Node.JS (JavaScript) script named "Node.js", but Windows wont let me do it. When I am in same folder and enter command node Node.js, my file gets opened with default editor... How to override this functionality and make commands first and files second in queue of launching.

1
  • Two questions: Does node.js run the script correctly if you use npm start? Does it still open in a text editor regardless of the file name? (Rename it to index.js for example) Also, if there is more than one node.exe located by your PATH environment variable, the first exe with that name to be found will be used; That could be problematic if it's not actually Node.js's node.exe.
    – Romen
    Dec 11, 2019 at 21:45

1 Answer 1

1

This is indeed quirky behavior. It looks like a bug having to do with the order windows parses the command line for execution. Perhaps it isn't.. I don't know what the programmer intended.

Simple solution, use .exe after node (as in "node.exe node.js") or rename the script to not be node.js.

I can't completely explain it, but I do understand what it happening here.

When you run node node.js, ".js" is listed as a type of program that windows can execute. From the command line, type echo %PATHEXT% and you will find ";.JS" somewhere on that list.

When you run something in windows and don't provide an extension windows will check this PATHEXT list to see what to do. It looks like it ignores the order and looks for .EXE last (bug?) and the local directory before going to the path.

If the thing you are running is in the list, it will lookup the association in the registry for what to run. In this case, .JS is associated in windows with your editor.

How can I tell? I had the same problem as you reproducing this bug making it easy to figure out.

  • If I did node node.js it fired up webstorm with a command line of "webstorm64.exe node node.js". Webstorm was my default for .js files. Also, note the extra (stupid) node passed to it...

  • When calling node.exe node.js, windows didn't try to lookup the file association for the .JS file and everything worked just peachy.

  • After editing my PATHEXT list to not contain .JS, the problem also went away. You can edit PATHEXT using your Windows environment.

Specifically, if "How to override this functionality and make commands first and files second in queue of launching".. I tried rearranging the PATHEXT variable to no avail. I can't really tell you what it is doing under the hood. Perhaps it is because there was more than one file named "node" specified so it went with the JS file instead?? I dunno.

1
  • Removing ;.JS from PATHEXT works just fine. The only annoying thing is that Windows puts it back after some updates. Jul 20, 2021 at 0:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .