Virtual module for node.js - vmod
- Runs in different script context than your current script;
- Executes a string of code in require-like environment;
- Allows you to override globals of the sandbox environment;
- Returns
module.exports
output; - Throws errors that can be caught in your current context;
Known limitations:
- console.log, console.warn, console.error, etc. output not visible;
- Modifications to prototypes of some globals not visible in the vmod context;
- Only CommonJS modules supported;
$ npm install vmod --save
const vmod = require('vmod');
console.log(
vmod('module.exports = 123;')
); // 123
const vmod = require('vmod');
console.log(
vmod(
'module.exports = () => "yay!";'
)()
); // "yay!"
/*
./_test-file.js:
module.exports = "test file data";
*/
const vmod = require('vmod');
console.log(
vmod(
'module.exports = require("./_test-file.js")'
)()
); // "test file data"
const vmod = require('vmod');
vmod(
'module.exports = require("./_test-file.js")',
{ require: null }
); // TypeError: require is not a function