Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing non-existent property of module.exports inside circular dependency NodeJS

Im having some issues when using module.exports inside NodeJS, and I've followed multiple guides, and im almost certain Im doing it right. I have to scripts, main.js and event.js. Im trying to share a function from main.js to event.js, but its not working. Here is the code:

Main.js

function Scan(){
    if(fs.readdirSync('./events/').length === 0){
        console.log(colors.yellow('Events Folder Empty, Skipping Scan'))
    } else {
        var events = fs.readdirSync('./events/').filter(file => file.endsWith('.json'))
                for(const file of events){
                    let rawdata = fs.readFileSync('./events/' + file);
                    let cJSON = JSON.parse(rawdata);
                }
                events.sort()
                tevent = events[0]
                StartAlerter()
                }
}

module.exports = { Scan };

Event.js

const main = require('../main')

main.Scan;

This returns the error:

(node:19292) Warning: Accessing non-existent property 'Scan' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

What am I doing wrong?

like image 232
gjoe Avatar asked Aug 31 '25 04:08

gjoe


1 Answers

I discovered that the arrangement had no effect in the error.

I simply changed the way I exported the function from the Main.js

from:

module.exports = { Scan };

to:

exports.Scan = Scan

And in Event.js, I was able to access the file like this

const main = require("./Main.js");
let result = main.Scan();

This solved my problem, I hope it helps another developer 😎

like image 199
Joel Avatar answered Sep 02 '25 18:09

Joel