How come npm install doesn't work on git bash

2019-03-17 12:49发布

问题:

I have git bash open and I type in npm install and then it returns:

bash: npm command not found

I don't understand, because I have node.js command prompt and when I type in npm -v then it returns 3.7.3. How come it doesn't work in git?

回答1:

in git bash type ...

which npm

this will tell you where npm is installed.

now i'm assuming this will give u nothing since it seems npm is not on your system PATH variable. The PATH variable defines where Windows looks for commands.

Go into your control panel and click system advanced settings and add the directory containing npm to your path.

if you dont know where it is then open a normal windows console and type ...

where npm

this will show you where it is so you can add it to your path

then close your git bash terminal and reopen it and npm should work.

PS if you want to check the PATH variable inside git bash to make sure the correct npm folder is on the PATH then just type ...

echo $PATH

PPS Another tip - you can tweak the PATH for git bash only via your .bashrc OR .bash_profile files



回答2:

If you are on Windows, try this: In CMD, go to folder

C:\Program Files or (x86)\nodejs\

Then try the following

C:\Program Files\nodejs>set path=%PATH%;%CD%
C:\Program Files\nodejs>setx path "%PATH%"

It works for me!



回答3:

In our case, the solution was simply to close the Git bash window and re-open it.



回答4:

you need to add nodejs to your path, along with npm

where node

where npm



回答5:

The terminal emulator installed by git is named Mintty. To run npm with Mintty, you need to add the path to an environment variable.

Add the following line to your ~\.bash_profile file. If it does not exist, create it.

PATH="/usr/local/share/npm/bin:/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

When Mintty starts up it will read .bash_profile. You should now be able to run npm from Mintty.

These other questions might be helpful also.

https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path https://askubuntu.com/questions/58814/how-do-i-add-environment-variables



回答6:

Are you on Windows?

If so, can you go to the Git Bash console and run:

echo $PATH

and then check if the node path(e.g. c/Program Files/node ) is there?

If the node path is not there you'll need to add it to the system path variable.

If it's there, can you try to run npm between double quotes?

"npm"


回答7:

Assuming you are on Windows trying git-bash, and node was installed by Visual Studio: The cause may be a missing npm bash script.

There is an npm.cmd bath file in the path:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\npm.cmd

But git bash wont run .cmd files. So you need to create a bash script for npm.

Create the following file named npm in your node folder: (C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\)

#!/bin/sh
basedir=`dirname "$0"`

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
  ret=$?
else 
  node  "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
  ret=$?
fi
exit $ret


回答8:

I had the same issue and I successed to solve it by changing the git enviornment system variable from C:\Program Files\Git\cmd to C:\Program Files\Git

Hope that will help someone



回答9:

I had to add node path to system variable AND reboot. For some reason closing and reopening git bash was not enough