I am trying to execute a script code inside isolated-vm‘s isolate. Script code needs some third party packages as dependencies like lodash, node-xlsx, etc.,. Is it possible to pass the dependent modules to an isolate?
Getting the following error while trying to pass module to an isolate:
TypeError: [object Module] could not be cloned.
Code:
import ivm from 'isolated-vm'
import _ from 'lodash-es'
(async function() {
try{
let code = `
async function setup() {
log('setup called!!');
log('config -> ', config);
}
setup();
`;
const isolate = new ivm.Isolate({ memoryLimit: 128 })
const context = isolate.createContextSync()
const jail = context.global
jail.setSync('global', jail.derefInto());
const config = {
id: 1234,
name: 'test'
}
jail.setSync('config', new ivm.ExternalCopy(config).copyInto());
jail.setSync('libraries', new ivm.ExternalCopy(_).copyInto());
jail.setSync('log', function(...args) {
console.log(...args);
})
const hostile = isolate.compileScriptSync(code)
hostile.run(context).catch(err => console.error(err));
} catch(e) {
console.log('err->', e)
}
}) ()
Expected: Need to access libraries (lodash module) inside setup() function