Node and npm are installed but Git bash doesn't recognize 'node' inside 'npm run dev' (other shells work fine)

Viewed 2584

I have npm and node installed (tried NVM for Windows and direct installations).

When running 'npm -v' or 'node -v' in Git Bash everythings works fine. But when I try to run 'npm run dev' (or any other command) the output says that 'node command is not recognized'.

In other shells (CMD/Powershell) everything works fine.

I've checked Path variable in Windows, Path variable in Git Bash, everything seems to be correct.

Error screenshot Path variable screenshot

Path variables cmd/procmon

Any help appreciated.

p.s. While I was trying to find an answer I saw the same question from @jameseg , maybe if he sees this one he could help.

1 Answers

Your PATH environment variable is quite a mess. It has duplicate entries, also has an entry C:\Program Files\nodejs\node.exe which is not valid because it should be a folder, not a file, and it has . in the middle which doesn't make much sense either.

But the main problem is that it has a stray doublequote, after C:\Program Files\Java\jdk-13.0.1\bin:

stray doublequote

With this, effectively all the paths after it are ignored, because they are treated as part of one big quoted string (which is implicitly terminated by the end of the variable data).


To illustrate what I mean, consider this example:

This correct PATH variable...

C:\a;C:\b;"C:\c 123";C:\d;C:\e

...is interpreted as:

C:\a
C:\b
C:\c 123
C:\d
C:\e

But, this bad PATH variable where I deleted one of the quotes...

C:\a;C:\b;C:\c 123";C:\d;C:\e

...is interpreted like this:

C:\a
C:\b
C:\c 123";C:\d;C:\e

This may at first make only half sense, but it's because of the quirky way Windows parses this variable: When encountering a doublequote, it's removed from the result but toggles a flag that says whether we are now inside a quoted string. And when the flag is set, semicolons are ignored. So even if the stray quote is at the end of a path (or in the middle of it), it will have the effect of essentially quoting the rest of the variable data until the next doublequote or the end of the data.

Confusingly, you may still have where node report that it found node, because the where.exe tool does its own parsing, in a slightly different way (ignoring quotes), so you cannot rely on its output! (For example, try set PATH=c:\win""dows. where explorer will say it can't be found, yet explorer will work to open Explorer. You get the opposite with something like set PATH=x"y;c:\windows - where explorer will list c:\windows\explorer.exe, yet explorer will not work.) The reason why it works in Git Bash is probably the same: when the environment variables are translated to UNIX paths, they are parsed slightly differently than Windows would do it itself, inadvertently correcting the problematic entry in the process.

So, the solution is to remove this doublequote from your path variable.

Related