1

I have an npm module that I would like to configure once and call in multiple places.

The npm module (let's call it 'signature') is basically like this

module.exports = function(options) {
 return new Signature(options);
};

var Signature = function(options) { }
Signature.prototype.sign = function() {}

I made another module ('signer') to configure it:

var signature = require('signature');

module.exports = function() {
// I pass whatever config options here
return signature({});
};

In my code I do:

var signer = require('../utils/signer');
signer.sign();

However this gives me a "has no method "sign" error. What am I doing wrong? I suspect I have to initialize something but not sure what. If I bypass the config module (signer) and just call the signature module then it works fine:

var signature = require('signature');
var s = signature();
s.sign();

1 Answer 1

1

Signer exports a function that returns a signature. Try:

var signer = require('../utils/signer');
signer().sign();
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't that create a new instance of Signature each time? I am basically wanting to configure Signature once and have an easy way to call the ready made instance wherever. Perhaps I need to change my approach?
It would. From the sample you have provided, if you want signer to export a single instance you could change it to: module.exports = signature({});

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.