node-retrieve-globals
Execute a string of JavaScript using Node.js and return the global variable values and functions.
- Sync and async methods for synchronous or asynchronous JavaScript code respectively.
- Can return any JavaScript data types
- Can provide external variable values as context to the local scope
Installation
Available on npm
npm install node-retrieve-globals
Usage
import { ModuleScript } from "node-retrieve-globals";
And then:
let code = `var a = 1;
const b = "hello";
function hello() {}`;
let vm = new ModuleScript(code);
vm.getGlobalContextSync();
Returns:
{ a: 1, b: "hello", hello: function hello() {} }
Pass in your own Data and reference it in the JavaScript code
let code = `let ref = myData;`;
let vm = new ModuleScript(code);
vm.getGlobalContextSync({ myData: "hello" });
Returns:
{ ref: "hello" }