The Wayback Machine - https://web.archive.org/web/20240812140302/https://www.geeksforgeeks.org/error-warning-accessing-non-existent-property-findone-of-module-exports-inside-circular-dependency/
Open In App

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

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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



Similar Reads

Difference between module.exports and exports in Node.js
The module is a plain JavaScript Object representing the current module. It is local to each module and also it is private. It has exports property which is a plain JavaScript variable, set to module.exports. At the end of the file, Node.js return module.exports to the required function. About module.exports: When we want to export a single class/v
2 min read
How to Fix Missing Dependency Warning When Using useEffect Hook?
When using the useEffect hook in React, you might encounter warnings about missing dependencies. This happens because useEffect expects you to specify all the dependencies that the effect depends on. The React Hooks ESLint plugin will helpfully point out any missing dependencies to ensure your effects run correctly. In this article, we will learn h
3 min read
What is the purpose of module.exports in node.js ?
The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method. Purpose: The main purpose of module.exports is
3 min read
How to write code using module.exports in Node.js ?
module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to use module.exports in Node.js. I will walk you thr
3 min read
Mongoose Query.prototype.findOne() API
The Mongoose Query API findOne() method is used to find a single document, from a collection, using the MongoDB query system. Syntax: Query.prototype.findOne(filter, projection, options) Parameters: It accepts the following 4 parameters as mentioned above and described below: filter: It is a mongoose object which identifies the existing document to
3 min read
Mongoose findOne() Function
The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.  Syntax: Model.findOne(id)Parameters: Model: This is the name of the collection used to retrieve the document corresponding to the provided ID.id: This is the identif
2 min read
How to Perform a findOne Operation in MongoDB using Node.js?
The findOne operation in MongoDB is used to get a single document from the collection if the given query matches the collection record. While using findOne, if more than one record is there with the exact same match, then it will return the very first one. We will use this operation if we need to fetch a specific record, like a user's profile, any
4 min read
Difference Between Default & Named Exports in JavaScript
In JavaScript module facilitates the "export", of variables, functions, or classes from one module to another. This system primarily offers two approaches for exporting, Named Exports and Default Exports. Named exports to allow multiple values to be exported from a module with specific names, while default exports are used for single value or modul
4 min read
What is the Purpose of Exports Array in NgModule?
NgModule is a class marked by the @NgModule decorator. It identifies the module's own components, directives, and pipes, making some of them public, through the exports property, so that external components can use them. The @NgModule decorator takes a metadata object as an argument. This object configures various aspects of the module, such as dec
3 min read
How to Import all Exports of a File as an Object in Node.js ?
In Node.js, you can import all exports of a file as an object using the ES Modules syntax (import) or the CommonJS syntax (require). This approach is useful when you have multiple exports from a module and want to access them conveniently through a single object interface. Below are the approaches to Import all Exports of a File as an Object in Nod
4 min read
three90RightbarBannerImg