Skip to content

Instantly share code, notes, and snippets.

@jdwolk
Last active August 19, 2016 00:06
Show Gist options
  • Save jdwolk/2a242f815856682a7371c496ab0004bf to your computer and use it in GitHub Desktop.
Save jdwolk/2a242f815856682a7371c496ab0004bf to your computer and use it in GitHub Desktop.
Webpack Circular Dependencies

Handling Webpack Circular Dependency Issues

The situation:

Utils

// file: utils/index.js

export UtilsA from './utilsA.js';
export UtilsB from './utilsB.js';
// file: utils/utilsA.js

const UtilsA = () => {
 // some stuff
};

export default UtilsA();
// file: utils/utilsB.js

import { ServiceA } from '../services';

const UtilsB = () => {
 // some other stuff
};

export default UtilsB();

Services

// file: services/index.js

export ServiceA from './serviceA.js';
// file: widgets/serviceA.js

import { UtilsA } from '../utils';

const ServiceA = () => {
  // do something with UtilsA,
  // don't even TOUCH UtilsB
};

export default ServiceA();

Explanation

This blows up even though WidgetA isn't using UtilsB directly. Happens because ServiceA is trying to get to UtilsA through utils/index.js, and utils/index.js exports UtilsB which requires ServiceA!

Solution

Use Webpack Cylic Dependency Checker to check for cycles. In modules with cycles, refer to dependencies directly rather than through index.js (I know, I know...it defeats the point of having index.js!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment