Required module does not update when the variable inside that module changes












0














I have a javascript file and in it I require another file that has a variable that changes.



main.js



let num = require("./extra").num


extra.js



let num = 5;

setInterval(() => {
if (num > 0) {
num--;
}
console.log(num);
}, 1000);

module.exports = {
num
};


Now in the main.js file that variable num is always 5 and never changes. What do I need to do to get it updated all the time?



Note: I'm declaring the variable in main.js AFTER the variable changes so it shouldnt't be 5










share|improve this question






















  • In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
    – natn2323
    Nov 21 '18 at 19:20












  • In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
    – Pecius
    Nov 21 '18 at 19:29
















0














I have a javascript file and in it I require another file that has a variable that changes.



main.js



let num = require("./extra").num


extra.js



let num = 5;

setInterval(() => {
if (num > 0) {
num--;
}
console.log(num);
}, 1000);

module.exports = {
num
};


Now in the main.js file that variable num is always 5 and never changes. What do I need to do to get it updated all the time?



Note: I'm declaring the variable in main.js AFTER the variable changes so it shouldnt't be 5










share|improve this question






















  • In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
    – natn2323
    Nov 21 '18 at 19:20












  • In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
    – Pecius
    Nov 21 '18 at 19:29














0












0








0


0





I have a javascript file and in it I require another file that has a variable that changes.



main.js



let num = require("./extra").num


extra.js



let num = 5;

setInterval(() => {
if (num > 0) {
num--;
}
console.log(num);
}, 1000);

module.exports = {
num
};


Now in the main.js file that variable num is always 5 and never changes. What do I need to do to get it updated all the time?



Note: I'm declaring the variable in main.js AFTER the variable changes so it shouldnt't be 5










share|improve this question













I have a javascript file and in it I require another file that has a variable that changes.



main.js



let num = require("./extra").num


extra.js



let num = 5;

setInterval(() => {
if (num > 0) {
num--;
}
console.log(num);
}, 1000);

module.exports = {
num
};


Now in the main.js file that variable num is always 5 and never changes. What do I need to do to get it updated all the time?



Note: I'm declaring the variable in main.js AFTER the variable changes so it shouldnt't be 5







javascript node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:11









PeciusPecius

103




103












  • In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
    – natn2323
    Nov 21 '18 at 19:20












  • In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
    – Pecius
    Nov 21 '18 at 19:29


















  • In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
    – natn2323
    Nov 21 '18 at 19:20












  • In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
    – Pecius
    Nov 21 '18 at 19:29
















In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
– natn2323
Nov 21 '18 at 19:20






In regards to your note, how is the variable changing? With your current code, I believe if you do a new require statement, it'll create a new instance of the num in your extra.js. So the one that was changed before, is no longer the one that is being accessed now.
– natn2323
Nov 21 '18 at 19:20














In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
– Pecius
Nov 21 '18 at 19:29




In setInterval the variable gets decremented. So if it does that, when I require it it shouldnt be the default 5
– Pecius
Nov 21 '18 at 19:29












3 Answers
3






active

oldest

votes


















0














This will work:



in extra.js:



let num = 5;

setInterval(() => {
if (num > 0) {
num--;
}
console.log("in extra: " + num);
}, 1000);

module.exports = {
getNum: function() {
return num;
}
};


in main.js:



let num = require('./extra').getNum();
console.log("initial value is: " + num);

setInterval(() => {
let num = require('./extra').getNum();
console.log("in main loop: " + num);
}, 1000);


Since the print statements in main.js output every second, you'll notice that every new call of require('./extra').getNum() will get the newly decremented value.






