0
votes

I have a 2 projects which requires each others.

The root-app is run by node. It imports child-app/index.js.

The child-app has a dependency on root-app as well. It needs to import Component.js from it. child-app is never run independently of root-app, it is always imported by the root-app.

root-app:
  /Component.js
  /index.js
  /package.json

child-app:
  /index.js
  /package.json

So basically the root-app requires the child-app and the child-app requires a component from the root-app.

root-app/index.js:

import ChildApp from 'child-app';

child-app/index.js

import Component from 'root-app/Component';

At the moment, my package.json files look like this:

root-app/package.json

{
  "name": "root-app",
  "dependencies": {
    "child-app": "file:../child-app"
  }
}

child-app/package.json

{
  "name": "child-app",
  "peerDependencies": {
    "root-app": "file:../root-app"
  }
}

This of course doesn't work and I get an error:

Unable to resolve module 'root-app' from 'root-app/node_modules/child-app/index.js'

So my question, is there any way to have this kind of dependencies working without the need of creating a third project that will work as a "bridge" between those two projects?

Thanks!

1
The concept of modules is to reduce complexity by delegating certain functionality to reusable dependencies. If the dependencies have to be aware of their dependents, it defeats the purpose of encapsulation. In order for these to both be truly modules, I think they should be unaware of each other and a new module should combine what they export.csander

1 Answers

0
votes

You have "name":"root-app" in child-app/package.json. But still, it's better to avoid circular dependencies between packages. You might take a look at the dependency inversion principle to get an idea how to do so.