Skip to content
Virtual module for node.js
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
README.md
_test-file.js
package.json
vmod.js
vmod.test.js
yarn.lock

README.md

NPM version Build Status Downloads

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;

Installation

$ npm install vmod --save

Simple

'use strict';
const vmod = require('vmod');

console.log(
    vmod('module.exports = 123;')
); // 123

Export function

'use strict';
const vmod = require('vmod');

console.log(
    vmod(
        'module.exports = () => "yay!";'
    )()
); // "yay!"

Require External file

/*
./_test-file.js:
module.exports = "test file data";
*/

'use strict';
const vmod = require('vmod');

console.log(
    vmod(
        'module.exports = require("./_test-file.js")'
    )()
); // "test file data"

Override sandbox variable

'use strict';
const vmod = require('vmod');

vmod(
    'module.exports = require("./_test-file.js")',
    { require: null }
); // TypeError: require is not a function
You can’t perform that action at this time.