npm global path prefix
Asked Answered
E

12

165

I am being more cautious than usual because I have been confused by the behavior of npm in the past.

I am on a Mac and have installed node.js through brew install node.

Now that I want to run jslint.js on the command-line as the command jslint I find that the canonical way to accomplish this is sudo npm install -g jslint which ran successfully with this output:

$ sudo npm install -g jslint
npm http GET https://registry.npmjs.org/jslint
npm http 200 https://registry.npmjs.org/jslint
npm http GET https://registry.npmjs.org/jslint/-/jslint-0.1.9.tgz
npm http 200 https://registry.npmjs.org/jslint/-/jslint-0.1.9.tgz
npm http GET https://registry.npmjs.org/nopt
npm http 200 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz
npm http 200 https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz
npm http GET https://registry.npmjs.org/abbrev
npm http 200 https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/abbrev/-/abbrev-l.0.4.tgz
npm http 200 https://registry.npmjs.org/abbrev/-/abbrev-1.0.4.tgz
/usr/local/share/npm/bin/jslint -> /usr/local/share/npm/lib/node_modules/jslint/
bin/jslint.js
[email protected] /usr/local/share/npm/lib/node_modules/jslint
└── [email protected] ([email protected])

Subsequently

$ jslint ply.js
zsh: command not found: jslint

due to /usr/local/share/npm/bin not being in my $PATH.

1) Why did brew not install global npm bin path to path? Maybe it did, but something zsh does is messing it up. Where might I be able to find that?
2) Should I do this? (Append :/usr/local/share/npm/bin to the exported $PATH at the bottom of my ~/.zshrc)

It seems like this isn't the right way to do it because if I install something else later (using Homebrew or something) I'll be needing to append it to my zsh startup script to set the path. I guess in this particular instance it's just a matter of the npm install -g not making the right symlinks in a "proper" location (like /usr/local/bin maybe).

I think what I will do is manually build out symlinks inside /usr/local/bin for any programs that I have trouble with and it should be good enough for my purposes.

EDIT: I'm updating this in 2019, because this old question is popular but now outdated both in terms of my personal workflow and also in terms of how to best go about installing node and managing its various dependencies.

My personal workflow is now to use the installer obtained from nodejs.org to install node on systems. I actually trust this more than the homebrew package itself, because it knows best how to manage the files. If you want to change your system node from 8.10 to 10.15, whether on Mac or Windows, simply download the installer from nodejs.org for 10.15 for your OS. Attempting to upgrade node installed via homebrew has always proven to be a more difficult approach in my experience.

Also, the biggest thing to point out is that the use of sudo as shown in my npm install of jslint example above is highly discouraged; I am fairly certain that homebrew packages should never be installed or interacted with in any way using the superuser. It should not be necessary, and will lead to file permission headaches!

Furthermore I recommend ESLint over jslint, and have not used jslint in years.

Encephalography answered 10/2, 2013 at 23:42 Comment(7)
Thanks, they're fixed now. I'm not usually that careless but I typed this question up very quicklyEncephalography
@DaveNewton I usually do that, yeah. This question was a real sloppy one but it's one of my most popular ones.Encephalography
isn't it easier to copy text than screenshot? anyway fixed.Brueghel
@saleemrashid1 Hey I was meaning to do this. Did you really read it and copy it out? Thanks.Encephalography
Yep! You're welcome. I'm working on a game and while I was waiting for NPM to download stuff I did it!Brueghel
I recommend brew unlink node and brew install nvm. It asks you to add some things to your ~/.zshrc but greatly simplifies keeping multiple versions of node on your system. You can install latest with nvm install node.Unintelligent
@Unintelligent good advice about nvm. I still don't use it as much as I should, but it definitely works to reduce head trauma because you have other node versions ready to test at your fingertips.Encephalography
B
340

Extending your PATH with:

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

isn't a terrible idea. Having said that, you shouldn't have to do it.

Run this:

npm config get prefix

