2

All I am trying to do is to export two functions from a module. One function taking an argument and the other with no argument:

function initClass(params)
{
   return new Promise( (resolve, reject) => {
        if (!wallet) {
            wallet = new WalletClient(params);
            resolve(wallet);
        } else {
            console.log('Wallet is initialized');
            resolve(wallet);
        }
});
}

function getInstance()
{
   return wallet;
}

For initClass(params) only, I can do this as:

module.exports = (params) => {
   initClass(params)
}

And then I call this as:

var init = require('./class.js')(params).initClass(params);

This works fine.

Now, for me to export getInstance() as well, I have tried to do the following but it doesn't seem to work.

module.exports = (params) => {
   initClass(params),
   getInstance
}

This complaints that there is no function getInstance.

Then I tried this:

module.exports.init = (params) => {
   initClass(params)
}

module.exports.instance = {
   getInstance
}

Then call them as:

var init = require('./class.js').init(params).initClass(params);

What is the proper way to export multiple functions like this? Thank you.

5
  • 1
    Regardless of import/export, to call initClass chained to init, init would have to return something that has initClass on its prototype - which it clearly doesn't Commented Jul 19, 2019 at 8:25
  • 1
    Your var init = require('./class.js')(params).initClass(params); call does not look valid. Or are you returning something from the exported function? Commented Jul 19, 2019 at 8:32
  • @Bergi Yes, I am exporting an instantiated class from initClass Commented Jul 19, 2019 at 8:36
  • 1
    @madu Then please show the actual code. Commented Jul 19, 2019 at 8:44
  • @Bergi added the code of initClass. Thank you. Commented Jul 19, 2019 at 8:48

3 Answers 3

3

You're making it more complex than needed. Once you have your functions defined, you can export it with this:

module.exports = {
  initClass,
  getInstance
}

To use it, you do this:

const init = require("./class.js");
init.initClass(params);
const instance = init.getInstance();

What you're exporting from the module is an object (which I've named init in the example above) that contains two functions. You don't have to pass arguments to the functions at the time you require.

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

Comments

2

module.exports is basically an object with keys which can refer to any variables/functions of your file.In your case,

module.exports.initClass = function (params){
...
}

module.exports.getInstance = function (){
}

When importing it

var init = require('./class.') // init object has two keys -> initClass and getInstance
 init.initClass('abc')
 init.getInstance()

Comments

1

If i'm understanding correctly you are trying to export multiple methods if that is the case simple use this.

   module.exports = {
        method: function() {},
        otherMethod: function(parmas) {}
    }

In your code use like this.

 var init = require('./class.js');
    init.method()
    init.otherMethond(paramObj)

If you want below scenario you need to check out about method chaining.

var init = require('./class.js').init(params).initClass(params);

1 Comment

Thank you Venkat. I tried doing require('./class.js').init(params).initClass(params) but it gives an error that cant read property of undefined initClass

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.