npm works via command prompt but not git bash

1k Views Asked by At

I used node version manager for windows to install NodeJS by executing:

nvm install 16.2.0

npm still wasn't working, so I ran

nvm list available

Which brought me to here: https://nodejs.org/download/release

I copied the files from npm/npm-1.4.9.zip to the v16.2.0 directory and confirmed it was working by opening a command prompt and executing:

npm -v

However, if I open Git BASH and attempt to execute the same command I get the following error message:

bash: npm: command not found

So I followed umpteen Stack Overflow threads on how to resolve this issue. Nearly every one said to restart Git BASH or Visual Studio Code, so I did and it didn't work. I then restarted my computer and it didn't work. So then I tried updating my PATH by executing the following in my command prompt:

npm config get prefix

Then taking that value and adding it to my PATH environment variable. I then shut every thing down, restarted the computer, opened Git BASH and tried running npm -v again, but it still gives me the same error.

I'm at my wits end and don't know why npm works in my command prompt but will not work in git bash.

UPDATE It is worth mentioning that node -v works fine from Git BASH, it is just npm -v

2

There are 2 best solutions below

0
On BEST ANSWER

I was able to solve my issue, though I'm unsure if this is a bug with NVM or not.

As of writing this, the current LTS version of NodeJS is 14.17.0. So I executed:

nvm install 14.17.0
nvm use 14.17.0

This not only installed NPM properly, but it also setup the PATH for me.

So for some reason, installing the newest version of NodeJS via NVM did not install NPM but installing the current LTS version did.

0
On

You'll need to add NVM to your C:\Users\PROFILE\.bashrc (If this file doesn't exist, create it.)

.bashrc:

export NVM_DIR="$NVM_HOME"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Side note: (Some older tutorials for NVM use export NVM_DIR="$HOME/.nvm", which is not what we want. If using the latest version of NVM for Windows, In order for cmd and git bash to be identical, we need export NVM_DIR="$NVM_HOME".)