Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set node.js REPL module paths

Tags:

I can't figure out how to add paths to my Node.js installation (v.0.4.7 on Mac OS X). I've installed npm, and it installs globally to /usr/local/lib/node_modules. However, when I installed npm, it didn't notify Node about where it chose to put global modules (should it have?). Now when I use Node in command-line mode, I can't simply require() my globally-installed modules. So, I'm wondering if there is some kind of Node configuration file or environment variable where I can add my global npm module installation path?

I know that I can just add it to the require.paths array when I'm in Node's command line, but I want to add this global module folder once and for all, so that Node will always search that directory for modules when I require() them from the command line. Thanks in advance for any help and pointers about making npm and Node co-exist!

like image 202
ampersand Avatar asked May 05 '11 05:05

ampersand


2 Answers

OK, I got it. Combining info from http://nodejs.org/docs/v0.4.7/api/modules.html#file_Modules and https://github.com/isaacs/npm/blob/master/doc/faq.md#readme it is clear that Node checks the NODE_PATH environment variable when checking for modules. To set this I did the following:

echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc

This sets NODE_PATH to npm's global install folder.

like image 148
ampersand Avatar answered Oct 21 '22 02:10

ampersand


Damn, I misunderstood. Sorry about that.

Back in topic, you can put these two lines in set-repl-paths.js

require.paths.unshift('/usr/lib/node_modules');
require("repl").start();

Then executing node set-repl-paths.js you will have a repl with the paths already setted. You can write a simple bash script or set a shell alias so you can just type node-repl or something similar.

With npm 1.x you should use local installation, and leave global installation for modules that provide command line utilities.

If you really want global install for module foo, then in your module folder issue a npm link foo. Now you can require("foo") in your module.

Best practice is to use local installation.

See npm 1.0: Global vs Local installation on the nodejs blog.

like image 37
Giacomo Avatar answered Oct 21 '22 02:10

Giacomo