Modifying $PATH variable
Solution 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.
Solution 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.
Solution 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'
user273072545345
Updated on July 09, 2022Comments
-
user273072545345 almost 2 years
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 yourPATH
environment
variable to have npm-installed binaries picked up:
/usr/local/share/npm/binOk ... 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 thisexport 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...
-
ThiefMaster over 10 yearsNow if there's a
npm
binary e.g. in/usr/local/bin
the one in/usr/local/share/npm/bin
won't be used at all... -
macedigital over 10 yearsThanks for downvoting, mate. A
which npm
(with or without above change(s)) would reveal if and where the first matchednpm
binary (in order of specified directories...) is located. -
ThiefMaster over 10 yearsTrue, but he might want to override another (older/outdated) version that's installed in there. Anyway, it would probably be a good idea to mention the reasoning why you did not prepend the directory in your answer.
-
macedigital over 10 yearsI see your point, but figured it'd be less confusing ("Trying to install node.js") to just point out the missing colon. Anyways, I edited my answer.
-
macedigital over 10 yearsCould you
ls -la /usr/local/share/npm/bin
just to make sure, thenpm
command is actually in this directory. You can also trywhich -a npm
, so it lists all found entries, not just the first one. As above path comes before/usr/local/bin
it should have been picked first. -
user273072545345 over 10 years@macedigital, hey, updated with the additional info as requested ... seems I only have one version ...
-
macedigital over 10 yearsOK, I guess I know now what the issue is. You installed npm alright, homebrew put the executable in
/usr/local/bin
(which makes sense, btw). All node-modules you install that provide a binary will be put in/usr/local/share/npm/bin
(also makes sense). As you have not yet done so, the directory doesn't exist. Steps to verify: 1.)ls -a /usr/local/bin/npm
<- it should have a timestamp around when you installed with homebrew. 2.)npm -g install recess
(just an example). <- 'recess' should be in/usr/local/share/npm/bin
-
macedigital over 10 yearsSorry, my bad, I meant to say
ls -l
. I just updated my answer so its hopefully a bit more clear. Basically just try to install a node-module that provides a binary,npm -g install express
is a good starting point. After npm did its trick, express should be available as a command on the command line.which express
should then point to/usr/local/share/npm/bin/express
. If that is the case you finally enjoy checking out all the cool nodejs stuff ;) -
user273072545345 over 10 years@macedigital, hey, this is what i got: lrwxr-xr-x 1 name admin 29 Dec 22 10:38 /usr/local/bin/npm -> ../Cellar/node/0.10.9/bin/npm ... so, yes this shows the timestamp. and yes, which express shows that directory. (this is a modified comment as i asked about grunt and then changed my mind as that's for another post).