https://github.com/zewish/vmod
Virtual module for node.js
https://github.com/zewish/vmod
eval module nodejs safe-evaluation virtual
Last synced: 20 days ago
JSON representation
Virtual module for node.js
- Host: GitHub
- URL: https://github.com/zewish/vmod
- Owner: zewish
- Created: 2017-11-07T17:20:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-07-28T14:24:16.000Z (almost 3 years ago)
- Last Synced: 2025-10-29T05:59:49.947Z (7 months ago)
- Topics: eval, module, nodejs, safe-evaluation, virtual
- Language: TypeScript
- Size: 117 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/vmod)
[](https://www.npmjs.com/package/vmod)
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;
Installation
------------
```bash
$ npm install vmod --save
```
Simple
------
```js
const vmod = require('vmod');
console.log(
vmod('module.exports = 123;')
); // 123
```
Export function
---------------
```js
const vmod = require('vmod');
console.log(
vmod(
'module.exports = () => "yay!";'
)()
); // "yay!"
```
Require External file
---------------------
```js
/*
./_test-file.js:
module.exports = "test file data";
*/
const vmod = require('vmod');
console.log(
vmod(
'module.exports = require("./_test-file.js")'
)()
); // "test file data"
```
Override sandbox variable
-------------------------
```js
const vmod = require('vmod');
vmod(
'module.exports = require("./_test-file.js")',
{ require: null }
); // TypeError: require is not a function
```