5

I am looking for a way to get a list of all available node modules. It would be interesting to get this dynamically, because different versions or future versions may add or deprecate modules.

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

4 Answers4

8

If you are using Node version > 8.11.3, the recommended way to achieve that is to use the builtinModules property of the module object, as follows:

const builtins = require('module').builtinModules;

Further details: https://nodejs.org/api/modules.html#modules_module_builtinmodules

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
4

You can use this code to get the list of all globally installed modules:

function exec(callback) {
  require('child_process').exec('npm ls -g --depth=0 --json', function(err, data, stderr) {
    if (err) return cb(err)
    callback(data);
  });
}

function get_modules(callback) {
    var res = [];
    exec(function(d) {
        d = JSON.parse(d);
        var m = d.dependencies;     
        for(key in m) res.push(key);
        callback(res);
    });
}

get_modules(console.log);

If you want built-in modules , use

console.log(require("module").builtinModules)

Refer this doc.

vinoth h
  • 511
  • 3
  • 11
0

Programmatically get the list (only works on node 8 and above)

console.log(require("module").builtinModules)

Latest List:

const builtins = [
  '_http_agent',       '_http_client',        '_http_common',
  '_http_incoming',    '_http_outgoing',      '_http_server',
  '_stream_duplex',    '_stream_passthrough', '_stream_readable',
  '_stream_transform', '_stream_wrap',        '_stream_writable',
  '_tls_common',       '_tls_wrap',           'assert',
  'async_hooks',       'buffer',              'child_process',
  'cluster',           'console',             'constants',
  'crypto',            'dgram',               'dns',
  'domain',            'events',              'fs',
  'fs/promises',       'http',                'http2',
  'https',             'inspector',           'module',
  'net',               'os',                  'path',
  'perf_hooks',        'process',             'punycode',
  'querystring',       'readline',            'repl',
  'stream',            'string_decoder',      'sys',
  'timers',            'tls',                 'trace_events',
  'tty',               'url',                 'util',
  'v8',                'vm',                  'worker_threads',
  'zlib'
]

Here's an older list:

const builtins = [
  'assert',         'buffer',   'child_process',
  'cluster',        'console',  'constants',
  'crypto',         'dgram',    'dns',
  'domain',         'events',   'fs',
  'http',           'https',    'module',
  'net',            'os',       'path',
  'process',        'punycode', 'querystring',
  'readline',       'repl',     'stream',
  'string_decoder', 'sys',      'timers',
  'tls',            'tty',      'url',
  'util',           'vm',       'zlib'
];

Here's a function that attempts to work across all versions of node:

export const builtins = () => {
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const result = require('module');
    // eslint-disable-next-line node/no-unsupported-features/node-builtins
    return result.builtinModules;
  } catch (e) {
    // prettier-ignore
    return [
      'assert',         'buffer',   'child_process',
      'cluster',        'console',  'constants',
      'crypto',         'dgram',    'dns',
      'domain',         'events',   'fs',
      'http',           'https',    'module',
      'net',            'os',       'path',
      'process',        'punycode', 'querystring',
      'readline',       'repl',     'stream',
      'string_decoder', 'sys',      'timers',
      'tls',            'tty',      'url',
      'util',           'vm',       'zlib'
    ];
  }
};

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • @ruakh no I think it's useful to have an array of all of them same as this list here https://stackoverflow.com/questions/14168703/crypto-algorithm-list – ThomasReggi Jan 02 '19 at 10:02
  • Maybe it would be better to add this list in the question or in an answer, the same way as in the link you refer to. – TGrif Jan 02 '19 at 17:58
-1

You can get the list of native modules like this:

const repl = require('repl') 
console.log(repl._builtinLibs)

This way you get the native modules available in your specific version of Nodejs.

TGrif
  • 5,725
  • 9
  • 31
  • 52