Skip to content

vm importModuleDynamically option in Node 20.10 requires --experimental-vm-modules flag and 20.9 does not #51154

@zachleat

Description

@zachleat

Version

v20.10.0

Platform

23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 arm64

Subsystem

node:vm

What steps will reproduce the bug?

Given the following (I also tested a CommonJS version with the same result):

import vm from "vm";

let code = `
;(async function() {
	await import("@zachleat/noop");
})()`

let context = vm.createContext({});

await vm.runInContext(code, context, {
	importModuleDynamically: function(specifier) {
		return import(specifier);
	}
});

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

Throws an error every time on Node v20.10 and newer. Both in ESM and CJS versions of the test code.

What is the expected behavior? Why is that the expected behavior?

Previous versions of Node prior to v20.10 did not require the --experimental-vm-modules flag. If import() is supported in CommonJS—why is import() not supported in vm? I was relying on this method as an escape hatch until vm.Module was stable. I suppose my question is: was this a bug that was fixed or is this a regression?

Failures

Node v20.10 and newer: `node reduced-test.js`
Node v20.10 and newer: `node reduced-test.cjs`

Successes

Node v20.10 and newer: `node --experimental-vm-modules reduced-test.js`
Node v20.10 and newer: `node --experimental-vm-modules reduced-test.cjs`
Node v14–v20.9: `node --experimental-vm-modules reduced-test.js`
Node v14–v20.9: `node --experimental-vm-modules reduced-test.cjs`
Node v14–v20.9: `node reduced-test.js`
Node v14–v20.9: `node reduced-test.cjs`

What do you see instead?

node:internal/modules/esm/utils:180
    throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG();
          ^

TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG]: A dynamic import callback was invoked without --experimental-vm-modules
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:180:11)
    at evalmachine.<anonymous>:3:2
    at evalmachine.<anonymous>:4:3
    at Script.runInContext (node:vm:133:12)
    at Object.runInContext (node:vm:279:6)
    at file:///Users/zachleat/Code/node-retrieve-globals/reduced-test.js:10:20
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12) {
  code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG'
}

Node.js v20.10.0

Additional information

Appreciate y’all!

Metadata

Metadata

Assignees

No one assigned

    Labels

    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