Search code examples
node.jscmdenvironment-variables

node command returns nothing in Command Prompt (Windows 10)


Lately, I was trying to install NodeJs on my desktop PC and I ran into this weird problem. When I type, eg. node -v command, it returns nothing. After node I can add anything, it will just be ignored and won't return anything in the next line. I've added C:\Program Files\nodejs to PATH system variable, but it still doesn't work. Only time node -v command works is when I do (in cmd) C:\Program Files\nodejs>cd C:\Program Files\nodejs, then C:\Program Files\nodejs>node -v and in the next line it returns proper verision v16.13.1 .

Obviously, node command works and returns properly only when I run command from \nodejs folder, but as I saw on the Internet, it should work (with system PATH set up) from any folder. I think something is messed up in my Environment Variables, but if you know how to solve this problem, please let me know! Every comment is appreciated!

I downloaded NodeJs from https://nodejs.org/en/download/ (LTS, Windows Installer, 64-bit). Thanks in advance!

PS. npm command works correctly from any folder.

@Compo cmd screenshot


Solution

  • Your issue is that you have many things in the %Path%, and clearly have made errors when adding for node.exe.

    The first noted problem is that you have added C:\Program Files\nodejs\node.exe when you should have used only C:\Program Files\nodejs. Entries in this variable are for the directories which hold your executable files, not the files themselves. However, as C:\Program Files\nodejs is already included, you can simply remove that invalid entry.

    The next issue, is that you need to understand what happens when you enter node in the Command Prompt or a batch file. What happens is that the current directory is searched for files named node which have an extension matching one listed in the %PATHEXT% value, (which is searched in order first to last). If a match is found, that file is run, and the searching stops. If no file is found, the same process occurs with every location listed under %Path%, (in its listing order first to last), the first matching file is run and the searching stops.

    So by using node, what happens in your case is the %Path% is being searched, because there is no file named node.COM, node.EXE, node.BAT, node.CMD, node.VBS, node.VBE, node.JS, node.JSE, node.WSF, node.WSH, or node.MSC in the current directory. So each location is searched in order, until it reaches the first match, which in your case happens to be C:\xampp\htdocs\WebRulet\node.JS.

    So essentially by using a presumptive/lazy command you are effectively running:

    C:\xampp\htdocs\WebRulet\node.JS -v
    

    Which is not what you wanted, and why you are not getting the result you had hoped for.

    So now you understand the process which happens, by using code which make assumptions, and how that could cause issues, errors, or potential catastrophies. You should realize with all of that searching, especially if you have many entries in your %Path% and/or %PATHEXT% values, that the quickest and safest way to run your command would be:

    "C:\Program Files\nodejs\node.exe" -v
    

    Or

    "%ProgramFiles%\nodejs\node.exe" -v
    

    Please note that those absolute paths are double-quoted because they contain space characters. However, spaces are not the only problematic characters used in filenames, so best practice is, unless you are certain there are no such poison characters, to always double-quotes.

    Now I know that almost every site will never explain all of that information, and all the code you read will not follow it either, so you are probably going to want to minimize your typing whilst working on the command line.

    In order to do that, you will need to ensure that your %Path% value string, is ordered in such a way as your most frequently typed executable file path, is nearer the beginning, than any other location holding a possible matching file. However I will strongly suggest that you always use file extensions, for safety, (it is, after all, usually just four more characters to type).

    Noting the entries in your %Path%, there are some extremely important ones missing, which means that your %Path% is essentially corrupted, and requires fixing because it will seriously affect the proper running of your Operating System.

    To fix your variables, and order them correctly, begin by typing the following in your Command Prompt window:

    Start %SystemRoot%\System32\SystemPropertiesAdvanced.exe
    

    In the window which opens, click on the [Environment Variables] button. A new window will open, within the User variables (upper pane), double-click on Path, and using the [New] [Delete], [Move Up] and [Move Down] buttons make sure that the entries in it, in this order, are:

    %UserProfile%\AppData\Roaming\npm
    %UserProfile%\.dotnet\tools
    %UserProfile%\AppData\Local\Microsoft\WindowsApps
    %UserProfile%\AppData\Roaming\Composer\vendor\bin
    D:\Inkscape\bin
    

    Once done, click on [OK] to close the window, then do the same thing for System variables, (lower pane), with the following ordered list:

    %SystemRoot%\System32
    %SystemRoot%
    %SystemRoot%\System32\wbem
    %SystemRoot%\System32\WindowsPowerShell\v1.0
    %SystemRoot%\System32\OpenSSH
    %ProgramData%\ComposerSetup\bin
    %ProgramData%\DockerDesktop\version-bin
    %ProgramFiles%\Docker\Docker\resources\bin
    %ProgramFiles%\Azure Data Studio\bin
    %ProgramFiles%\nodejs
    %ProgramFiles%\dotnet
    %ProgramFiles%\heroku\bin
    %ProgramFiles%\Oracle\VirtualBox
    %ProgramFiles%\NVIDIA Corporation\NVIDIA NvDLISR
    %ProgramFiles(x86)%\NVIDIA Corporation\PhysX\Common
    %SystemDrive%\xampp\bin
    %SystemDrive%\xampp\htdocs\WebRulet
    

    Once complete click on [OK], [OK], and [OK] to close your windows, and then close the Command Prompt window.

    From now on you should be able to open any new Command Prompt window and use:

    node -v
    

    But remember, I strongly advise that you get into the habit of using its extension:

    node.exe -v
    

    Which should result in:

    v16.13.1