new vm.SourceTextModule(code[, options])
code<string> 要解析的 JavaScript 模块代码optionsidentifier<string> 用于堆栈跟踪的字符串。 默认值:'vm:module(i)'其中i是上下文特定的升序索引。cachedData<Buffer> | <TypedArray> | <DataView> 为所提供的源提供可选的Buffer或TypedArray或DataView,其中包含 V8 的代码缓存数据。code必须与创建此cachedData的模块相同。context<Object>vm.createContext()方法返回的上下文隔离化的对象,用于编译和评估此Module。lineOffset<integer> 指定在此Module产生的堆栈跟踪中显示的行号偏移量。 默认值:0。columnOffset<integer> 指定在此Module生成的堆栈跟踪中显示的第一行列号偏移量。 默认值:0。initializeImportMeta<Function> 在评估此Module期间调用以初始化import.meta。meta<import.meta>module<vm.SourceTextModule>
importModuleDynamically<Function> 在调用import()时在评估此模块期间调用。 如果未指定此选项,则调用import()将使用ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING拒绝。specifier<string> 传给import()的说明符module<vm.Module>importAssertions<Object> 传给optionsExpression可选参数的"assert"值,如果没有提供值,则为空对象。- 返回: <Module Namespace Object> | <vm.Module> 建议返回
vm.Module以利用错误跟踪,并避免包含then函数导出的命名空间出现问题。
创建新的 SourceTextModule 实例。
分配给作为对象的 import.meta 对象的属性可能允许模块访问指定 context 之外的信息。
使用 vm.runInContext() 在特定上下文中创建对象。
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) {
// 注意:这个对象是在顶层上下文中创建的。因此,
// Object.getPrototypeOf(import.meta.prop) 指向
// 顶层上下文中的 Object.prototype,
// 而不是在上下文对象中。
meta.prop = {};
}
});
// 由于模块没有依赖关系,链接器函数永远不会被调用。
await module.link(() => {});
await module.evaluate();
// 现在,Object.prototype.secret 将等于 42。
//
// 要解决这个问题,则将上面的
// meta.prop = {};
// 替换为
// 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) {
// 注意:这个对象是在顶层上下文中创建的。因此,
// Object.getPrototypeOf(import.meta.prop) 指向
// 顶层上下文中的 Object.prototype,
// 而不是在上下文对象中。
meta.prop = {};
}
});
// 由于模块没有依赖关系,链接器函数永远不会被调用。
await module.link(() => {});
await module.evaluate();
// 现在,Object.prototype.secret 将等于 42。
//
// 要解决这个问题,则将上面的
// meta.prop = {};
// 替换为
// meta.prop = vm.runInContext('{}', contextifiedObject);
})();code<string> JavaScript Module code to parseoptionsidentifier<string> String used in stack traces. Default:'vm:module(i)'whereiis a context-specific ascending index.cachedData<Buffer> | <TypedArray> | <DataView> Provides an optionalBufferorTypedArray, orDataViewwith V8's code cache data for the supplied source. Thecodemust be the same as the module from which thiscachedDatawas created.context<Object> The contextified object as returned by thevm.createContext()method, to compile and evaluate thisModulein.lineOffset<integer> Specifies the line number offset that is displayed in stack traces produced by thisModule. Default:0.columnOffset<integer> Specifies the first-line column number offset that is displayed in stack traces produced by thisModule. Default:0.initializeImportMeta<Function> Called during evaluation of thisModuleto initialize theimport.meta.meta<import.meta>module<vm.SourceTextModule>
importModuleDynamically<Function> Called during evaluation of this module whenimport()is called. If this option is not specified, calls toimport()will reject withERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING.specifier<string> specifier passed toimport()module<vm.Module>importAssertions<Object> The"assert"value passed to theoptionsExpressionoptional parameter, or an empty object if no value was provided.- Returns: <Module Namespace Object> | <vm.Module> Returning a
vm.Moduleis recommended in order to take advantage of error tracking, and to avoid issues with namespaces that containthenfunction exports.
Creates a new SourceTextModule instance.
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);
})();