Skip to content

Commit

Permalink
doc: fix module syncBuiltinESMExports example
Browse files Browse the repository at this point in the history
Fix failing code and add explanations of each assert.

PR-URL: #34284
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
bmacnaughton authored and danielleadams committed Jan 12, 2021
1 parent b6f74b0 commit 153be6c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,29 @@ does not add or remove exported names from the [ES Modules][].
```js
const fs = require('fs');
const assert = require('assert');
const { syncBuiltinESMExports } = require('module');

fs.readFile = null;
fs.readFile = newAPI;

delete fs.readFileSync;

fs.newAPI = function newAPI() {
function newAPI() {
// ...
};
}

fs.newAPI = newAPI;

syncBuiltinESMExports();

import('fs').then((esmFS) => {
assert.strictEqual(esmFS.readFile, null);
assert.strictEqual('readFileSync' in fs, true);
// 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);
});
```
Expand Down

0 comments on commit 153be6c

Please sign in to comment.