From 46f19efa66923a56b1e33aa9d208a83c26cb989e Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Fri, 24 Nov 2017 16:29:38 -0500 Subject: [PATCH 1/3] module: add builtinModules Provides list of all builtin modules in Node. Includes modules of all types: - prefixed (ex: _tls_common) - deprecated (ex: sys) - regular (ex: vm) --- lib/module.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/module.js b/lib/module.js index 396ca9021ef360..e1279b75bc3271 100644 --- a/lib/module.js +++ b/lib/module.js @@ -74,6 +74,12 @@ function Module(id, parent) { } module.exports = Module; +const builtinModules = Object.keys(NativeModule._source) + .filter(NativeModule.nonInternalExists); + +Object.freeze(builtinModules); +Module.builtinModules = builtinModules; + Module._cache = Object.create(null); Module._pathCache = Object.create(null); Module._extensions = Object.create(null); From 846dea636e041b6966769baa32ec9c0fba6ee1b6 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Mon, 27 Nov 2017 19:03:09 -0500 Subject: [PATCH 2/3] [squash] add tests and docs --- doc/api/modules.md | 22 ++++++++++++++++++++++ test/parallel/test-module-builtin.js | 14 ++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/parallel/test-module-builtin.js diff --git a/doc/api/modules.md b/doc/api/modules.md index 0b0e62b5bc3402..f9f311d241ce92 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -817,6 +817,28 @@ The `module.require` method provides a way to load a module as if `module` is typically *only* available within a specific module's code, it must be explicitly exported in order to be used. +## The `Module` Object + + + +* {Object} + +Provides general utility methods when interacting with instances of +`Module` -- the `module` variable often seen in file modules. Accessed +via `require('module')`. + +### module.builtinModules + + +* {string[]} + +A list of the names of all modules provided by Node.js. Can be used to verify +if a module is maintained by a third-party module or not. + [`__dirname`]: #modules_dirname [`__filename`]: #modules_filename [`Error`]: errors.html#errors_class_error diff --git a/test/parallel/test-module-builtin.js b/test/parallel/test-module-builtin.js new file mode 100644 index 00000000000000..5a0e4bdd57af6e --- /dev/null +++ b/test/parallel/test-module-builtin.js @@ -0,0 +1,14 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { builtinModules } = require('module'); + +// Includes modules in lib/ (even deprecated ones) +assert(builtinModules.includes('http')); +assert(builtinModules.includes('sys')); + +// Does not include internal modules +assert.deepStrictEqual( + builtinModules.filter((mod) => { return mod.startsWith('internal/'); }), + [] +); From 14460415999e5525e5a06948f6b38f70b49a935e Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Mon, 27 Nov 2017 19:26:11 -0500 Subject: [PATCH 3/3] nit: addaleax comment --- test/parallel/test-module-builtin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-module-builtin.js b/test/parallel/test-module-builtin.js index 5a0e4bdd57af6e..3897d71ecf4405 100644 --- a/test/parallel/test-module-builtin.js +++ b/test/parallel/test-module-builtin.js @@ -9,6 +9,6 @@ assert(builtinModules.includes('sys')); // Does not include internal modules assert.deepStrictEqual( - builtinModules.filter((mod) => { return mod.startsWith('internal/'); }), + builtinModules.filter((mod) => mod.startsWith('internal/')), [] );