Where should my npm modules be installed on Mac OS X? - node.js

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?

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

npm root -g
to check the npm_modules global location

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

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

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\

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

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.

Related

Global Node Packages Installed to Wrong Directory

I'm trying to install Hexo globally using npm. When I run
npm install -g hexo-cli
I'm informed that it was installed to /Users/myusername/.node/bin/hexo -> /Users/myusername/.node/lib/node_modules/hexo-cli/bin/hexo
The problem comes in when I run hexo init blog and the hexo command is not found.
I installed Node and npm with Homebrew, so when I run which node and which npm, the results are /usr/local/bin/node and /usr/local/bin/npm respectively.
I'm thinking that I still have leftover files and directories from when I installed Node without homebrew, but I don't want to start deleting things without fully knowing the repercussions. Would I be safe to delete all files located in the /Users/myusername/.node/ directory? I can't figure out why npm is not installing to the proper directory.
After a little digging, I found that my npm prefix variable was pointing do the wrong directory, left behind by the old Node installation. I ran npm config get prefix to see where it was pointing.
I set the new prefix value using npm config set prefix /usr/local. Homebrew is symlinked with this directory via /usr/local/bin. I uninstalled hexo-cli and reinstalled through npm, and now it works perfectly.
For NVM users
Run nvm use --delete-prefix v10.13.0 --silent replacing v10.13.0 with whatever version of node you're using.
A simple way to cope with environment variables/path problems on Windows:
Run command:
npm install -g hexo
Using node.js command prompt rather than cmd windows provided by Windows itself.

Where does npm store node modules?

I just setup official node.js on windows which includes npm in custom directory d:\myserver\nodejs
I tested npm with
npm install less
it works but I can't see any less directory in node_modules\npm\node_modules subdirectory.
Where could I find it (I guess the name of less module is actually less).
under : node_modules
just check where you where pointer "in what directory were you" at the time of the installation
d:\myserver\nodejs -> node_modules
Just for further clarification, google brought me here while looking up where global modules are stored (installed via npm install -g ...).
From the documentation
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you’re going to require() it.
Install it globally if you’re going to run it on the command line.
If you need both, then install it in both places, or use npm link.

npm installs all modules in /usr/local/lib/node_modules/

I have node.js 0.8.14 installed on Ubuntu 12.10. I created a directory in my home directory with a sub directory node_modules. I want to install some local node modules there but running
npm install myModule
in this directory installs this module in /usr/local/lib/node_modules/ (same behavior as installing the module with the -g flag
There is no node path in .bashrc.
Any idea how I can install local node modules?
After some further research I found the solution.
Running the command npm config ls revealed that the default config global=false (you see the default config with npm config ls -l) was overwritten by global=true in /home/vsdev/.npmrc and /usr/local/etc/npmrc.
Reverting this to global=false solved the issue.
That is odd.
FYI you don't need to create the node_modules directory, npm will do that for you
npm normally just installs to the current directory. Even if the package you are installing is configured to prefer global installation, npm will install it locally unless you explicitly pass the -g parameter.
can you run the following shell commands and confirm npm is really the real npm?
which npm
alias | grep npm
npm install load all in node_modules then it might be version 3 behaviour http://blog.npmjs.org/post/110924823920/npm-weekly-5 or as mentioned by #vsdev so once you make sure it version 3 behaviour and u want to go with it then its fine else follow below
1- uninstall all modules.. into the node_modules folder in your project then execute: npm uninstall *
2- Tell npm to install with legacy bundling for this one install:
npm install --legacy-bundling
A "permanent" alternative:
Set your npm config to always use legacy bundling...
npm set legacy-bundling=true
.. and run as usual:
npm install
*fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.

Node Version Manager (NVM) npm installing modules to common folder

I've installed NVM for node.js using the instructions from this post:
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
When I switch between node versions and then use npm to install a module, all the modules are placed in the same 'node_modules' folder (~/node_modules/) instead of in the 'node_modules' directory specific to that version of node?
Any idea on how to remedy this?
Based on the comments from https://github.com/creationix/nvm/pull/97:
When installing packages with npm using the global switch -g the
package ends up in the proper directory (i.e.
.nvm/$VERSION/lib/node_modules), however node is unable to require it
since it somehow isn't searching on it's prefix.
So using npm install -g xxxxx will put the modules in the correct location for NVM but if you try to require one of them node can't find the module. I am still playing around with this and will update if I find a solution.
Update
Where does NPM put node_modules? (see https://docs.npmjs.com/files/folders)
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
If you need both, then install it in both places, or use npm link.
So what I did was run npm init (see http://npmjs.org/doc/init.html) in my projects root dir which generated package.json. Now when I run npm install xxxxx it creates a node_modules dir in my project folder (which I add to my .gitignore). This works for modules that I require in my code.
For commands such as CoffeeScript I install with npm install -g coffee-script which puts it in the correct directory (.nvm/$VERSION/lib/node_modules). While I can't require these modules (npm link should solve this problem) I can run the commands - i.e. coffee.
I just installed express globally (-g) and was having problem when require("express"). Just like Jesse Vogt said I just reinstalled express but this time without the -g just like this: "sudo npm install express" and now is working perfectly!
For latest nvm window version 1.1.7.
Package was installed and placed into the respective nodejs version.
nvm use 16.8.0
npm install truffle
nvm use 16.7.0
npm install mysql

How to include the coffee-script module in node using Cygwin on Windows

I'm trying to get the functionality of CoffeeScript.compile in node.js.
I've installed node on Cygwin in Windows, and installed coffee script with npm.
I can use the coffee command fine but if I try to
require("coffee-script");
I get "Cannot find module 'coffee-script'" in node.
Am I going about this the wrong way?
It sounds like require isn't looking in npm's global install path. Run
require.paths
from the Node REPL to see which paths are being looked in. On the command line, run
npm ls -g
to see the directory that npm is installing global libraries in (it's /usr/local/lib on my Mac). Add /node_modules to that, and add it to require.paths. You can do this on a one-time basis by running
require.paths.shift('/usr/local/lib/node_modules');
(Update: Modifying require.paths is no longer allowed as of Node 0.5+.)
or you can do it permanently by adding the line
export NODE_PATH=/usr/local/lib/node_modules
to your ~/.bashrc file.
Are you using from a different directory? If so, install it globally with the -g flag.
(npm install coffee-script -g).

Resources