Where should my npm modules be installed on Mac OS X?

node.jsMacosNpm

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

/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.

Solution 2 - node.js

npm root -g

to check the npm_modules global location

Solution 3 - node.js

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/.

Solution 4 - node.js

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

Solution 5 - node.js

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\

Solution 6 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen HaroldView Question on Stackoverflow
Solution 1 - node.jsThomas David KehoeView Answer on Stackoverflow
Solution 2 - node.jslazyTankView Answer on Stackoverflow
Solution 3 - node.jsginnaView Answer on Stackoverflow
Solution 4 - node.jsT04435View Answer on Stackoverflow
Solution 5 - node.jsLukas LiesisView Answer on Stackoverflow
Solution 6 - node.jsHarsh BorichaView Answer on Stackoverflow