zaphod1984
zaphod1984

Reputation: 856

Customizing existing Module

I have a collection of helper functions and i like to merge them together with already existing utility modules.

Somehow like this:

var customUtil = require('customUtilites');
customUtil.anotherCustomFunction = function() { ... };

exports = customUtil;

Can this be achieved somehow?

Upvotes: 1

Views: 552

Answers (1)

qiao
qiao

Reputation: 18219

You can totally do so.

e.g.

customUtilities.js:

module.exports = {
  name: 'Custom'
};

helperA.js

module.exports = function() {
  console.log('A');
}

helperB.js:

module.exports = function() {
  console.log('B');
}

bundledUtilities.js:

var customUtilities = require('./customUtilities');

customUtilities.helperA = require('./helperA');
customUtilities.helperB = require('./helperB');

module.exports = customUtilities;

main.js:

var utilities = require('./bundledUtilities');
utilities.helperA();

run node main.js you will see A printed.

Upvotes: 2

Related Questions