{"id":579,"hash":"9698a73331e4aba1599c582be3dfe799493799dc93635c4a4ac4242be1472791","pattern":"Webpack import returns undefined, depending on the order of imports","full_message":"I'm using webpack + babel. I have three modules looking like this:\n\n// A.js\n\n// some other imports here\nconsole.log('A');\nexport default 'some-const';\n\n// B.js\n\nimport someConst from './A';\nconsole.log('B', someConst);\nexport default 'something-else';\n\n// main.js\n\nimport someConst from './A';\nimport somethingElse from './B';\nconsole.log('main', someConst);\n\nWhen main.js is executed, I see the following:\n\nB undefined\nA\nmain some-const\n\nIf I swap the imports in main.js, B becoming the first, I get:\n\nA\nB some-const\nmain some-const\n\nHow come B.js gets undefined instead of a module in the first version? What's wrong?","ecosystem":"npm","package_name":"webpack","package_version":null,"solution":"After almost a full workday of narrowing down the issue (AKA hair-pulling), I've finally came to realize that I have a circular dependency.\n\nWhere it says // some other imports here, A imports another module C, which, in turn, imports B. A gets imported first in main.js, so B ends up being the last link in the \"circle\", and Webpack (or any CommonJS-like environment, for that matter, like Node) just short-circuits it by returning A's module.exports, which is still undefined. Eventually, it becomes equal to some-const, but the synchronous code in B ends up dealing with undefined instead.\n\nEliminating the circular dependency, by moving out the code that C depends on out of B, has resolved the issue. Wish Webpack would somehow warn me about this.\n\nEdit: On the last note, as pointed out by @cookie, there's a plugin for circular dependency detection, if you'd like to avoid hitting this problem [again].","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/35240716/webpack-import-returns-undefined-depending-on-the-order-of-imports","votes":94,"created_at":"2026-04-19T04:51:22.578491+00:00","updated_at":"2026-04-19T04:51:22.578491+00:00"}