The default on OS X is /usr/local, which means that npm will symlink binaries into /usr/local/bin, which should already be on your PATH (especially if you're using Homebrew).

So:

  1. npm config set prefix /usr/local if it's something else, and
  2. Don't use sudo with npm! According to the jslint docs, you should just be able to npm install it.

If you installed npm as sudo (sudo brew install), try reinstalling it with plain ol' brew install. Homebrew is supposed to help keep you sudo-free.

Blackhead answered 12/2, 2013 at 19:32 Comment(14)
Thanks! npm refuses to install jslint globally, though. npm install -g jslint fails. Error: EACCES, open '/Users/user/.npm/564fcf84-jslint.lock'Encephalography
Wouldn't I need to chown /usr/local if I am to have any scripts write links into it without requiring sudo? How about following this to set nom/node up in ~/.local or something?Encephalography
SWEET! I went and undid the various dirs deposited by user root in my ~/ dir (by sudo rm -r on tmp/ and a .npm/) and subsequently a npm install -g jslint worked beautifully. It's neat that npm itself is a node program and is a bit of a JS command line proof-of-concept for me which I am beginning to appreciate now that I understand it a bit better.Encephalography
@StevenLu Yep, chown-ing /usr/local and everything in it is standard practice on OS X. See the troubleshooting page on the Homebrew wiki.Blackhead
Yep it looks like the chowning of /usr/local has already occurred on my machine otherwise npm et al run without sudo would not be able to deposit links into /usr/local/bin/ etc.Encephalography
NPM_PREFIX=`npm config get prefix` seems a more adaptable way of setting the prefix, then export PATH="$NPM_PREFIX/bin:$PATH"Muniment
@tadman, thanks for that tip! For me though they would both end up in the same exact place, ~/.zshrc.Encephalography
Thank you! This has driven me nuts! I think Titanium Studio set that bloody prefix value and it's been ruining everything for me today when I try and install new packages.Denicedenie
Can someone help me fixing the same issue in windows? I'm on windows7. When i do : npm install bower -g, it installs here. C:\Users\<username>\AppData\Roaming\npm While i try to run bower install underscore, it says bower is not recognized as internal or external command.Coursing
chown-ing /usr/local is a very hacky quick fix that will likely cause more trouble down the line. It also requires that you re-do it from time to time. It's better for people to setup their paths correctly so they don't have to lean on hacky practices. I agree with @Muniment that utilizing npm config and PATH is the way to go.Frodeen
@wilmoore I would disagree with the assertion that chown-ing /usr/local is "hacky." It's the standard Homebrew way of doing things, and Homebrew is very popular.Blackhead
@TrevorBurnham I'm OK with the fact that we disagree here.Frodeen
Sure. There's a good discussion of this very subject at apple.stackexchange.com/questions/1393/…Blackhead
npm seems to recommend using sudo: github.com/npm/…Arathorn
H
6

Spent a while on this issue, and the PATH switch wasn't helping. My problem was the Homebrew/node/npm bug found here - https://github.com/npm/npm/issues/3794

If you've already installed node using Homebrew, try ****Note per comments that this might not be safe. It worked for me but could have unintended consequences. It also appears that latest version of Homebrew properly installs npm. So likely I would try brew update, brew doctor, brew upgrade node etc before trying****:

npm update -gf

Or, if you want to install node with Homebrew and have npm work, use:

brew install node --without-npm
curl -L https://npmjs.org/install.sh | sh
Hord answered 10/4, 2014 at 3:33 Comment(3)
Casually recommending that people globally force an update is not a good idea.Infiltration
Blindly following the advice from someone on the internets is not a good idea.Skiest
The force method on -gf made it for me. Thank you.Brie
M
6

I use brew and the prefix was already set to be:

$ npm config get prefix
/Users/[user]/.node

I did notice that the bin and lib folder were owned by root, which prevented the usual non sudo install, so I re-owned them to the user

$ cd /Users/[user]/.node
$ chown -R [user]:[group] lib
$ chown -R [user]:[group] bin

Then I just added the path to my .bash_profile which is located at /Users/[user]

PATH=$PATH:~/.node/bin
Melgar answered 16/9, 2015 at 19:56 Comment(0)
H
5

I managed to fix Vue Cli no command error by doing the following:

  • In terminal sudo nano ~/.bash_profile to edit your bash profile.
  • Add export PATH=$PATH:/Users/[your username]/.npm-packages/bin
  • Save file and restart terminal
  • Now you should be able to use vue create my-project and vue --version etc.

I did this after I installed the latest Vue Cli from https://cli.vuejs.org/

I generally use yarn, but I installed this globally with npm npm install -g @vue/cli. You can use yarn too if you'd like yarn global add @vue/cli

Note: you may have to uninstall it first globally if you already have it installed: npm uninstall -g vue-cli

Hope this helps!

Hellenism answered 11/4, 2019 at 15:41 Comment(2)
This basically worked for me. I had to adjust it to export PATH=$PATH:/Users/[your username]/node_modules/.binDomingadomingo
Even though after I use export PATH=$PATH:~/.npm-global/bin my terminal is finding global packages correctly, I have to manually type it with every further terminal creation. How do I fix this?Postscript
C
3

brew should not require you to use sudo even when running npm with -g. This might actually create more problems down the road.

Typically, brew or port let you update you path so it doesn't risk messing up your .zshrc, .bashrc, .cshrc, or whatever flavor of shell you use.

Cryostat answered 11/2, 2013 at 0:19 Comment(3)
Indeed brew does not require and recommends against use of sudo. I was using sudo on npm (because something I read told me to). I'm curious about where to look up how the path is set. It actually seems like the zsh installer runs something that ends up hardcoding the path (at the time of installation of zsh) into the .zshrc which I'm not liking so muchEncephalography
Anything installed by npm should be installed in /usr/local. The /usr path namespace is reserved for your distro's official packages. /usr/local/bin in your $PATH or not, is a zsh issue.Sturgill
the brew scripts do not add anything to your startup script, this is something you need to do. see madebyhoundstooth.com/blog/install-node-with-homebrew-on-os-x for an example.Cryostat
M
2

In newer MacOS versions you need to edit the .zshrc file not .bash_profile:

In your terminal nano ~/.zshrc then add the line:

export PATH=$PATH:/Users/[your username]/.npm-packages/bin
Mislead answered 4/7, 2022 at 21:29 Comment(0)
L
0

Try running:

PATH=$PATH:~/npm/bin

and then do a test by running express in the command line. This worked for me.

Lineation answered 28/10, 2016 at 1:14 Comment(1)
pretty sure you mean ~/.npm/bin or some such. there shouldnt be an npm dir added to your home dir.Encephalography
L
0

If you have linked the node packages using sudo command

Then go to the folder where node_modules are installed globally.

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

Windows XP - %USERPROFILE%\Application Data\npm\node_modules Windows 7 - %AppData%\npm\node_modules

and then run the command

ls -l

This will give the list of all global node_modules and you can easily see the linked node modules.

Lezley answered 22/2, 2017 at 4:10 Comment(0)
O
0

sudo brew is no longer an option so if you install with brew at this point you're going to get 2 really obnoxious things: A: it likes to install into /usr/local/opts or according to this, /usr/local/shared. This isn't a big deal at first but i've had issues with node PATH especially when I installed lint. B: you're kind of stuck with sudo commands until you either uninstall and install it this way or you can get the stack from Bitnami

I recommend this method over the stack option because it's ready to go if you have multiple projects. If you go with the premade MEAN stack you'll have to set up virtual hosts in httpd.conf (more of a pain in this stack than XAMPP)plust the usual update your extra/vhosts.conf and /etc/hosts for every additional project, unless you want to repoint and restart your server when you get done updatading things.

Orthorhombic answered 1/3, 2017 at 16:53 Comment(0)
R
0

Any one got the same issue it's related to a conflict between brew and npm Please check this solution https://gist.github.com/DanHerbert/9520689

Reduplicate answered 15/4, 2017 at 16:5 Comment(0)
L
0

I uninstalled node from homebrew on M1 Apple Silicon and instead used the Mac Installer from https://nodejs.org/en/download/. After doing that I was able to install with npm and npx.

Lambaste answered 1/3, 2021 at 5:0 Comment(0)
A
-2

Simple solution is ...

Just put below command :

  1. sudo npm config get prefix

    if it's not something like these /usr/local, than you need to fix it using below command.

  2. sudo npm config set prefix /usr/local...

Now it's 100% working fine

Actuary answered 6/5, 2019 at 10:52 Comment(1)
Highly recommend avoiding sudo with npm. See my edit.Encephalography

© 2022 - 2024 — McMap. All rights reserved.