Node Version Manager (NVM) npm installing modules to common folder - node.js

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

Related

one dedicated directory for node_modules

I am quite new to npm but I have a question about npm node modules. I have the node modules now installed in several directories. One for Gulp, one for nodeJS etc. I was wondering how I could have one dedicated node modules directory and let the other packages point to that directory, instead of having node modules all over the place.
What is best practise?
Cheers,
Lino
Yes, you can have global node_modules directory accessible to multiple application.
A package is available globally if it is installed with -g option. For instance if body-parser is required globally it should be installed with the following npm command:
npm install body-parser -g
Following are the paths are usage of modules:
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.

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.

Using npm to install packages gives command not found?

Using npm to install packages, I can never use their cli. For instance, install grunt-cli, avn, nvm, etc. and running
npm install -g avn avn-nvm avn-n
avn setup
Results in command "avn" not found. I don't know if this is a pathing issue, but my PATH is:
/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin:/usr/local/sbin:/usr/local/opt/ruby/bin:/usr/local/lib/python2.7/site-packages:/usr/local/share/npm/bin
I am using OSX.
Your path does not include where the node_modules you're installing globally exist. You need to include the bin path for the modules:
export PATH=$PATH:/usr/lib/node_modules/.bin

Homebrew Install Node Location

Kinda 2 questions related to each other here.
When doing a brew install node, should I first navigate to the root of whatever folder I'm going to hold all my future web projects/apps then run it? Or does it not matter where I run the install initially for Node? Because I notice it creates a node_modules folder in /local/lib/node_modules
I assume it doesn't matter, and when you start installing node packages using npm install [package] it'll create a separate node_modules folder under the context you're in so lets say /www/MyApplication run npm install and it'll create /www/MyApplication/node_modules....and that the one under /local/lib/node_modules just serves as the one for npm itself because it needs its own root node_modules folder which is how npm runs?
Correct, it makes no difference where you run brew install node, it will install into your Homebrew folder.
When you use npm to install a Node module, it will install into the current directory, unless you use the -g global flag. Normally you will install modules that are project dependencies into your project folder, and global modules are for global utilities.
For example, to use Grunt you would install the grunt-cli package globally for the command-line utility.
npm install -g grunt-cli
And for each project that uses Grunt you will install a version of the grunt module for use with the project.
npm install grunt

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.

Resources