GeezerVison is showing github.com — module: add builtinModules · nodejs/node@95778d5
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 +22Lines changed: 22 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -817,6 +817,28 @@ The `module.require` method provides a way to load a module as if |
817 | 817 | `module` is typically *only* available within a specific module's code, it must |
818 | 818 | be explicitly exported in order to be used. |
819 | 819 |
|
| 820 | +## The `Module` Object |
| 821 | + |
| 822 | +<!-- YAML |
| 823 | +added: v0.3.7 |
| 824 | +--> |
| 825 | + |
| 826 | +* {Object} |
| 827 | + |
| 828 | +Provides general utility methods when interacting with instances of |
| 829 | +`Module` -- the `module` variable often seen in file modules. Accessed |
| 830 | +via `require('module')`. |
| 831 | + |
| 832 | +### module.builtinModules |
| 833 | +<!-- YAML |
| 834 | +added: REPLACEME |
| 835 | +--> |
| 836 | + |
| 837 | +* {string[]} |
| 838 | + |
| 839 | +A list of the names of all modules provided by Node.js. Can be used to verify |
| 840 | +if a module is maintained by a third-party module or not. |
| 841 | + |
820 | 842 | [`__dirname`]: #modules_dirname |
821 | 843 | [`__filename`]: #modules_filename |
822 | 844 | [`Error`]: errors.html#errors_class_error |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/module.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,12 @@ function Module(id, parent) { |
74 | 74 | } |
75 | 75 | module.exports = Module; |
76 | 76 |
|
| 77 | +const builtinModules = Object.keys(NativeModule._source) |
| 78 | + .filter(NativeModule.nonInternalExists); |
| 79 | + |
| 80 | +Object.freeze(builtinModules); |
| 81 | +Module.builtinModules = builtinModules; |
| 82 | + |
77 | 83 | Module._cache = Object.create(null); |
78 | 84 | Module._pathCache = Object.create(null); |
79 | 85 | Module._extensions = Object.create(null); |
|
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 | +); |