Matt Elson
Matt Elson

Reputation: 4355

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

Node.js has a list of built-in modules,e.g.,

os, path

I know these modules are compiled into the binary. However, after installed Node.js, I can't find the modules under the Node.js directory.

Could someone know where the modules are located?

Upvotes: 1

Views: 1693

Answers (2)

sumit_suthar
sumit_suthar

Reputation: 462

you can use following to get built_in modules in node.js

require('repl')._builtinLibs

Upvotes: 4

Andy
Andy

Reputation: 17781

You are correct in that they are compiled into the binary - as JavaScript. This can be proven by running just the node binary from https://nodejs.org/ in isolation - all the standard libraries are available.

Here's evidence of (non-contiguous) JavaScript in the binary:

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

Upvotes: 1

Related Questions