Import module from the same directory which includes the current module not working

Viewed 238

I have this directory structure:

start-dir
  example-dir1
    module1.js
    module2.js
    index.js
  ...other-directories...
  example-dir2
    module3.js
    module4.js
    index.js
...other-directories...

The index.js file imports module1 and 2 and exports them as objects:

exports.module1 = require('./module1');
exports.module2 = require('./module2');

Then in module1.js I want to include module2.js - using the index.js like this:

// module1.js

const { module2 } = require('@example-dir1');

console.log(module2) // undefined

Note: the @ is absolute path for the root directory, this is using web pack alias, but without the alias it also won't work - even when using "direct" import, like this: require('./index')

Module 1 not only needs to import module 2 from the same example-dir1, it sometimes needs to import other modules from other directories, for example:

// module1.js

const { module3 } = require('@example-dir2');

console.log(module3); // undefined

But if I try to do the same thing but on directories which are not childs of the start-dir (notice that this is the root directory of the example), it will work.

So the problem is when trying to import a module which imports and exports the current module in it

How can it be fixed?

0 Answers
Related