代码之家  ›  专栏  ›  技术社区  ›  ThomasReggi

在哪里可以获得所有本机节点模块的数组?

  •  1
  • ThomasReggi  · 技术社区  · 6 年前

    我正在寻找一种方法来获得所有可用节点模块的列表。动态地获取它会很有趣,因为不同的版本或将来的版本可能会添加或不推荐模块。

    4 回复  |  直到 6 年前
        1
  •  4
  •   vinoth h    6 年前

    您可以使用此代码获取所有全局安装模块的列表:

    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);
    

    如果需要内置模块,请使用

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

    参考这个 doc .

        2
  •  3
  •   Itay Maman    6 年前

    如果使用的是Node version>8.11.3,建议使用 builtinModules 财产 module 对象,如下所示:

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

    更多细节: https://nodejs.org/api/modules.html#modules_module_builtinmodules

        3
  •  0
  •   TGrif    6 年前

    您可以获得如下本机模块的列表:

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

    这样,您就可以在特定版本的Nodejs中获得本机模块。

        4
  •  -2
  •   ThomasReggi    6 年前
    const m = [
        "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"
    ];