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 targos committed May 1, 2021
1 parent 099d776 commit 5770ae0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions doc/api/module.md
Expand Up @@ -87,21 +87,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 5770ae0

Please sign in to comment.