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...

11
  • 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 ? Commented May 4, 2017 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. Commented May 4, 2017 at 11:46
  • @Blastfreak no I cant, I need the same name across, basically just modified the question Commented May 4, 2017 at 11:47
  • 1
    Check this out, might give you a direction : stackoverflow.com/questions/17120117/… Commented May 4, 2017 at 11:50
  • 1
    no, the index.js will have a a singleton pattern. You will then call the properly of that instance using .fileNamePrefix Commented May 4, 2017 at 12:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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?
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?
that is correct I need to calculate a value in my index.js and use that across other modules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.