Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Npm link and shared module instances

So I am using npm link to develop multiple modules at once but it is causing me issues with mongoose connection because it is not using the same instance.

If I literally copy my 2nd module into my main app's node_modules folder the connections work as expected, same if I install through github, the problem is if I do an npm link the 2nd module has its own instance of mongoose instead of using the existing connection in the main app.

Does anyone know any workarounds for this?

like image 762
jonnie Avatar asked Nov 10 '22 03:11

jonnie


1 Answers

So Not the perfect solution but what I realised was that since node will always check the parent directory's node_modules folder if not present in the current directory's node_modules I put all my modules into the same working directory and npm installed the mongoose and any other shared instance modules in that directory.

It does Work fine but requires the manual steps of npm installing on the parent directory and deleting from within the modules node_modules folder

see below for structure that I used

|---workspace
      |-----node_modules -> install mongoose or other shared instance modules
      |-----mainApp
               |------node_modules -> delete mongoose or other shared instance modules
      |-----moduleOne
               |------node_modules -> delete mongoose or other shared instance modules
      |-----moduleTwo
               |------node_modules -> delete mongoose or other shared instance modules
like image 147
jonnie Avatar answered Nov 15 '22 06:11

jonnie