当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js module.syncBuiltinESMExports()用法及代码示例


module.syncBuiltinESMExports()

添加于:v12.12.0

module.syncBuiltinESMExports() 方法更新内置 ES Modules 的所有实时绑定以匹配 CommonJS 导出的属性。它不会在 ES Modules 中添加或删除导出的名称。

const fs = require('node:fs');
const assert = require('node:assert');
const { syncBuiltinESMExports } = require('node:module');

fs.readFile = newAPI;

delete fs.readFileSync;

function newAPI() {
  // ...
}

fs.newAPI = newAPI;

syncBuiltinESMExports();

import('node:fs').then((esmFS) => {
  // It syncs the existing readFile property with the new value
  assert.strictEqual(esmFS.readFile, newAPI);
  // readFileSync has been deleted from the required fs
  assert.strictEqual('readFileSync' in fs, false);
  // syncBuiltinESMExports() does not remove readFileSync from esmFS
  assert.strictEqual('readFileSync' in esmFS, true);
  // syncBuiltinESMExports() does not add names
  assert.strictEqual(esmFS.newAPI, undefined);
});

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 module.syncBuiltinESMExports()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。