Sharing & modifying a variable between multiple files node.js

后端 未结 7 1060
日久生厌
日久生厌 2020-12-04 14:25

main.js

var count = 1;

// psuedocode
// if (words typed begins with @add)
require(\'./add.js\');

// if (words typed begins with @remove)
require(\'./remo         


        
相关标签:
7条回答
  • 2020-12-04 15:16

    I know I'm a little bit late to answer this questions, just 7yrs!

    You can simply use a global variable:

    global.myVar = 'my-val';
    console.log(myVar); // returns 'my-val'
    // from here on it's accessable to all modules by just the variable name
    

    using-global-variables-in-node-js

    0 讨论(0)
提交回复
热议问题