Open In App

Error: Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

It looks like you’re trying to use a method called findOne but it doesn’t exist. This error message suggests that there is a circular dependency in your code, which means that two or more modules are trying to import each other. This can cause problems because the modules can’t be resolved properly. This error indicates that there is a problem with the way your code is organized and that it is causing a circular dependency.

Description (circular dependency): To fix this error, you’ll need to refactor your code to remove the circular dependency. This might involve reorganizing your code or splitting it up into smaller modules. It’s also a good idea to check that all of your modules are importing and exporting the right functions and variables.

Error Example: 

 

Javascript




const A = require('./moduleA');
const B = require('./moduleB');
  
A.findOne();


Output:

Error Output

Solution: There are several approaches to solving this error:

  1. Reorganizing the code to break the circular dependency
  2. Using a third module to break the circular dependency
  3. Removing the dependency altogether

Example 1: Reorganized code to break the circular dependency:

  • moduleA.js

Javascript




const B = require('./moduleB');
  
module.exports = {
    findOne: () => {
        console.log('Finding one from A');
        B.findTwo();
    }
};


  • moduleB.js

Javascript




const A = require('./moduleA');
  
module.exports = {
    findTwo: () => {
        console.log('Finding two from B');
    }
};


  • main.js

Javascript




const A = require('./moduleA');
  
A.findOne();


Output: 

Finding one from A
Finding two from B

Example 2: One approach to solve the circular dependency issue is to use a third module to break the dependency. This involves creating a new module, module C, that is required by both module A and module B. This allows module A and module B to access the functions in module C without creating a circular dependency.

  • moduleA.js

Javascript




const C = require('./moduleC');
  
module.exports = {
    findOne: () => {
        console.log('Finding one from A');
        C.findTwo();
    }
};


  • moduleB.js

Javascript




const C = require('./moduleC');
  
module.exports = {
    findTwo: () => {
        console.log('Finding two from B');
        C.findOne();
    }
};


  • moduleC.js

Javascript




module.exports = {
    findOne: () => {
        console.log('Finding one from C');
    },
    findTwo: () => {
        console.log('Finding two from C');
    }
};


Output:

Finding one from A
Finding two from C
Finding one from C

Example 3: Another approach to solving the circular dependency issue is to remove the dependency altogether. This can be done by refactoring the code to not require the functions from the other module.

  • moduleA.js

Javascript




module.exports = {
    findOne: () => {
        console.log('Finding one from A');
    }
};


  • moduleB.js

Javascript




module.exports = {
    findTwo: () => {
        console.log('Finding two from B');
    }
};


  • main.js

Javascript




const A = require('./moduleA');
const B = require('./moduleB');
  
A.findOne();
B.findTwo();


Output:

Finding one from A
Finding two from B

Conclusion: All the above method is equally valid, and you can choose the one that works best for your project



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads