npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

module-dep-graph

v0.1.0

Published

Automatically track a dependency graph of all commonjs modules in an application

Downloads

4

Readme

module-dep-graph

Automatically track a dependency graph of all commonjs modules in an application

Installation

npm i module-dep-graph

Usage

Let's say we have the following source code:

// A.js
var B = require('./B');
var C = require('./C');
module.exports = 'I am module A!';
// B.js
var C = require('./C');
var D = require('./D');
module.exports = 'I am module B!';
// C.js
module.exports = 'I am module C!';
// D.js
module.exports = 'I am module D!';

In the very first file that your program executes, you will want to load module-dep-graphs:

require('module-dep-graphs'); 

Then, at any point in the execution of your program, you can dynamically pull the dependency graph of all commonjs modules:

var A = require('./A');
var deps = require('module-dep-graph');

At this point, calling console.log(deps.graph) would produce:

{ '/absolute/path/to/A.js':
   { key: '/absolute/path/to/A.js',
     deps: [ Module[B], Module[C] ],
     reverse: [] },
  '/absolute/path/to/B.js':
   { key: '/absolute/path/to/B.js',
     deps: [ Module[C], Module[D] ],
     reverse: [ Module[A] ] },
  '/absolute/path/to/C.js':
   { key: '/absolute/path/to/C.js',
     deps: [],
     reverse: [ Module[B], Module[A] ] },
  '/absolute/path/to/D.js':
   { key: '/absolute/path/to/D.js',
     deps: [],
     reverse: [ Module[B] ] } }

Where we see that deps.graph is a cyclic graph represented as a set of key-value pairs of the absolute paths to a module's file, and a node on the graph where each node has:

  • key: the absolute path of the module's source file
  • deps: an array of references pointing to the nodes in the graph of each module that this node directly depends on.
  • reverse: an array of references pointing to the nodes in the graph that directly depend on that node.

Because of the reverse array, this graph represents an undirected graph of module dependencies.

Additionally, the module-dep-graph export provides two helpful methods:

.dependentsOf(key)

Returns an array of keys (absolute paths) for every module that the provided module key is a dependency of, both directly and indirectly (through sub-dependencies). This is done by walking the graph using the node's reverse property.

deps.dependentsOf(require.resolve('./C'))
// [ '/absolute/path/to/A.js', '/absolute/path/to/B.js' ]

.dependenciesOf(key)

Returns an array of keys (absolute paths) for every module that the provided module key depends on, both directly and indirectly (through sub-dependencies). This is done by walking the graph using the node's deps property.

deps.dependenciesOf(require.resolve('./A'))
// [ '/absolute/path/to/B.js', '/absolute/path/to/C.js', '/absolute/path/to/D.js' ]

How does it work?

This module works by using Node's internal module module, and wrapping some of it's internal methods. Namely, the Module._load method. Check the source of this repo, as well as the source of module for more insight.

License

MIT