133

I was attempting to upgrade phonegap via npm when I started running into trouble. Long story short, there are two node_modules directories on my computer.

/usr/local/lib/node_modules
/usr/local/share/npm/lib/node_modules

When I run npm upgrade -g phonegap, it appears that npm updates the copy of the package that resides in /usr/local/lib/node_modules. However, if I which phonegap I find that the symlink points to the older installation at /usr/local/share/npm/lib/node_modules.

Also, when I attempt to install a stand alone package such as express, the files are installed in the /usr/local/lib/node_modules directory, but no symlink to the executable is created in anywhere in my $PATH.

Two questions:

  • Which is the proper directory for node modules on Mac OS X?
  • How can I configure npm to link executables in my $PATH when it installs software?

Bonus points: Does the method of installing node affect the configuration? There are a lot of options.


EDIT: I figured out that symlinks were being created in my /usr/local/bin, but my .bash_profile was setup with /usr/local/share/npm/bin ahead of /usr/local/bin in my $PATH. I vaguely remember adding that path to my profile at some point, but I'm not sure why.

So the question now becomes: how did I end up with two different node_modules directories on my computer and why would I want to have my node_modules in the share/npm/lib subdirectory instead of right in /usr/local/lib?

Ben Harold
  • 6,242
  • 6
  • 47
  • 71

7 Answers7

142

/usr/local/lib/node_modules is the correct directory for globally installed node modules.

/usr/local/share/npm/lib/node_modules makes no sense to me. One issue here is that you're confused because there are two directories called node_modules:

/usr/local/lib/node_modules
/usr/local/lib/node_modules/npm/node_modules

The latter seems to be node modules that came with Node, e.g., lodash, when the former is Node modules that I installed using npm.

Thomas David Kehoe
  • 10,040
  • 14
  • 61
  • 100
  • 1
    What is the difference between this two nodes? For OS X users is recommended when installing to use the one from node. Should it be better to move to a proper installation of npm? – alicia Mar 21 '17 at 17:35
  • 3
    Installed npm2 from macports, your paths are absent in those locations, also as absent in /opt/local/share.. – holms Jul 04 '17 at 21:50
142
npm root -g

to check the npm_modules global location

Govind Rai
  • 14,406
  • 9
  • 72
  • 83
lazyTank
  • 1,783
  • 1
  • 8
  • 11
  • 1
    The nicest and easiest way to find the global path of node_modules. – Felix Htoo Jul 22 '21 at 12:03
  • This should be the selected answer. Concise and accurate and works under all circumstances i.e. nvm or without nvm + multiple node versions installed. – Gautam May 22 '23 at 07:43
104

Second Thomas David Kehoe, with the following caveat --

If you are using node version manager (nvm), your global node modules will be stored under whatever version of node you are using at the time you saved the module.

So ~/.nvm/versions/node/{version}/lib/node_modules/.

ginna
  • 1,723
  • 1
  • 16
  • 16
16

Find the current path for current active npm installation:

npm root -g

OR try one of these popular defaults:

Linux:

/usr/local/lib/node_modules

MacOS (installed through brew):

/opt/homebrew/lib/node_modules

Linux (probably macos also) when installed with nvm:

~/.nvm/versions/node/{version}/lib/node_modules/

Windows (bonus )

C:\Program Files\nodejs\node_modules\

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
14

If you want to know the location of you NPM packages, you should:

which npm // locate a program file in the user's path SEE man which
// OUTPUT SAMPLE
/usr/local/bin/npm
la /usr/local/bin/npm // la: aliased to ls -lAh SEE which la THEN man ls
lrwxr-xr-x  1 t04435  admin    46B 18 Sep 10:37 /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js

So given that npm is a NODE package itself, it is installed in the same location as other packages(EUREKA). So to confirm you should cd into node_modules and list the directory.

cd /usr/local/lib/node_modules/
ls
#SAMPLE OUTPUT
@angular npm .... all global npm packages installed

OR

npm root -g

As per @anthonygore 's comment

T04435
  • 12,507
  • 5
  • 54
  • 54
  • 9
    Easier: npm root -g – anthonygore Jan 22 '19 at 10:54
  • 1
    for me, `npm root -g` and `which npm` didn't give the same results. I wanted to uninstall `expo-cli` and I succeeded by removing `expo` and `expo-cli` directories from `/usr/local/bin/npm`. Thank You @T04435 ! – Aleksandar Jul 10 '19 at 15:41
1

use this command: npm -t -> to find the path to your global npm package.

if you are using nvm (node version manager package). Then your path may look something like this /Users/yourName/.nvm/versions/node/v14.15.3/lib/node_modules/npm

Harsh Boricha
  • 93
  • 1
  • 7
0

None of the other solutions worked for me and I am on Mac OS (Catalina currently). What I wanted was a portable experience like Go gives me, where I can specify where I want global modules/packages to be installed. Particularly, I wanted the packages to be installed in my user/home directory.

First, I ran this to set the global prefix for NPM:

npm -g set prefix $HOME/.npm-global

Then, I added the bin to my PATH (inside $HOME/.bash_profile:

export PATH=$HOME/.npm-global/bin:$PATH

Now, when I install a global package like the Angular CLI, I can be sure I'm referencing the proper location with this command:

which ng

Or via NPM:

npm -g list

Hope it helps.

GoForth
  • 573
  • 7
  • 10