node.js 为什么 npm install 在 git bash 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35915832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:02:40  来源:igfitidea点击:

How come npm install doesn't work on git bash

node.jsgitshellnpmgit-bash

提问by damien9999

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

我打开 git bash 并输入npm install,然后它返回:

bash: npm command not found

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

我不明白,因为我有node.js命令提示符,当我输入时,npm -v它返回3.7.3. 为什么它在 git 中不起作用?

回答by Ben

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

在我们的例子中,解决方案只是关闭 Git bash 窗口并重新打开它。

回答by danday74

In git bash type ...

在 git bash 类型中...

which npm

This will tell you where npm is installed.

这将告诉您 npm 的安装位置。

Now, I'm assuming this will give you nothing since it seems npm is not on your system PATH variable. The PATH variable defines where Windows looks for commands.

现在,我假设这不会给您任何东西,因为 npm 似乎不在您的系统 PATH 变量中。PATH 变量定义了 Windows 查找命令的位置。

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

进入您的控制面板并单击系统高级设置并将包含 npm 的目录添加到您的路径中。

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

如果您不知道它在哪里,请打开一个普通的 Windows 控制台并键入...

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.

然后关闭你的 git bash 终端并重新打开它,npm 应该可以工作了。

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

PS 如果你想检查 git bash 中的 PATH 变量,请确保正确的 npm 文件夹在 PATH 上,然后只需输入 ...

echo $PATH

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

PPS 另一个提示 - 您只能通过 .bashrc 或 .bash_profile 文件调整 git bash 的 PATH。

回答by Tiago Oliveira

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

如果您使用的是 Windows,请尝试以下操作:在 CMD 中,转到文件夹

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!

这个对我有用!

回答by Marc Barbeau

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

git 安装的默认终端模拟器名为Mintty。要npm使用 Mintty运行,您需要将路径添加到环境变量中。

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

将以下行添加到您的~\.bash_profile文件中。如果它不存在,请创建它。

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 npmfrom Mintty.

当 Mintty 启动时,它会显示.bash_profile. 您现在应该可以npm从 Mintty运行了。

These other questions might be helpful also.

这些其他问题也可能有帮助。

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

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

回答by Devnegikec

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

您需要将 nodejs 与 npm 一起添加到您的路径中

where node

where npm

节点

哪里 npm

回答by Nevitones

Are you on Windows?

你在 Windows 上吗?

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

如果是这样,您可以转到 Git Bash 控制台并运行:

echo $PATH

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

然后检查节点路径(例如 c/Program Files/node )是否存在?

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 吗?

"npm"

回答by Gerardo Grignoli

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

假设您在 Windows 上尝试使用 git-bash,并且节点是由 Visual Studio 安装的:原因可能是缺少npmbash 脚本。

There is an npm.cmdbath file in the path:

npm.cmd路径中有一个bath文件:

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

But git bashwont run .cmdfiles. So you need to create a bash script for npm.

git bash不会运行.cmd文件。所以你需要为 npm 创建一个 bash 脚本。

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

npm在您的节点文件夹中创建以下文件:( C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\)

#!/bin/sh
basedir=`dirname "
npm --version
node --version
"` 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

回答by Terix

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

我必须将节点路径添加到系统变量并重新启动。出于某种原因,关闭并重新打开 git bash 是不够的

回答by Patee Gutee

I know this post is old but this could help someone who installed Node / npmvia Visual Studio Installer. I tried every solution I could find online but none works until I did the following:

我知道这篇文章很旧,但这可以帮助那些Node / npm通过Visual Studio Installer. 我尝试了我可以在网上找到的所有解决方案,但在我执行以下操作之前都无效:

  • Uninstall Nodeusing Visual Studio Installer.

  • Install it using installer downloaded from the Node.js site. Use default options.

  • 卸载Node使用Visual Studio Installer

  • 使用从Node.js 站点下载的安装程序进行安装。使用默认选项。

回答by smartdev007

If you installed git bash first and then node.js, uninstall gitbash and keep node.js. And then re-install git bash again.

如果先安装 git bash 再安装 node.js,请卸载 gitbash 并保留 node.js。然后再次重新安装git bash。

##代码##

check version using this command

使用此命令检查版本