October 22, 2024
Chicago 12, Melborne City, USA
javascript

How to create ES Module context with eval?


I have a Scheme interpreter written in JavaScript called LIPS. It’s all ES Module. But as part of the standard library I’ve created require function that use code like this:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

But you can’t import esm package with require. So I need to implement import.

The problem is that import is not global variable. For some reason it’s accessible but not in window or global. So to get access it from scheme you need (using JavaScript syntax)

eval("import")("fs")

But eval don’t create ES context and this throw an error:

Uncaught SyntaxError: Cannot use import statement outside a module

I want to know if you can create ES context with eval, so I can access dynamic import with the need to modify the library JavaScript code.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video