Node js. Problems with connecting modules

Introduction

A very unusual error, related, as it seems to me, to the cycles between module calls (because otherwise I can not even guess what). Not infrequently in programming, when I had to divide a program into modules, I encountered the problem of module loops (when modules call each other directly or through a chain of other modules). But with Node js, as it seemed to me, there is no such problem, so I did not follow it, but now this seems to be a problem, although I can't track it.

Main part

I have a module that is responsible for small functions like "Say on behalf of the server" and it is obviously involved in a large number of other modules. moduleFunctions.js

And there is an eating module that stores functions that initiate the main functions of the program itself moduleCommands.js. (And it also uses moduleFunctions.js)

Everything worked fine, until I needed to call the module moduleCommands.js inside moduleFunctions.js. And after that, the program stopped seeing exports from moduleFunctions.js in general, in the third module (with what exactly in it, in the rest he sees).

I couldn't figure out what the problem was for a long time, until I cut off the import moduleCommands.js inside moduleFunctions.js and everything worked again.

The project is still very small, here is a link to the github, to easily see the link between the modules and the code itself: https://github.com/VovaParamonov/webbot

Also, the code consists of two "main participants of actions" (modules):

moduleFunctions.js

const clc = require('cli-color');
const { commands } = require("./moduleCommands");

module.exports.serverSay = function serverSay(text) {
    console.log(`[SERVER]: ${text}`);
};

module.exports.serverErr = function serverErr(text) {
    console.log(clc.red(`[SERVER ERROR]: ${text}`))
};

module.exports.serverCommandsList = function serverCommandsList () {
    Object.keys(commands).forEach(commandName => console.log(commandName));
};

moduleCommands.js

const cheerio = require("cheerio");
const { createRequest } = require("./moduleRequests");
const { serverSay } = require("./moduleFunctions");


module.exports.commands = {
    "omgtuInformation" : function omgtuInformation() {
        createRequest("https://omgtu.ru/", (body, status) => {
            const $ = cheerio.load(body);

            serverSay(`New information from site: ${$("#menu_banner2").text()}`);
        })
    }
};

P.S. I understand that many people will write about problems in the architecture, since I needed such cyclic connections between modules and it is enough for me to fix it, but I need to understand how this is exactly the problem, so as not to face it in the future. In google, I didn't find anything similar with node js module loops, and I don't know which way to dig anymore.

Thank you

Author: Grundy, 2020-04-12

1 answers

In the Node.js module documentation, there is a separate section about cyclic dependencies. The essence of the problem is that require returns a reference to the "unfinished" module when a loop is detected, and finally determines all exports only after all imports are completed. The error occurs when trying to destructure this link in an incomplete state.

 0
Author: Spatz, 2020-04-13 06:26:59