0

I've tried using the isolated-vm and vm2 libraries to run code in an isolated environment, but I've run into issues where the required packages are not installed in the isolated environment. isolated-vm not support module and vm2 not installing it on isolated enviroment.

Here I tried with vm2 to run script -

import { NodeVM, VMScript } from "vm2";
import { resolve } from "path";
import fs from "fs";

async function runScript(scriptPath, dependencies = []) {
  const scriptSource = fs.readFileSync(scriptPath, "utf8");

  const vm = new NodeVM({
    console: "inherit",
    sandbox: {},
    require: {
      external: true,
      import: dependencies,
    },
  });
  const script = new VMScript(scriptSource);
  const result = await vm.run(script);
  console.log(result);
}

runScript(resolve("./src/discordService.js"), ["discord.js-selfbot-v13"]);

phd
  • 82,685
  • 13
  • 120
  • 165
  • Perhaps a container? But why cant just simple install the needed modules? – Marc May 03 '23 at 05:54
  • vm2 is deprecated. It has critical security issues and should not be used for production anymore. They suggest migrating to isolated-vm. Reference: https://github.com/patriksimek/vm2 – Radha Satam Aug 16 '23 at 13:22

0 Answers0