Skip to content

SourceTextModule: Using dynamic imports while passing cachedData throws TypeError: Invalid host defined options #43663

Description

@Chooks22

Version

v18.4.0

Platform

Linux arch-linux 5.18.8-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Wed, 29 Jun 2022 23:03:10 +0000 x86_64 GNU/Linux

Subsystem

node:vm

What steps will reproduce the bug?

Needs --experimental-vm-modules flag

import { SourceTextModule } from 'node:vm'

async function linker() {
  const fakeMod = new SourceTextModule('console.log("Hello, world!")')
  await fakeMod.link(() => {})
  await fakeMod.evaluate()
  return fakeMod
}

const code = 'import("")'
const modA = new SourceTextModule(code, {
  importModuleDynamically: linker,
})

const modB = new SourceTextModule(code, {
  importModuleDynamically: linker,
  cachedData: modA.createCachedData(),
})

await modA.link(linker)
await modA.evaluate() // logs "Hello, world!"

await modB.link(linker)
await modB.evaluate() // TypeError: Invalid host defined options

How often does it reproduce? Is there a required condition?

100% of the time when passing cachedData

What is the expected behavior?

Dynamic imports works as expected.

What do you see instead?

vm:module(0):1
import("")
^

TypeError: Invalid host defined options
    at vm:module(0):1:1
    at SourceTextModule.evaluate (node:internal/vm/module:226:23)
    at file:///home/chooks/code/play/test.js:24:12

Additional information

  • Works when cachedData is not passed.
  • Works when using static imports.
  • Breaks even if returned the same reference to fakeMod.

Activity

  1. added
    vmIssues and PRs related to the vm subsystem.
    on Jul 3, 2022
  2. benjamingr commented on Jul 3, 2022

    @benjamingr
    Member

    @nodejs/loaders

  3. aduh95 commented on Jul 5, 2022

    @aduh95
    Contributor

    @nodejs/vm

  4. benjamingr commented on Jul 5, 2022

    @benjamingr
    Member

    Probably same issue as #43681

  5. legendecas commented on Jul 8, 2022

    @legendecas
    Member

    I don't think this is related to #43681. The problem here is that the v8::Module created from cache_data has an incorrect host_defined_options (an empty one), rather than the one defined by the host, tracked on https://bugs.chromium.org/p/chromium/issues/detail?id=1244145. I'll reach out to cbruni to see the next steps for the issue.

  6. added
    v8 engineIssues and PRs related to the V8 dependency.
    confirmed-bugIssues and PRs for confirmed bugs.
    on Jul 8, 2022
  7. SnirBroshi commented on Aug 27, 2022

    @SnirBroshi

    Similar repro with regular vm.Script:

    const vm = require('vm');
    
    const code = `
    	console.log('Hello');
    	import('foo');
    `;
    
    function importModuleDynamically(specifier, _, importAssertions) {
    	console.log('importing...');
    	return new Promise(resolve => {}); // stub, never resolves
    }
    
    const scriptA = new vm.Script(code, {
    	importModuleDynamically,
    });
    const cachedData = scriptA.createCachedData();
    scriptA.runInThisContext();
    
    const scriptB = new vm.Script(code, {
    	importModuleDynamically,
    	cachedData,
    });
    if (scriptB.cachedDataRejected) throw new Error();
    scriptB.runInThisContext();

    Output:

    Hello
    importing...
    Hello
    evalmachine.<anonymous>:3
            import('foo');
            ^
    
    TypeError: Invalid host defined options
        at evalmachine.<anonymous>:3:2
        at Script.runInThisContext (node:vm:129:12)
        at Object.<anonymous> (demo.js:24:9)
        at Module._compile (node:internal/modules/cjs/loader:1105:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Module._load (node:internal/modules/cjs/loader:827:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
        at node:internal/main/run_main_module:17:47
    
  8. SnirBroshi commented on Sep 18, 2023

    @SnirBroshi

    This used to work and then got broken.

    In v16.10.0 there is no problem importing modules, and in v16.11.0 it throws this error.

    I'm not sure what specific change caused it (here's Node.js changelog v16.10.0 ... v16.11.0, and v8 changelog for these versions), but I suspect this v8 commit.

  9. sdegutis commented on Oct 28, 2023

    @sdegutis

    I'm having this error too (also the TS type for this function is missing):

    sdegutis/immaculatalibrary.com@d7fe87a

  10. joyeecheung commented on Jan 29, 2026

    @joyeecheung
    Member

    I believe this is already fixed by #48510 - it no longer reproduces on main. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.v8 engineIssues and PRs related to the V8 dependency.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions