Possible to select globally installed module over locally install one?

51 views
Skip to first unread message

Bgsosh

unread,
Sep 10, 2015, 9:56:22 AM9/10/15
to nodejs
When there is both a globally locally installed version of a module, does anyone know if it is possible to require() the global version, over the local one?

Thanks,
S

Sam Roberts

unread,
Sep 12, 2015, 6:55:56 PM9/12/15
to nod...@googlegroups.com
Global modules are misnamed, they are only scripts that you intend to
run, they are NEVER requireable.

Zlatko

unread,
Sep 13, 2015, 11:13:50 AM9/13/15
to nodejs
What do you mean, they're not requireable?

    [zlatko@zlatko-mint /tmp]$ node -e "require('express')"
    module.js:338
        throw err;
          ^
    Error: Cannot find module 'express'
    [...cut...]

    [zlatko@zlatko-mint /tmp]$ npm i -G express
    \
    [...cut...]

    [zlatko@zlatko-mint /tmp]$ node -e "require('express')"
    [zlatko@zlatko-mint /tmp]$ 


For the OP, it's possible to do it manually, ie: `require('/home/zlatko/.nvm/versions/node/v4.0.0/lib/node_modules/gulp');` but it's not very scalable.

The problem is - when I bump my node version to, say, 4.0.1, I'd have to fix the path. Also, if it were you, you would not have a `/home/zlatko` part, but possibly /usr/lib/node_modules or similar.


I don't know if there's a node config option for something like that though, probably there isn't.

Zlatko

Ryan Schmidt

unread,
Sep 13, 2015, 2:35:58 PM9/13/15
to nod...@googlegroups.com
On Sep 13, 2015, at 03:21, Zlatko wrote:
>
> What do you mean, they're not requireable?

Probably means that globally installed modules are not intended to be used in a require statement, and users should not attempt to find a way to do so.

Sam Roberts

unread,
Sep 13, 2015, 6:48:18 PM9/13/15
to nod...@googlegroups.com
On Sun, Sep 13, 2015 at 1:21 AM, Zlatko <zlad...@gmail.com> wrote:
> What do you mean, they're not requireable?

They aren't.

> [zlatko@zlatko-mint /tmp]$ npm i -G express
> \
> [...cut...]

You misspelled -g as -G, npm ignored you, and installed it locally. :-)

Do an "ls node_modules/express", and you will see.
Reply all
Reply to author
Forward
0 new messages