node.js 修改 $PATH 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20732936/
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 16:32:45  来源:igfitidea点击:

Modifying $PATH variable

node.jspathterminalenvironment-variables

提问by user273072545345

Trying to install node.js.

尝试安装 node.js。

Did brew install node

做过 brew install node

It seems to have worked.

它似乎奏效了。

However, received this message upon its completion

但是,在完成后收到此消息

Homebrew installed npm.
We recommend prepending the following path to your PATHenvironment
variable to have npm-installed binaries picked up:
/usr/local/share/npm/bin

Homebrew 安装了 npm。
我们建议在您的PATH环境
变量前面添加以下路径以获取 npm 安装的二进制文件:
/usr/local/share/npm/bin

Ok ... so, I open my bash_profile...

好的...所以,我打开我的bash_profile...

And this is what I have in it:

这就是我所拥有的:

 export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Trying to understand how to modify it correctly so I won't ruin it ...

试图了解如何正确修改它,这样我就不会破坏它......

Do I add /usr/local/share/npm/binlike this

添加我/usr/local/share/npm/bin喜欢这个

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

If not, what is the correct way to add that path?

如果没有,添加该路径的正确方法是什么?

Thank you for any help provided!

感谢您提供的任何帮助!

PS. let me know if there is any additional information I could have provided

附注。如果我可以提供任何其他信息,请告诉我

EDIT

编辑

upon seeing which npmin macedigital's answer, I ran that ...

在看到which npmmacedigital 的回答后,我跑了......

and got this: /usr/local/bin/npm

得到了这个: /usr/local/bin/npm

and that was before I did the second answer (ie, ThiefMaster's answer).

那是在我做第二个答案之前(即 ThiefMaster 的答案)。

ran which npmagain ...

又跑which npm了……

and got the same answer as before ...

并得到了和以前一样的答案......

i did echo $PATHand got this:

我确实回声$PATH并得到了这个:

/Users/name/.rvm/gems/ruby-1.9.3-p374/bin:/Users/name/.rvm/gems/ruby-1.9.3-p374@global/bin:/Users/name/.rvm/rubies/ruby-1.9.3-p374/bin:/Users/name/.rvm/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin

/Users/name/.rvm/gems/ruby-1.9.3-p374/bin:/Users/name/.rvm/gems/ruby-1.9.3-p374@global/bin:/Users/name/.rvm/ rubies/ruby-1.9.3-p374/bin:/Users/name/.rvm/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/local/sbin:~/bin :/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin

So, it looks like I already had it installed?

所以,看起来我已经安装了它?

Therefore, how do I handle the answers? I hate leaving it unresolved since both of you were so helpful and I feel bad that I asked without providing echo $PATHinformation since that would have told you that I had it installed ...

因此,我该如何处理这些答案?我讨厌让它悬而未决,因为你们两个都非常有帮助,我很遗憾我没有提供回声$PATH信息就问了,因为那会告诉你我已经安装了它......

EDIT 2

编辑 2

ls -la /usr/local/share/npm/bingets this:

ls -la /usr/local/share/npm/bin得到这个:

ls: /usr/local/share/npm/bin: No such file or directory

ls: /usr/local/share/npm/bin: 没有那个文件或目录

which -a npmgets this: /usr/local/bin/npm

which -a npm得到这个: /usr/local/bin/npm

EDIT 3

编辑 3

ls -a /usr/local/bin/npmgets this: /usr/local/bin/npm

ls -a /usr/local/bin/npm得到这个: /usr/local/bin/npm

there's no timestamp...

没有时间戳...

回答by macedigital

Short answer, do this (notice the additional colon I inserted):

简短的回答,这样做(注意我插入的额外冒号):

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

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

The $PATHenvironment variable is colonseparated list of directories to look in if you want to run a command without a fully qualified path (e.g. running npminstead of having to type /usr/local/share/npm/bin/npm).

$PATH环境变量是冒号分隔的目录中,如果你想没有一个完全合格的路径运行命令来看看的列表(例如运行npm,而不必到类型/usr/local/share/npm/bin/npm)。

You can try this from a terminal before actually saving the change in bash_profile. If everything is good, which -a npmwill show you all fully qualified path(s).

在实际保存 bash_profile 中的更改之前,您可以从终端尝试此操作。如果一切正常,which -a npm将向您显示所有完全合格的路径。

UPDATE

更新

It is not necessary to modify the $PATH variable in order to use npm. What homebrew install recommends instead is to add the directory where npm-installed binaries are stored to the $PATH variables, so its more convenient to use them from the command line later on.

无需修改 $PATH 变量即可使用 npm。homebrew install 推荐的是将存储 npm 安装的二进制文件的目录添加到 $PATH 变量中,以便稍后从命令行使用它们更方便。

Node modules like phantomjs, phonegap, express, etc. provide binaries which after the change are available on the command prompt without having to type the full path.

phantomjs、phonegap、express 等节点模块提供二进制文件,更改后可在命令提示符下使用这些二进制文件,而无需键入完整路径。

回答by ThiefMaster

The cleanest solution is adding the following between the two lines you posted:

最干净的解决方案是在您发布的两行之间添加以下内容:

export PATH="/usr/local/share/npm/bin:$PATH"

That way everything stays readable and you prepend it to PATH just like the program suggested it. And if you ever want to undo the change you just remove that line instead of editing a possibly long line.

这样一切都保持可读,你可以像程序建议的那样将它添加到 PATH 中。如果您想撤消更改,只需删除该行而不是编辑可能很长的行。

回答by SkorpEN

In PATH ORDER IS IMPORTANT. So anything before desired npm version will still cause problems.

路径顺序很重要。因此,在所需的 npm 版本之前的任何内容仍然会导致问题。

#adding in first place of the path, before anything else
export PATH=/usr/local/bin:otherPathEntries:$PATH

assuming that version of npm You want is in /usr/local/bin, to check all use 'which -a npm'

假设你想要的 npm 版本在 /usr/local/bin 中,检查所有使用'which -a npm'