dragonfly dragonfly - 3 days ago 6
Node.js Question

Access globally installed package via require in node

I have a

gulp
based build system. Currently I have some tasks, e.g. a task that is accessing
npm
(
require('npm')
) programmatically. In order to achieve it I need to specify
npm
in my
package.json
dependencies, so that
require
can find it inside
node_modules
. However
npm
is obviously available along with
node
& I also have
npm
installed globally (latest 3.x version of
npm
).

Is there a way to require a global instance of
npm
? I do the same stuff e.g. with other npm packages (I mean I have global package installed but I duplicate it in
package.json
to make it available via
require
).

AJS AJS
Answer

Yes you can do it by adding NODE_PATH to your environment variable eg:

export NODE_PATH=/usr/local/lib/node_modules/

After doing this you node should be able to find globally installed packages as well.

NOTE:your node module path may defer.

to make it permanent you can add the commmand to your ~/.bashrc file

Comments