Modifying $PATH variable

2020-06-03 07:32发布

问题:

Trying to install node.js.

Did 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 PATH environment
variable to have npm-installed binaries picked up:
/usr/local/share/npm/bin

Ok ... so, I open my 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/bin like this

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 npm in macedigital's answer, I ran that ...

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

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

ran which npm again ...

and got the same answer as before ...

i did echo $PATH and got this:

/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 $PATH information since that would have told you that I had it installed ...

EDIT 2

ls -la /usr/local/share/npm/bin gets this:

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

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

EDIT 3

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

there's no timestamp...

回答1:

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"

The $PATH environment variable is colon separated list of directories to look in if you want to run a command without a fully qualified path (e.g. running npm instead of having to type /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 npm will show you all fully qualified path(s).

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.

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.



回答2:

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.



回答3:

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

#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'