GeezerVison is showing github.com — module: add builtinModules · nodejs/node@cdf4a9c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/modules.md +24Lines changed: 24 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -633,5 +633,29 @@ object. Since `require()` returns the `module.exports`, and the `module` is |
633 | 633 | typically *only* available within a specific module's code, it must be |
634 | 634 | explicitly exported in order to be used. |
635 | 635 |
|
| 636 | +## The `Module` Object |
| 637 | + |
| 638 | +<!-- YAML |
| 639 | +added: v0.3.7 |
| 640 | +--> |
| 641 | + |
| 642 | +* {Object} |
| 643 | + |
| 644 | +Provides general utility methods when interacting with instances of |
| 645 | +`Module` -- the `module` variable often seen in file modules. Accessed |
| 646 | +via `require('module')`. |
| 647 | + |
| 648 | +### module.builtinModules |
| 649 | +<!-- YAML |
| 650 | +added: REPLACEME |
| 651 | +--> |
| 652 | + |
| 653 | +* {string[]} |
| 654 | + |
| 655 | +A list of the names of all modules provided by Node.js. Can be used to verify |
| 656 | +if a module is maintained by a third-party module or not. |
| 657 | + |
| 658 | +[`__dirname`]: #modules_dirname |
| 659 | +[`__filename`]: #modules_filename |
636 | 660 | [`Error`]: errors.html#errors_class_error |
637 | 661 | [module resolution]: #modules_all_together |
Collapse file
Copy file name to clipboardExpand all lines: lib/module.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,9 +48,16 @@ function Module(id, parent) { |
48 | 48 | } |
49 | 49 | module.exports = Module; |
50 | 50 |
|
| 51 | +const builtinModules = Object.keys(NativeModule._source) |
| 52 | + .filter(NativeModule.nonInternalExists); |
| 53 | + |
| 54 | +Object.freeze(builtinModules); |
| 55 | +Module.builtinModules = builtinModules; |
| 56 | + |
51 | 57 | Module._cache = {}; |
52 | 58 | Module._pathCache = {}; |
53 | 59 | Module._extensions = {}; |
| 60 | + |
54 | 61 | var modulePaths = []; |
55 | 62 | Module.globalPaths = []; |
56 | 63 |
|
|
Collapse file
Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const { builtinModules } = require('module'); |
| 5 | + |
| 6 | +// Includes modules in lib/ (even deprecated ones) |
| 7 | +assert(builtinModules.includes('http')); |
| 8 | +assert(builtinModules.includes('sys')); |
| 9 | + |
| 10 | +// Does not include internal modules |
| 11 | +assert.deepStrictEqual( |
| 12 | + builtinModules.filter((mod) => mod.startsWith('internal/')), |
| 13 | + [] |
| 14 | +); |