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

generate-call-graph

v0.0.3

Published

Generate Call Graph in Node/Electron project

Downloads

2

Readme

This tiny tool is to help generate a complete module relationship graph for Node.js/Electron project.

How to use

const generate = require('generate-call-graph');
const path = require('path');

const dot = generate(require.main, {
  sep: path.sep,
  title: 'Title of Graph',
  ignore: ['path', 'generate-call-graph'],
});

require('fs').writeFileSync('output.dot', dot, 'utf8');

Please notice that API only provides string as result. You will need graphviz to generate the final SVG/PNG graph out of this dot file. For example, run the following script:

# generate PNG version
dot -Tpng -O output.dot
# generate SVG version
dot -Tsvg -O output.dot

Time Cost of Loading Module

This package also contains a tiny utility that helps you to determine the loading time cost of each module. To use this, first load the module as early as possible (for example, load it in preload script in Electron):

require('generate-call-graph/enhancer');

Then, you could generate a graph with time cost labeled:

const generate = require('generate-call-graph');
const path = require('path');

const dot = generate(require.main, {
  sep: path.sep,
  title: 'Title of Graph',
  ignore: ['path', 'generate-call-graph'].map(require.resolve),
  labelTime: true,
  threshold: {
    warn: 50,
    error: 100,
  },
});

require('fs').writeFileSync('output.dot', dot, 'utf8');

In the example above, it will also mark the line as orange if it costs more than 50ms to load, and mark it as red if it costs more than 100ms. Easy to illustrate the potential issue when initializing the app.

Options

Following are possible options to modify the behavior of API:

  • sep: required / string, simply pass require('path').sep will be enough.

    This API is design to be runnable in web environment, thus need to pass environment related info (such as path.sep from outside);

  • title: required / string, title of the generate graph.

  • ignore: optional / string[], list of IDs that should be ignored from generating the graph. In Node.js, each module as a unique ID, which is equal to the path of that file.

  • labelTime: optional / boolean, whether or not the graph should label cost time on each line.

    Please notice that: 1) one module might be loaded multiple times, only the first time will be labeled by time cost, for the rest of usages, cache will be used instead of loading it again; 2) you will need to load generate-call-graph/enhancer utility file as early as possible to get those info beforehand.

  • threshold: optional / { warn?: number; error?: number }, the threshold to warn the loading of modules based on time cost.

    Default threshold are 50ms for warning (orange color) and 100ms for error (red color). You can overwrite these two thresholds using this option.