Skip to content

import.meta.url does not work for class methods #2649

Open
@tri2820

Description

@tri2820

Describe the Bug

Using import.meta.url inside a class method results in a SyntaxError: Cannot use 'import.meta' outside a module. This error occurs during the module's import, even without instantiating the class or invoking the method.

The source code is valid & functional when used in a <script type="module">.

const compartment = new Compartment({
      __options__: true,
      globals: {
        console,
      },
      importHook: async (specifier: string): Promise<any> => {
        if (specifier == "test") {
          const module = {
            source: new ModuleSource(
              `console.log("Can use import.meta normally", import.meta.url);
                export class Test  {

                  constructor() {
                    console.log("This throws error", import.meta.url);
                  }
                }`
            ),
            specifier,
            compartment,
          };
          return module;
        }
      },
      importMetaHook: (_moduleSpecifier: string, meta: any) => {
        meta.url = 'https://example.com';
      },
    });

    compartment.import("test");

Steps to Reproduce

  1. Create a Compartment with importHook and importMetaHook.
  2. Use importHook to return a ModuleSource containing code with a class method that accesses import.meta.url.
  3. Observe the error.

Expected Behavior

The code snippet with import.meta.url inside a class method should execute without errors, as it does with standard <script type="module">.

Platform Environment

1.71.114 Chromium: 130.0.6723.58 (Official Build) (64-bit)

"@endo/module-source": "^1.1.2",
"ses": "^1.9.1",

Additional Context

Error Message:

VM6833:5 Uncaught (in promise) SyntaxError: Cannot use 'import.meta' outside a module
    at Object.eval (eval at makeEvaluate (make-evaluate.js:92:27), <anonymous>:12:36)
    at safeEvaluate (make-safe-evaluator.js:78:14)
    at compartmentEvaluate (compartment-evaluate.js:90:10)
    at makeModuleInstance (module-instance.js:445:18)
    at instantiate (module-link.js:124:22)
    at link (module-link.js:53:10)
    at compartmentImportNow (compartment.js:58:37)
    at compartment.js:121:27

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions