G
G
Galdar Turin2021-02-21 16:47:05
Node.js
Galdar Turin, 2021-02-21 16:47:05

How, who, implements the connection of modules?

Hello everyone, I was interested in such a question, maybe someone will throw off links to interesting articles or share their experience. That's the point. Let's say there is such a module - 1

Module - 1

exports.modul1= function () {
 console.log("New modul1")
}


Also module -2 which will call module 1
Unit 2

const m = require('./modul1');
m.modul1()
exports.modul2= function () {
 console.log("New modul2")
}


Well, 3, which will call module 1
Module -3

const m = require('./modul1');
m.modul1()
exports.modul3= function () {

 console.log("New modul3")
}


It turns out that two instances of the 1st module will be created in two different modules, so if there was a connection to MySQL in module 1, then 2 different connections would be created. If so, how can this be avoided. The only way that comes to my mind is with some such module
main

const m1 = require('./modul1');
    const m2 = require('./modul2');
    const m3 = require('./modul2');

    m2.modul2( m1 )
    m3.modul3( m1 )


Like declare everything in one file, call everything there and scatter variables, but isn't there really only such an option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2021-02-22
@GreyCrew

Everything is well described here)
https://medium.com/@lazlojuly/are-node-js-modules-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question