patriksimek / vm2

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

Is import.meta supported? #438

Closed cristiano-belloni closed 2 years ago

cristiano-belloni commented 2 years ago

I need to resolve the location of a module in my sandbox, and I need to resolve it like if it was an ESM module (i.e. respecting the conditional exports). I'm trying to use import.meta.resolve like this:

const getSandboxedModule = vm.run(
    `
    module.exports = async function(target) {
      const loc = import.meta.resolve(target)
      return require(loc);
    }
    `,
    path.join(__dirname, storageDirectory, "sandbox.mjs")
  );

But I get this error: SyntaxError: Cannot use 'import.meta' outside a module

Is there any way to use the import resolver inside vm2 (I know that import is not supported, but I'm only trying to resolve a dependency, not importing it)

XmiliaH commented 2 years ago

I do not know of a way to support import.meta in a normal script. The node VM module seems to not have any option for this.