new vm.SourceTextModule(code[, options])


  • code <string> 解析 JavaScript 模块代码
  • options
    • identifier <string> 在堆栈跟踪中使用的字符串。 默认值: 'vm:module(i)',其中 i 是上下文特定的递增索引。
    • cachedData <Buffer> | <TypedArray> | <DataView> 为提供的源代码提供可选的 BufferTypedArray,或者带有 V8 代码缓存数据的 DataViewcode 必须与创建此 cachedData 的模块相同。
    • context <Object>vm.createContext() 方法返回的 情境化 对象,用于编译和评估此 Module。如果未指定上下文,则模块将在当前执行上下文中进行评估。
    • lineOffset <integer> 指定由此 Module 生成的堆栈跟踪中显示的行号偏移量。默认值: 0
    • columnOffset <integer> 指定由此 Module 生成的堆栈跟踪中显示的首行列号偏移。默认值: 0
    • initializeImportMeta <Function> 在评估此 Module 时调用,以初始化 import.meta
    • importModuleDynamically <Function> 在评估此模块时调用,当调用 import() 时触发。如果未指定此选项,对 import() 的调用将以 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING 拒绝。如果未设置 --experimental-vm-modules,此回调将被忽略,对 import() 的调用将以 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG 拒绝。
      • specifier <string> 传递给 import() 的说明符
      • module <vm.Module>
      • importAttributes <Object> 传递给 [optionsExpression][optionsExpression] 可选参数的 "assert" 值,如果未提供值,则为一个空对象。
      • 返回:<Module Namespace Object> | <vm.Module> 建议返回 vm.Module,以便利用错误跟踪,并避免包含 then 函数导出的命名空间出现问题。

创建一个新的 SourceTextModule 实例。

🌐 Creates a new SourceTextModule instance.

分配给 import.meta 对象的属性如果是对象,可能允许模块访问指定 context 之外的信息。使用 vm.runInContext() 可以在特定上下文中创建对象。

🌐 Properties assigned to the import.meta object that are objects may allow the module to access information outside the specified context. Use vm.runInContext() to create objects in a specific context.

import vm from 'node:vm';

const contextifiedObject = vm.createContext({ secret: 42 });

const module = new vm.SourceTextModule(
  'Object.getPrototypeOf(import.meta.prop).secret = secret;',
  {
    initializeImportMeta(meta) {
      // Note: this object is created in the top context. As such,
      // Object.getPrototypeOf(import.meta.prop) points to the
      // Object.prototype in the top context rather than that in
      // the contextified object.
      meta.prop = {};
    },
  });
// Since module has no dependencies, the linker function will never be called.
await module.link(() => {});
await module.evaluate();

// Now, Object.prototype.secret will be equal to 42.
//
// To fix this problem, replace
//     meta.prop = {};
// above with
//     meta.prop = vm.runInContext('{}', contextifiedObject);const vm = require('node:vm');
const contextifiedObject = vm.createContext({ secret: 42 });
(async () => {
  const module = new vm.SourceTextModule(
    'Object.getPrototypeOf(import.meta.prop).secret = secret;',
    {
      initializeImportMeta(meta) {
        // Note: this object is created in the top context. As such,
        // Object.getPrototypeOf(import.meta.prop) points to the
        // Object.prototype in the top context rather than that in
        // the contextified object.
        meta.prop = {};
      },
    });
  // Since module has no dependencies, the linker function will never be called.
  await module.link(() => {});
  await module.evaluate();
  // Now, Object.prototype.secret will be equal to 42.
  //
  // To fix this problem, replace
  //     meta.prop = {};
  // above with
  //     meta.prop = vm.runInContext('{}', contextifiedObject);
})();