如何在 Node.js 目录下查找内置模块

How to find built-in modules under the Node.js directory

本文关键字:查找 内置 模块 Node js      更新时间:2023-09-26

Node.js 有一个内置模块的列表,例如

os, path

我知道这些模块被编译成二进制文件。但是,安装 Node.js 后,我在 Node.js 目录下找不到模块。

有人知道模块的位置吗?

您是正确的,因为它们被编译成二进制文件 - 作为 JavaScript。这可以通过单独运行 https://nodejs.org/的node二进制文件来证明 - 所有标准库都可用。

以下是二进制文件中(非连续)JavaScript的证据:

$ strings $(command -v node) | grep -E ''b(os|path)'b' 
...<cut>
const path = require
('path');
const os = require('os');
      historyPath = path.join(os.homedir(), '.node_repl_history');
    const historyData = repl.history.join(os.EOL);
        'a valid, user-writable path to enable.'n'
        err.path = self.spawnfile;
  this.path = null;
// Special case for a simple path URL
// are the ones that are *expected* to be seen, so we fast-path them.
    // Try fast path regexp
      this.path = rest;
  // resolution will treat //foo/bar as host=foo,path=bar because that's
    // http://a@b?@c => user:a host:b path:/?@c
      // http://a@b/c@d => host:b auth:a path:/c@d
    this.path = p + s;

您可以使用以下内容来获取节点中的built_in模块.js

require('repl')._builtinLibs