how to link to installed node packages / modules from another dir

60 views
Skip to first unread message

NodeNinja

unread,
Mar 26, 2016, 1:48:08 AM3/26/16
to nodejs
On linux I installed sqlite3

npm install sqlite3

sqlite3 was installed in the home directory in a folder called "node_modules"

however my server dir is different and from there I am unable to link to sqlite3 unless I copy the "node_modules" folder into my server dir.
Is there a way to link the installed "sqlite3" so that its globally accessibly from any directory?

Thanks in advance.

Ryan Schmidt

unread,
Mar 26, 2016, 3:03:27 PM3/26/16
to nod...@googlegroups.com
Be in the root directory of your project when you run the "npm install sqlite3" command, so that it gets installed in the correct directory.

You are meant to install separate copies of things for each project. You're not meant to link things.

NodeNinja

unread,
Mar 27, 2016, 5:12:28 PM3/27/16
to nodejs


Be in the root directory of your project when you run the "npm install sqlite3" command, so that it gets installed in the correct directory.

You are meant to install separate copies of things for each project. You're not meant to link things.

1. Is this a limitation of node modules or something to do with "sqlite3" ?
2. Why didn't they allow to install once and any or multiple projects to link to that module?
3. Instead of installing the module multiple times can it be installed once and then the "node_modules" folder be copied to the project dir?
Regards
Aeon

Ryan Graham

unread,
Mar 27, 2016, 8:42:19 PM3/27/16
to nodejs
On Sun, Mar 27, 2016, 2:12 PM NodeNinja <aeon...@gmail.com> wrote:
1. Is this a limitation of node modules or something to do with "sqlite3" ?

Not a limitation, just the recommended practice. If your apps all share their dependencies then they must all use the same versions. 

2. Why didn't they allow to install once and any or multiple projects to link to that module?

I'd like to point you at some sort of canonical documentation on this, but I can't actually think of any that fully explains the philosophy. This is definitely a topic that falls into the tribal knowledge category. The most often cited reason is to allow multiple incompatible versions of dependencies to be installed, avoiding the most common cause of "dependency hell".

3. Instead of installing the module multiple times can it be installed once and then the "node_modules" folder be copied to the project dir?

There are ways of doing this, yes, but I'm curious what problem you are trying to solve by doing this.

~Ryan

--

~Ryan

NodeNinja

unread,
Oct 3, 2016, 9:07:41 AM10/3/16
to nodejs


There are ways of doing this, yes, but I'm curious what problem you are trying to solve by doing this.
~Ryan

I many node processes running and each process is running in its own directory.

eg. 
dir1\nodeproc1, dir2\nodeproc2, .... and so on
now each process needs access to SQLite and MySQL so for noderproc1 I installed sqlite and mysql in dir1\nodeproc1 folder and it made a node_modules folder inside that dir.
Now  what  I do for nodeproc2, nodeproc3 and so on do I need to run 

npm install sqlite3
npm install mysql

I need to run the above commands again in the respective folders??
Can't I install both the modules once and reuse it across the node processes running?

Ryan Graham

unread,
Oct 4, 2016, 1:22:57 AM10/4/16
to nodejs

Dependencies are loaded relative to the location of the code, not your current working directory. If you have code in foo/dir1/proc1.js and code in foo/dir2/proc2.js, they will both see modules in foo/node_modules regardless of which directory you run them from (eg. `cd /bar/dir3; node /foo/dir1/proc1.js`). Does that help?

~Ryan


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/fa1e0949-4926-4c8f-b33f-1bfdd326b3e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

~Ryan

Zlatko

unread,
Oct 6, 2016, 10:41:41 AM10/6/16
to nodejs
As Ryan said, you can install the "shared" modules at a common parent directory. So if you have /dir1/app1 and /dir2/app2, you could have a /node_modules folder with those shared directories.

However, it seems like a bad practice, updating one app might break the other. Are you trying to save disk space with this or reduce install time or something else entirely?
Reply all
Reply to author
Forward
0 new messages