5

I have an application with the following structure:

-MyApp
  -backend
    -app
      +mongoose-models
    +node_modules
    index.js
    package.json
  -admin
    +(lots of stuff)
  -frontend
    -app
      +app_controllers
    +node_modules
    +public
    +view
    index.js
    package.json

The "backend" folder is the node application for the "admin".
The "admin" is the public folder for the administration of the application, and "frontend" is the website the visitors will see.
What i'm trying to do is to access the mongoose schemas/models that are inside the "backend" folder from the controllers on "frontend/app/controller". I tried something like this but it didn't solve my problem.

Any help will be appreciated.

Anderson Ivan Witzke
  • 1,537
  • 1
  • 15
  • 24
  • Inside your frontend controller, can't you just do `var yourModel = require('../../../backend/app/mongoose-models/yourModel')` ? Bigger question: why setup your application that way? – Mikey Sep 03 '17 at 22:08
  • 1
    It was going to be like this because we wanted to have the main app and the admin app in the same project, but because of this being more difficult to maintain, we ended up splitting the projects... The `require` you sugested wouldn't work because the model would need to be running in a different app instance... (I've tried that already :v) – Anderson Ivan Witzke Sep 04 '17 at 15:18

2 Answers2

3

Alternatively, You can setup a monorepo using Lerna

You can have a project setup like this:

- MyApp:
  - models
    - ...
    - package.json
  - frontend
    - ...
    - package.json
  - backend
    - ...
    - package.json

And anywhere within frontend or backend, you can simply import your modules like

import {...} from '@myapp/models'
Krimson
  • 7,386
  • 11
  • 60
  • 97
2

I had the same challenge, deliberately wanting to split the code-base of two applications but sharing one schema in the database.

The problem with requiring the model from another application's path is in the line:

require('mongoose');

Require will use the mongoose in node_modules of the other application and in my case this was not even the same version. It will not throw any errors, but any query/update/save never executes the callback.

I solved this by force requiring the mongoose module in the application's path.

Right at the start of your application, add this to set the application's base directory:

global.__basedir = __dirname;

I know that using globals is frowned upon but I think this makes for a good exception.

Then in the model, require mongoose like this:

var mongoose = require(global.__basedir + '/node_modules/mongoose');

An alternative for global.__basedir could be to set the application's path directory in the environment and use it like this:

var mongoose = require(process.env.appdir + '/node_modules/mongoose');
Bo.
  • 31
  • 5
  • thanks for the answer, but since has been a long time, as I said in another comment, we ended splitting up the project... and now the project has also been abandoned.. lol I'll keep this anyway and maybe someday someone might need this... – Anderson Ivan Witzke Jun 01 '18 at 21:15