113

I am having a problem installing global node modules and everything I find online says the solve is just adding -g. Which is not the problem. I believe it's a linking issue or wrong directory issue.

Here is what I do:

$ npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.3
...downloads correctly

$ express myapp
bash: express: command not found

However when I run the direct link location to express it works:

   $ /usr/local/share/npm/bin/express myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
... Builds app correctly

Where the module is:

$ which node
/usr/local/bin/node
$ node -pe process.execPath
/usr/local/Cellar/node/0.8.20/bin/node
$ npm link express
/Users/bentonrr/Development/Personal/node_modules/express -> /usr/local/share/npm/lib/node_modules/express

In my .bash_profile I have:

export PATH=/usr/local/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node

Do I need to change my Node environment to download to correct folder? Is something not linking correctly? I am lost..

Thanks!

Other Specs:

$ node --version
v0.8.20
$ npm --version
1.2.11
$ brew --version
0.9.4
OSX Version 10.8.2
3
  • 1
    It's because /usr/local/share/npm/bin/ is not in your $PATH of your shell. That's why the shell can't find express. Feb 24, 2013 at 19:59
  • 2
    I added: export PATH=/usr/local/share/npm/bin:$PATH to my .bash_profile and it worked. Thanks! Add an answer so I can accept it and upvote.
    – im_benton
    Feb 24, 2013 at 21:42
  • In my case, also having NPM installed via Cellar on OSX, my bin path, which I set in .bash_profile, is: export PATH=$PATH:/usr/local/Cellar/node/10.5.0/bin
    – Shanerk
    Jun 20, 2019 at 16:45

11 Answers 11

164

This may mean your node install prefix isn't what you expect.

You can set it like so:

npm config set prefix /usr/local

then try running npm install -g again, and it should work out. Worked for me on a mac, and the solution comes from this site:

http://webbb.be/blog/command-not-found-node-npm/

EDIT: Note that I just came across this again on a new Mac I'm setting up, and had to do the process detailed here on stackoverflow as well.

EDIT 2: as mentioned by @MEMOGAMER000 in the comments

For Windows users, you can run npm config set prefix C:\Users\username\ instead of /usr/local/. –

8
  • I am using nodenv and there is most likely a better way to solve this issue for my case. However, your solution worked as a quick and dirty fix! I then switched the prefix back to the nodenv prefix and I was able to run the package from the /usr/local path. Thanks! Jan 19, 2017 at 6:31
  • 4
    I have spent months meaning to fix this and this is the only answer I've seen that fixed it for me! Thank you SO so much.
    – azz0r
    Jun 15, 2017 at 8:48
  • 1
    I tried a lot of solutions but only this one did it for me. For some reason my prefix was set to /Users/(username)/.npm-global even though I did a fresh install of everything. I'm also on a Mac
    – Castilho
    Jan 3, 2019 at 16:07
  • 1
    Thanks! npm config set prefix /usr/local worked for me on MacOS Ventura. Nov 2, 2022 at 9:13
  • 1
    For Windows users, you can run npm config set prefix C:\Users\username\ instead of /usr/local/. Sep 2, 2023 at 16:51
103

Add $(npm get prefix)/bin to your PATH (e.g., in .bashrc), like so:

echo "export PATH=$PATH:$(npm get prefix)/bin" >> ~/.bashrc

For more info, see npm help npm:

global mode: npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.

You can find the install prefix with npm get prefix or npm config list | grep prefix.

5
  • what about for non sudo using nvm, shouldn't the path be $HOME/.npm to the .bash_profile or .bashrc? I use nvm, so want to make sure im not cross tying things on this.
    – blamb
    May 3, 2017 at 5:16
  • 1
    For anyone that has installed Node and npm via Homebrew on OSX, the location of the bin folder that you need to add to the PATH will be /usr/local/Cellar/node/VERSION_NUMBER/bin/.
    – Jon Betts
    Nov 10, 2020 at 0:06
  • In my case, I extended Tim's answer a bit, so that the actual path is dynamic in the bash profile script: echo "export PATH=\$PATH:\$(npm get prefix)/bin" >> ~/.bash_profile I used .bash_profile instead, however you can also use .bashrc as well, in most cases. May 27, 2021 at 3:27
  • Additional tip: Use source ~/.bashrc to quickly update PATH w/o reboot.
    – Neeraj
    Feb 5, 2022 at 10:57
  • 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?
    – pokercatt
    Feb 18, 2022 at 10:16
