new vm.SourceTextModule(code[, options])


  • code <string> 要解析的 JavaScript 模块代码

    ¥code <string> JavaScript Module code to parse

  • options

    • identifier <string> 用于堆栈跟踪的字符串。默认值:'vm:module(i)',其中 i 是上下文特定的升序索引。

      ¥identifier <string> String used in stack traces. Default: 'vm:module(i)' where i is a context-specific ascending index.

    • cachedData <Buffer> | <TypedArray> | <DataView> 为所提供的源提供可选的 BufferTypedArrayDataView,其中包含 V8 的代码缓存数据。code 必须与创建此 cachedData 的模块相同。

      ¥cachedData <Buffer> | <TypedArray> | <DataView> Provides an optional Buffer or TypedArray, or DataView with V8's code cache data for the supplied source. The code must be the same as the module from which this cachedData was created.

    • context <Object> vm.createContext() 方法返回的 contextified 对象,用于编译和评估此 Module 中的对象。如果未指定上下文,则在当前执行上下文中评估模块。

      ¥context <Object> The contextified object as returned by the vm.createContext() method, to compile and evaluate this Module in. If no context is specified, the module is evaluated in the current execution context.

    • lineOffset <integer> 指定在此 Module 产生的堆栈跟踪中显示的行号偏移量。默认值:0

      ¥lineOffset <integer> Specifies the line number offset that is displayed in stack traces produced by this Module. Default: 0.

    • columnOffset <integer> 指定在此 Module 生成的堆栈跟踪中显示的第一行列号偏移量。默认值:0

      ¥columnOffset <integer> Specifies the first-line column number offset that is displayed in stack traces produced by this Module. Default: 0.

    • initializeImportMeta <Function> 在评估此 Module 期间调用以初始化 import.meta

      ¥initializeImportMeta <Function> Called during evaluation of this Module to initialize the import.meta.

    • importModuleDynamically <Function> 用于指定在调用 import() 时评估该模块期间应如何加载模块。此选项是实验模块 API 的一部分。不建议在生产环境中使用它。详细信息参见 编译 API 中支持动态 import()

      ¥importModuleDynamically <Function> Used to specify the how the modules should be loaded during the evaluation of this module when import() is called. This option is part of the experimental modules API. We do not recommend using it in a production environment. For detailed information, see Support of dynamic import() in compilation APIs.

创建新的 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);
})();