0

Currently I am just passing my fileNamePrefix like that:

let shortid = require('shortid');
let fileNamePrefix = shortid.generate();

module1.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f1.json`
module2.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f2.json`
module3.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f3.js

Which I think in not quite right, I might need to pass more things to my modules later on, so I want to avoid to pass that as function params.

What is the best way to approach that in nodejs?

Will global object like global.fileNamePrefix = shortid.generate(); will do in that case or would you approach that different? I just read that global is not good...

Sergino
  • 10,128
  • 30
  • 98
  • 159
  • This doesn't really look like a specific node problem and you should be more precise in your question. How is that "filename" used ? Why don't you just export it ? If it's conceptually part of a bigger thing, why don't you make it a part of a bigger object ? – Denys Séguret May 04 '17 at 11:45
  • 1. You can do fileName = shortid.generate(); in every module if its not goint to create efficiency problems. 2. You can use singleton pattern in which can have a property filename. It will only have one state across the project so you do not need to worry about efficiency. – Mihir Bhende May 04 '17 at 11:46
  • @Blastfreak no I cant, I need the same name across, basically just modified the question – Sergino May 04 '17 at 11:47
  • Then you can definitely use singleton pattern. Then even if you do shortid.generate(); in every module, it will check if it already exists, it will return that otherwise it will create a new filename and return. So even if you have added shortid.generate(); multiple times in different modules, filename is eventually the same. – Mihir Bhende May 04 '17 at 11:49
  • 1
    Check this out, might give you a direction : http://stackoverflow.com/questions/17120117/sharing-modifying-a-variable-between-multiple-files-node-js – Mihir Bhende May 04 '17 at 11:50
  • @Blastfreak yeah, i thought so, about singleton but just wasn't sure that it is best approach in that case – Sergino May 04 '17 at 11:51
  • @Blastfreak `require('./index.js').fileNamePrefix;` - is it really normal approach ? Will it create an instance of `index.js` or just get `.fileNamePrefix` value without doing anything else? – Sergino May 04 '17 at 11:54
  • Yes its an efficient approach. Firstly you do not need to pass parameter everytime like you are doing now. Secondly, singleton is not creating new instance everytime, so its efficient. In singleton, basically even if you are creating a new object, you are internally getting the same already in user instance state. So by looking at the code, it will look like everytime new object is created however internally its not. – Mihir Bhende May 04 '17 at 12:00
  • @Blastfreak are you calling that `require('./index.js').fileNamePrefix` a singleton? – Sergino May 04 '17 at 12:01
  • 1
    no, the index.js will have a a singleton pattern. You will then call the properly of that instance using .fileNamePrefix – Mihir Bhende May 04 '17 at 12:02
  • @Blastfreak cant you just show that in the answer ? :) Sounds like it is something a little bit different than just in that answer http://stackoverflow.com/a/43782343/2926340 is it? – Sergino May 04 '17 at 12:06

2 Answers2

1

Just create module like that:

// unicname.js
let shortid = require('shortid');
let fileName = shortid.generate();

module.exports = {fname: fileName};

//module1.js
const {fname} = require('./unicname.js');
....

Since the node.js caching the modules the value will be calculated only one time so you can get same value in all your modules.

Sergaros
  • 821
  • 1
  • 5
  • 14
  • this one looking a bit different to what @Blastfreak is talking about in comments. What is the difference if you just call singleton in the main(`index.js`) module and then export the value to use that like `require('./index.js').fileNamePrefix` in other modules? Any thoughts on that? – Sergino May 04 '17 at 12:13
  • Maybe I not clear what do you need. As I understand you need once calcute value and use it in all modules of your project? – Sergaros May 04 '17 at 12:21
  • that is correct I need to calculate a value in my `index.js` and use that across other modules – Sergino May 04 '17 at 21:35
1

You can use either singleton approach or approach suggested by @Сергей Тертичный

  1. Singleton :

    //shortid.js
    
    var fileSingleTon = module.exports = {
    
        fileNamePrefix: null,
    
        getFileNamePrefix: function() {
            return fileSingleTon.fileNamePrefix || fileSingleTon.generate()
        },
        generate: function() {
            console.log('generating..');
            return fileSingleTon.fileNamePrefix = 'your_file_prefix';
        }
    }
    
    //module1.js
    
    var fileNamePrefix = require('./shortid').getFileNamePrefix();
    
    //do stuff for module1
    
    //module2/js
    
    var fileNamePrefix = require('./shortid').getFileNamePrefix();
    
    //do stuff for module1
    

    and so on..

Even now you are calling require('./shortid').getFileNamePrefix(); multiple times, generate function is getting called only once.

  1. Node Caching approach :

Consider you have shortid.js as following :

// A: block of code to do some manipulations

// B : module.exports = manipulation result.

So basically in node js "modules" core module which is responsible for giving us module.export functionality executes whatever is here above export(in abode example the part A) only for the first time and caches it even if you have required in in multiple other files. However, it only executes the functions or block of code in every require which is inside export. So you can use this approach where your A block will have login to generate fileNamePrefix and then B just returns it.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37