I'm writing an express app in ES6 and the file structure I am using is grouping files by functionality. Each module has an index.js
which is responsible for exporting any relevant methods, classes, etc. Other sibling modules only require the index.js
from another module.
However I have run into an issue with circular dependencies. For instance I have a User
module that exports:
export routes from './routes';
export default from './User'; //model
export helpers from './helpers';
Then I have another model Session
that does something similar:
export routes from './routes';
export helpers from './helpers';
The issue is that session/routes
uses something from user/helpers
and user/routes
uses something from session/helpers
. The importing in session/routers
is import {helpers} from '../user
. And the import in user/routers
is import {helpers} from '../session'
This results in a circular dependency between the two modules via their respective index.js
files.
This causes problems because sometimes when I import the package in some third module, it is empty on the first tick.
Other than the obvious solution of not including routes
as an export in both module's index.js
, is there another solution to dealing with this?