17

My npm couldn't find global packages as well. I did what Brad Parks suggested:

npm config set prefix /usr/local

Then I got an EACCES permissions error (DON'T USE sudo npm install -g <package>) and fixed it through the official npm docs: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

  1. On the command line, in your home directory, create a directory for global installations:

    mkdir ~/.npm-global
    
  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'
    
  3. In your preferred text editor, open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH
    
  4. On the command line, update your system variables:

    source ~/.profile
    
  5. Then install a package globally and test it! For example:

    npm install -g awsmobile-cli
    awsmobile configure
    

Instead of steps 3-5, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global
2
  • 1
    hi everyone, I have question. After step 2, I don't find bin/ in ~/.npm-global. So , It don't work, right? Dec 3, 2019 at 9:14
  • It should still work. Don't forget that export PATH=~/.npm-global/bin:$PATH line is in the ~/.profile file. You get any error?
    – Vicente
    Dec 3, 2019 at 9:23
11

In my case, The NODE_PATH environment variable was empty. Check whether it is empty-

echo $NODE_PATH

if the NODE_PATH is empty. Then change ~/.bash_profile and add NODE_PATH

nano ~/.bash_profile
export NODE_PATH=`npm root -g`
source ~/.bash_profile

Now install npm modules again and check whether that is being installed on the path npm root -g

2
  • This! is my issue when using NVM and the repl's require to have it look for global packages on linux/fedora
    – Ray Foss
    Jun 26, 2019 at 19:19
  • 1
    I've got the same problem with NVM.
    – H_I
    Apr 27, 2021 at 10:43
9

For Windows users

Add this to your path: "%AppData%\npm"

1
  • I wish this answer was weighted higher.
    – Shawn
    Oct 18, 2022 at 16:36
4

I do not ever install any npm stuff, via sudo! I have my own reasons, but I just try to keep things simple, and user based, since this is a user development world, and not everyone has root access, and root/sudo installing things like this just seems to clutter up things to begin with. After all, all developers should be able to follow these instructions, not just privileged sudo users.

This particular system is a RHEL7 accessed via SSH:

Frequently one needs various versions of node, so I use NVM https://github.com/creationix/nvm

So with that said, I can show you a working example for -g global installs, using NVM, NPM, and node paths not using root.

set your prefix for .npm-packages if it isn't already. (note, thats a hyphen, not an underscore)

nvm config ls
prefix = "/home/<yourusername>/.npm-packages"

Then adjust your ~/.bash_profile or .bashrc if you prefer readup on why and which here, with the following information.

#PATH EXPORTS
NODE_MODULES=$HOME/.npm                                          
NPM_PACKAGES=$HOME/.npm-packages/bin                           
export PATH=$PATH:$HOME/bin:$NODE_MODULES:$NPM_PACKAGES         

#NVM ENABLE                                                 
export NVM_DIR="$HOME/.nvm"                                   
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm 

That pretty much covers all paths. For e.g., if you install gulp like this npm install -g gulp it symlinks in ~/.npm-packages/bin (note thats a hyphen, not an underscore). (no need for gulp-cli, or gulp-cl)

You can pretty much replace/comment-out all other node path exports. You can put this path info below any other path info you already have, safely, without it overwriting that stuff.

2

Check your global Node module's binary folder, and add it to your $PATH.

npm list -g | head -1

If you use nodenv, the path will change whenever you install a new global node version. Adding a node path like this solves my problem.

"$HOME/.nodenv/versions/$(nodenv global)/bin"

Shortcut for adding the path to zsh

$ echo 'export PATH="$HOME/.nodenv/versions/$(nodenv global)/bin"' >> ~/.zshrc
1
  1. Add the following line to your ~/.bash_profile

    export PATH="$HOME/.npm/bin:$PATH"
    
  2. Load bash profile

    bash -l
    
0

The problem I had was missing the binaries because the user specific .npmrc file in my home directory had bin-links set to false, though the default is true.

Just in case this is your problem check that none of your .npmrc files have it set to false.

Then re-installing all modules will create the binaries at the prefix so your PATH can see them.

0

It may seem like a hack, but setting up yarn when possible saves you a lot of node environment headaches for various unix distros.

0
npm config set prefix /usr/local
sudo chown -R $(whoami) ~/.npm
npm update --force      

works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.