patriksimek / vm2

Advanced vm/sandbox for Node.js
MIT License
3.86k stars 293 forks source link

How to get the result of the code which is run by Node vm2 #496

Closed SotiriaSte closed 1 year ago

SotiriaSte commented 1 year ago

For example

const vm = new NodeVM({ console: 'inherit', sandbox: {} }); const script = 'const y = 2; const x =1; x + y;'; const result = vm.run(script);

The result is now {}. How to get the actual result witch is 3 in my case?

XmiliaH commented 1 year ago

The script in case of the NodeVM is a module. It needs to export the result.

const vm = new NodeVM({ console: 'inherit', sandbox: {} });
const script = 'const y = 2; const x =1; exports.result = x + y;';
const result = vm.run(script).result;