Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centralised node_modules

I would like to have my Node modules stored in a centralised place, in say, /var/http/common/ and have my app live/run in a different location, say /var/http/www/apps/APP#1_NAME/.

I was able to set up the requires in server.js to use relative paths like require('../../../common/express'), but from reading posts by NPM's author, it sounds like I'm hacking it, and I should use npm link to create a "local" reference for Node (which symlinks to the real installation).

I first installed my node modules in /var/http/common/, but then when I tried to create the symlink (npm link ../../../common/node_modules/express), npm seems to have treated it like a "global" install, and re-installed express in /usr/local/lib/node_modules/express (and created a "local" symlink to it ./node_modules/express ->) which is not what I expected to happen. Is this what I actually want? Should I have used npm config set prefix first?

like image 864
Jakob Jingleheimer Avatar asked Dec 13 '12 17:12

Jakob Jingleheimer


2 Answers

Turns out: I should have set npm config set prefix before doing anything else.

It might appear that npm link and npm install -g do the same thing; however whilst, npm link will install the module globally, it also creates a local symbolic link in node_modules pointing to $prefix/lib/node_modules/$module. MaxGfeller is incorrect: Without that local symlink, Node will complain that it cannot find the (globally installed) modules. This is determined thru my own attempts as well as is inferred from npm help folders:

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

This doesn't specifically address what I asked: I want to have the modules stored in a central location (to be accessed by multiple Node Applications) but I don't care about using them from the command like—I only want to use them in require('').

As for my question about using relative paths in require(''), I still have not got/found an authoritative answer for that, but it would seem from the existance of npm link that using rel paths is not the author's intent. To me it seems like a case of six-of-one, but I would like to remain consistent with Node's standard.

like image 136
Jakob Jingleheimer Avatar answered Oct 12 '22 17:10

Jakob Jingleheimer


You can install your node modules globally using by adding '-g' to the command. For example:

npm install express -g

In your code you can use it normally:

require('express');

The modules are then stored in /usr/local/lib/node_modules

like image 31
MaxGfeller Avatar answered Oct 12 '22 18:10

MaxGfeller