jonnie jonnie - 24 days ago 18
Node.js Question

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?

Answer

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
Comments