當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。