Extending or adding functions to modules of Node
// module that you like to extend
var async = require('async')
// add a new function, myOwnFunction, to the module
async.myOwnFunction = function () {
// do something
}
// re-export the module, for the changes to take effect
module.exports = async
The steps in order to complete this would be:
- Get the module's pre-defined functionality as your own variable
- Modify, delete, and/or append more functionality to the variable (via Javascript's functional paradigm)
- Re-export the variable as your own, which allows you to
require
it (either as an NPM module or as a file module).
First, we require async.
var newModule = require('async');
Now that we've retrieved the module async
, we can append our own function.
newModule.betterParallel = function(myParameters) { ... };
Not only can we add our own function, but we can even delete from the module - since it is now our own.
delete newModule['series'];
With that completed, we need to then re-export our new module.
module.exports = newModule;
If you want to publish this to NPM as your own module, you can use npm publish
. If you don't want to, you can simply require
this file - and now it contains your modified changes.