share|improve this answer





























    0














    require() is evaluated when the main.js begins to run (i.e. before the first setInterval() triggers), so require("./extra").num always return 5.



    Also since num is passed by value in module.exports, repeatedly calling require() won't help.



    A working (not sure if best) way to do it would be:



    let num = 5;

    setInterval(() => {
    if (num > 0) {
    num--;
    }
    console.log(num);
    }, 1000);

    function getNum() {
    return num;
    }

    module.exports = {
    getNum
    };


    then



    let num = require("./extra").getNum()





    share|improve this answer





























      0














      Another perspective on this problem: you are dealing with the difference between "value" and "reference" when storing information in a variable and passing it around.



      Primitives are passed by value (strings, numbers, booleans).
      More complex structures are passed by reference (Objects, Arrays, Functions).



      What is the difference?



      Pass by value:



      let a = 3
      let b = a

      console.log(a) // 3
      console.log(b) // 3

      a = 4

      console.log(a) // 4 (obviously)
      console.log(b) // 3 (when we assigned b = a, b was assigned to the value of a, which at the time was 3)


      In this example, b does not inherit all of the properties of a during that assignment (var b = a), only it's value. This is essentially what is happening in the code you posted. Because num is a primitive, when you assign values to it, only it's value at the time is passed along. This actually happens in two places:



      module.exports = {
      num
      }


      and



      let num = require("./extra").num


      In both instances, a variable is declared (or reassigned, in the case of module.exports, and assigned the value of num which is a primitive. Now those variables and/or object properties are "locked in" so to speak, unless they are manually reassigned elsewhere in the code to a different value.



      Pass by Reference:



      What I referred to above as a "complex structure," like an Object, is (in perhaps a simplistic way of stating things) essentially a pointer to a place in memory that contains references to other values (a.k.a. properties/methods). If that sounds abstract, that's because it is. Every time an object is created, the JavaScript engine reserves another place in memory for that object's properties to be collected and referenced. This creates some interesting scenarios, like:



      const a = {}
      const b = {}

      console.log(a === b) // false!
      console.log(a == b) // also false!


      Even though we look at those objects and would say they appear to be identical/equal, they are actually both just references to places in memory, and two different places in memory, so they cannot be the same. However, if we assign both variables to the same location in memory, they will always be equal (and herein is the solution to your problem):



      const a = {}
      const b = a
      console.log(a === b) // true!
      console.log(a == b) // also true!

      a.prop = 4

      console.log(b) // { prop: 4 }

      b.prop = 2
      b.other = 3

      console.log(a) // { prop: 2, other: 3 }


      As you can see, now that both variables point to the same place in memory (const b = a), they will always "share" the same properties and therefore, the same values of those properties.



      I hope that was helpful, and here is an implementation (probably not preferred to the singleton "getNum" option recommended by others, but certainly equally viable) of these principles that solves your original problem:



      index.js



      const obj = require('./num')

      console.log(obj.num)

      setTimeout(() => console.log(obj.num), 2000)
      setTimeout(() => console.log(obj.num), 4000)
      setTimeout(() => console.log(obj.num), 6000)


      num.js



      const obj = { num: 5 }

      setInterval(() => {
      if (obj.num > 0) {
      obj.num--
      }
      }, 1000)

      module.exports = obj


      Happy coding!






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419039%2frequired-module-does-not-update-when-the-variable-inside-that-module-changes%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        This will work:



        in extra.js:



        let num = 5;

        setInterval(() => {
        if (num > 0) {
        num--;
        }
        console.log("in extra: " + num);
        }, 1000);

        module.exports = {
        getNum: function() {
        return num;
        }
        };


        in main.js:



        let num = require('./extra').getNum();
        console.log("initial value is: " + num);

        setInterval(() => {
        let num = require('./extra').getNum();
        console.log("in main loop: " + num);
        }, 1000);


        Since the print statements in main.js output every second, you'll notice that every new call of require('./extra').getNum() will get the newly decremented value.






        share|improve this answer


























          0














          This will work:



          in extra.js:



          let num = 5;

          setInterval(() => {
          if (num > 0) {
          num--;
          }
          console.log("in extra: " + num);
          }, 1000);

          module.exports = {
          getNum: function() {
          return num;
          }
          };


          in main.js:



          let num = require('./extra').getNum();
          console.log("initial value is: " + num);

          setInterval(() => {
          let num = require('./extra').getNum();
          console.log("in main loop: " + num);
          }, 1000);


          Since the print statements in main.js output every second, you'll notice that every new call of require('./extra').getNum() will get the newly decremented value.






          share|improve this answer
























            0












            0








            0






            This will work:



            in extra.js:



            let num = 5;

            setInterval(() => {
            if (num > 0) {
            num--;
            }
            console.log("in extra: " + num);
            }, 1000);

            module.exports = {
            getNum: function() {
            return num;
            }
            };


            in main.js:



            let num = require('./extra').getNum();
            console.log("initial value is: " + num);

            setInterval(() => {
            let num = require('./extra').getNum();
            console.log("in main loop: " + num);
            }, 1000);


            Since the print statements in main.js output every second, you'll notice that every new call of require('./extra').getNum() will get the newly decremented value.






            share|improve this answer












            This will work:



            in extra.js:



            let num = 5;

            setInterval(() => {
            if (num > 0) {
            num--;
            }
            console.log("in extra: " + num);
            }, 1000);

            module.exports = {
            getNum: function() {
            return num;
            }
            };


            in main.js:



            let num = require('./extra').getNum();
            console.log("initial value is: " + num);

            setInterval(() => {
            let num = require('./extra').getNum();
            console.log("in main loop: " + num);
            }, 1000);


            Since the print statements in main.js output every second, you'll notice that every new call of require('./extra').getNum() will get the newly decremented value.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 19:42









            natn2323natn2323

            628216




            628216

























                0














                require() is evaluated when the main.js begins to run (i.e. before the first setInterval() triggers), so require("./extra").num always return 5.



                Also since num is passed by value in module.exports, repeatedly calling require() won't help.



                A working (not sure if best) way to do it would be:



                let num = 5;

                setInterval(() => {
                if (num > 0) {
                num--;
                }
                console.log(num);
                }, 1000);

                function getNum() {
                return num;
                }

                module.exports = {
                getNum
                };


                then



                let num = require("./extra").getNum()





                share|improve this answer


























                  0














                  require() is evaluated when the main.js begins to run (i.e. before the first setInterval() triggers), so require("./extra").num always return 5.



                  Also since num is passed by value in module.exports, repeatedly calling require() won't help.



                  A working (not sure if best) way to do it would be:



                  let num = 5;

                  setInterval(() => {
                  if (num > 0) {
                  num--;
                  }
                  console.log(num);
                  }, 1000);

                  function getNum() {
                  return num;
                  }

                  module.exports = {
                  getNum
                  };


                  then



                  let num = require("./extra").getNum()





                  share|improve this answer
























                    0












                    0








                    0






                    require() is evaluated when the main.js begins to run (i.e. before the first setInterval() triggers), so require("./extra").num always return 5.



                    Also since num is passed by value in module.exports, repeatedly calling require() won't help.



                    A working (not sure if best) way to do it would be:



                    let num = 5;

                    setInterval(() => {
                    if (num > 0) {
                    num--;
                    }
                    console.log(num);
                    }, 1000);

                    function getNum() {
                    return num;
                    }

                    module.exports = {
                    getNum
                    };


                    then



                    let num = require("./extra").getNum()





                    share|improve this answer












                    require() is evaluated when the main.js begins to run (i.e. before the first setInterval() triggers), so require("./extra").num always return 5.



                    Also since num is passed by value in module.exports, repeatedly calling require() won't help.



                    A working (not sure if best) way to do it would be:



                    let num = 5;

                    setInterval(() => {
                    if (num > 0) {
                    num--;
                    }
                    console.log(num);
                    }, 1000);

                    function getNum() {
                    return num;
                    }

                    module.exports = {
                    getNum
                    };


                    then



                    let num = require("./extra").getNum()






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 19:42









                    William ChongWilliam Chong

                    898416




                    898416























                        0














                        Another perspective on this problem: you are dealing with the difference between "value" and "reference" when storing information in a variable and passing it around.



                        Primitives are passed by value (strings, numbers, booleans).
                        More complex structures are passed by reference (Objects, Arrays, Functions).



                        What is the difference?



                        Pass by value:



                        let a = 3
                        let b = a

                        console.log(a) // 3
                        console.log(b) // 3

                        a = 4

                        console.log(a) // 4 (obviously)
                        console.log(b) // 3 (when we assigned b = a, b was assigned to the value of a, which at the time was 3)


                        In this example, b does not inherit all of the properties of a during that assignment (var b = a), only it's value. This is essentially what is happening in the code you posted. Because num is a primitive, when you assign values to it, only it's value at the time is passed along. This actually happens in two places:



                        module.exports = {
                        num
                        }


                        and



                        let num = require("./extra").num


                        In both instances, a variable is declared (or reassigned, in the case of module.exports, and assigned the value of num which is a primitive. Now those variables and/or object properties are "locked in" so to speak, unless they are manually reassigned elsewhere in the code to a different value.



                        Pass by Reference:



                        What I referred to above as a "complex structure," like an Object, is (in perhaps a simplistic way of stating things) essentially a pointer to a place in memory that contains references to other values (a.k.a. properties/methods). If that sounds abstract, that's because it is. Every time an object is created, the JavaScript engine reserves another place in memory for that object's properties to be collected and referenced. This creates some interesting scenarios, like:



                        const a = {}
                        const b = {}

                        console.log(a === b) // false!
                        console.log(a == b) // also false!


                        Even though we look at those objects and would say they appear to be identical/equal, they are actually both just references to places in memory, and two different places in memory, so they cannot be the same. However, if we assign both variables to the same location in memory, they will always be equal (and herein is the solution to your problem):



                        const a = {}
                        const b = a
                        console.log(a === b) // true!
                        console.log(a == b) // also true!

                        a.prop = 4

                        console.log(b) // { prop: 4 }

                        b.prop = 2
                        b.other = 3

                        console.log(a) // { prop: 2, other: 3 }


                        As you can see, now that both variables point to the same place in memory (const b = a), they will always "share" the same properties and therefore, the same values of those properties.



                        I hope that was helpful, and here is an implementation (probably not preferred to the singleton "getNum" option recommended by others, but certainly equally viable) of these principles that solves your original problem:



                        index.js



                        const obj = require('./num')

                        console.log(obj.num)

                        setTimeout(() => console.log(obj.num), 2000)
                        setTimeout(() => console.log(obj.num), 4000)
                        setTimeout(() => console.log(obj.num), 6000)


                        num.js



                        const obj = { num: 5 }

                        setInterval(() => {
                        if (obj.num > 0) {
                        obj.num--
                        }
                        }, 1000)

                        module.exports = obj


                        Happy coding!






                        share|improve this answer


























                          0














                          Another perspective on this problem: you are dealing with the difference between "value" and "reference" when storing information in a variable and passing it around.



                          Primitives are passed by value (strings, numbers, booleans).
                          More complex structures are passed by reference (Objects, Arrays, Functions).



                          What is the difference?



                          Pass by value:



                          let a = 3
                          let b = a

                          console.log(a) // 3
                          console.log(b) // 3

                          a = 4

                          console.log(a) // 4 (obviously)
                          console.log(b) // 3 (when we assigned b = a, b was assigned to the value of a, which at the time was 3)


                          In this example, b does not inherit all of the properties of a during that assignment (var b = a), only it's value. This is essentially what is happening in the code you posted. Because num is a primitive, when you assign values to it, only it's value at the time is passed along. This actually happens in two places:



                          module.exports = {
                          num
                          }


                          and



                          let num = require("./extra").num


                          In both instances, a variable is declared (or reassigned, in the case of module.exports, and assigned the value of num which is a primitive. Now those variables and/or object properties are "locked in" so to speak, unless they are manually reassigned elsewhere in the code to a different value.



                          Pass by Reference:



                          What I referred to above as a "complex structure," like an Object, is (in perhaps a simplistic way of stating things) essentially a pointer to a place in memory that contains references to other values (a.k.a. properties/methods). If that sounds abstract, that's because it is. Every time an object is created, the JavaScript engine reserves another place in memory for that object's properties to be collected and referenced. This creates some interesting scenarios, like:



                          const a = {}
                          const b = {}

                          console.log(a === b) // false!
                          console.log(a == b) // also false!


                          Even though we look at those objects and would say they appear to be identical/equal, they are actually both just references to places in memory, and two different places in memory, so they cannot be the same. However, if we assign both variables to the same location in memory, they will always be equal (and herein is the solution to your problem):



                          const a = {}
                          const b = a
                          console.log(a === b) // true!
                          console.log(a == b) // also true!

                          a.prop = 4

                          console.log(b) // { prop: 4 }

                          b.prop = 2
                          b.other = 3

                          console.log(a) // { prop: 2, other: 3 }


                          As you can see, now that both variables point to the same place in memory (const b = a), they will always "share" the same properties and therefore, the same values of those properties.



                          I hope that was helpful, and here is an implementation (probably not preferred to the singleton "getNum" option recommended by others, but certainly equally viable) of these principles that solves your original problem:



                          index.js



                          const obj = require('./num')

                          console.log(obj.num)

                          setTimeout(() => console.log(obj.num), 2000)
                          setTimeout(() => console.log(obj.num), 4000)
                          setTimeout(() => console.log(obj.num), 6000)


                          num.js



                          const obj = { num: 5 }

                          setInterval(() => {
                          if (obj.num > 0) {
                          obj.num--
                          }
                          }, 1000)

                          module.exports = obj


                          Happy coding!






                          share|improve this answer
























                            0












                            0








                            0






                            Another perspective on this problem: you are dealing with the difference between "value" and "reference" when storing information in a variable and passing it around.



                            Primitives are passed by value (strings, numbers, booleans).
                            More complex structures are passed by reference (Objects, Arrays, Functions).



                            What is the difference?



                            Pass by value:



                            let a = 3
                            let b = a

                            console.log(a) // 3
                            console.log(b) // 3

                            a = 4

                            console.log(a) // 4 (obviously)
                            console.log(b) // 3 (when we assigned b = a, b was assigned to the value of a, which at the time was 3)


                            In this example, b does not inherit all of the properties of a during that assignment (var b = a), only it's value. This is essentially what is happening in the code you posted. Because num is a primitive, when you assign values to it, only it's value at the time is passed along. This actually happens in two places:



                            module.exports = {
                            num
                            }


                            and



                            let num = require("./extra").num


                            In both instances, a variable is declared (or reassigned, in the case of module.exports, and assigned the value of num which is a primitive. Now those variables and/or object properties are "locked in" so to speak, unless they are manually reassigned elsewhere in the code to a different value.



                            Pass by Reference:



                            What I referred to above as a "complex structure," like an Object, is (in perhaps a simplistic way of stating things) essentially a pointer to a place in memory that contains references to other values (a.k.a. properties/methods). If that sounds abstract, that's because it is. Every time an object is created, the JavaScript engine reserves another place in memory for that object's properties to be collected and referenced. This creates some interesting scenarios, like:



                            const a = {}
                            const b = {}

                            console.log(a === b) // false!
                            console.log(a == b) // also false!


                            Even though we look at those objects and would say they appear to be identical/equal, they are actually both just references to places in memory, and two different places in memory, so they cannot be the same. However, if we assign both variables to the same location in memory, they will always be equal (and herein is the solution to your problem):



                            const a = {}
                            const b = a
                            console.log(a === b) // true!
                            console.log(a == b) // also true!

                            a.prop = 4

                            console.log(b) // { prop: 4 }

                            b.prop = 2
                            b.other = 3

                            console.log(a) // { prop: 2, other: 3 }


                            As you can see, now that both variables point to the same place in memory (const b = a), they will always "share" the same properties and therefore, the same values of those properties.



                            I hope that was helpful, and here is an implementation (probably not preferred to the singleton "getNum" option recommended by others, but certainly equally viable) of these principles that solves your original problem:



                            index.js



                            const obj = require('./num')

                            console.log(obj.num)

                            setTimeout(() => console.log(obj.num), 2000)
                            setTimeout(() => console.log(obj.num), 4000)
                            setTimeout(() => console.log(obj.num), 6000)


                            num.js



                            const obj = { num: 5 }

                            setInterval(() => {
                            if (obj.num > 0) {
                            obj.num--
                            }
                            }, 1000)

                            module.exports = obj


                            Happy coding!






                            share|improve this answer












                            Another perspective on this problem: you are dealing with the difference between "value" and "reference" when storing information in a variable and passing it around.



                            Primitives are passed by value (strings, numbers, booleans).
                            More complex structures are passed by reference (Objects, Arrays, Functions).



                            What is the difference?



                            Pass by value:



                            let a = 3
                            let b = a

                            console.log(a) // 3
                            console.log(b) // 3

                            a = 4

                            console.log(a) // 4 (obviously)
                            console.log(b) // 3 (when we assigned b = a, b was assigned to the value of a, which at the time was 3)


                            In this example, b does not inherit all of the properties of a during that assignment (var b = a), only it's value. This is essentially what is happening in the code you posted. Because num is a primitive, when you assign values to it, only it's value at the time is passed along. This actually happens in two places:



                            module.exports = {
                            num
                            }


                            and



                            let num = require("./extra").num


                            In both instances, a variable is declared (or reassigned, in the case of module.exports, and assigned the value of num which is a primitive. Now those variables and/or object properties are "locked in" so to speak, unless they are manually reassigned elsewhere in the code to a different value.



                            Pass by Reference:



                            What I referred to above as a "complex structure," like an Object, is (in perhaps a simplistic way of stating things) essentially a pointer to a place in memory that contains references to other values (a.k.a. properties/methods). If that sounds abstract, that's because it is. Every time an object is created, the JavaScript engine reserves another place in memory for that object's properties to be collected and referenced. This creates some interesting scenarios, like:



                            const a = {}
                            const b = {}

                            console.log(a === b) // false!
                            console.log(a == b) // also false!


                            Even though we look at those objects and would say they appear to be identical/equal, they are actually both just references to places in memory, and two different places in memory, so they cannot be the same. However, if we assign both variables to the same location in memory, they will always be equal (and herein is the solution to your problem):



                            const a = {}
                            const b = a
                            console.log(a === b) // true!
                            console.log(a == b) // also true!

                            a.prop = 4

                            console.log(b) // { prop: 4 }

                            b.prop = 2
                            b.other = 3

                            console.log(a) // { prop: 2, other: 3 }


                            As you can see, now that both variables point to the same place in memory (const b = a), they will always "share" the same properties and therefore, the same values of those properties.



                            I hope that was helpful, and here is an implementation (probably not preferred to the singleton "getNum" option recommended by others, but certainly equally viable) of these principles that solves your original problem:



                            index.js



                            const obj = require('./num')

                            console.log(obj.num)

                            setTimeout(() => console.log(obj.num), 2000)
                            setTimeout(() => console.log(obj.num), 4000)
                            setTimeout(() => console.log(obj.num), 6000)


                            num.js



                            const obj = { num: 5 }

                            setInterval(() => {
                            if (obj.num > 0) {
                            obj.num--
                            }
                            }, 1000)

                            module.exports = obj


                            Happy coding!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 20:12









                            Ben StewardBen Steward

                            995313




                            995313






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419039%2frequired-module-does-not-update-when-the-variable-inside-that-module-changes%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                Refactoring coordinates for Minecraft Pi buildings written in Python