node package manager
Stop writing boring code. Discover, share, and reuse within your team. Create a free org »

require-modify

require-modify Build Status

Modify the source of a required module

Install

$ npm install --save require-modify

Usage

Replace some text.

// greet.js 
module.exports = function () {
    console.log('hello');
};
var requireModify = require('require-modify');
 
var greet = requireModify('./greet', function (source) {
    return source.replace('hello', 'yo');
});
 
greet();
//=> yo 

Example 2

Expose a local variable.

// greet.js 
var greet = function () {
    console.log('hello');
};
var requireModify = require('require-modify');
 
var greet = requireModify('./greet', function (source) {
    return source + ';module.exports = greet;';
    // the leading semicolon is in case the user have 
    // forgotten a semicolon on the last statement 
});
 
greet();
//=> hello 

API

requireModify(moduleId, callback)

moduleId

Required
Type: string

Same as you would use in require().

callback(source)

Type: function

Callback where you can modify the source and return the new one.

License

MIT © Sindre Sorhus