cjs-expose is a simple utility tool for exposing private variables and functions from within a commonJS module.
npm install --save cjs-expose
or
npm install --save-dev cjs-expose
depending on use case.
//====== MODULE mymodule.js ======
const myPrivateVar = 1;
function myPrivateFunction() {
}
let myOtherPrivateFunction = function() {
}
//==========END MODULE================
//====== MODULE myothermodule.js ======
const expose = require('cjs-expose');
const myAccessor = expose('./mymodule.js');
console.log(myAccessor('myPrivateFunction'));
//output: [function myPrivateFunction]
console.log(myAccessor('myOtherPrivateFunction'));
//output: [function myOtherPrivateFunction]
console.log(myAccessor('myPrivateVar'));
//output: 1
console.log(myAccessor(['myOtherPrivateFunction']));
//output: [function myOtherPrivateFunction]
console.log(['myPrivateFunction','myOtherPrivateFunction'])
/*
output: {
myPrivateFunction:[function myPrivateFunction],
myOtherPrivateFunction:[function myOtherPrivateFunction]
}
*/
//==========END MODULE================