admin管理员组

文章数量:1326274

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.

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.

Share Improve this question asked Jan 2, 2019 at 8:52 ThomasReggiThomasReggi 59.6k97 gold badges258 silver badges459 bronze badges 1
  • cd /usr/share/doc/nodejs/api | find ./ -name "*.md" – Vadim Hulevich Commented Jan 2, 2019 at 9:34
Add a ment  | 

4 Answers 4

Reset to default 8

If you are using Node version > 8.11.3, the remended 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/api/modules.html#modules_module_builtinmodules

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.

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.

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_mon',
  '_http_ining',    '_http_outgoing',      '_http_server',
  '_stream_duplex',    '_stream_passthrough', '_stream_readable',
  '_stream_transform', '_stream_wrap',        '_stream_writable',
  '_tls_mon',       '_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'
    ];
  }
};

本文标签: javascriptWhere can I get an array of all native node modulesStack Overflow