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

get-modules-graph

v0.0.9

Published

Get and traverse graph of ECMAScript/TypeScript modules

Downloads

169

Readme

get-modules-graph

NPM version minzipped size code style: prettier Conventional Commits License MIT

Get and traverse graph of ECMAScript/TypeScript modules.

Basic example

import {dirname, join, sep} from 'node:path';

import {getModulesGraph} from 'get-modules-graph';

const modulesGraph = await getModulesGraph<number>({
  /**
   * Chooses an index module in a directory by the resolved path and directory path.
   * Throws an error if a directory and not a file is selected.
   */
  chooseIndexModule: (resolvedPath, directoryPath, directoryContent) => {
    if ('index.ts' in directoryContent) {
      return 'index.ts';
    }

    throw new Error(
      `Cannot choose index module in directory \`${directoryPath}\` for \`${resolvedPath}\``,
    );
  },
  /**
   * Chooses a module (or directory) in a directory by the resolved path
   * to a module in that directory.
   * If a directory is chosen, the choosing process will continue in a `chooseIndexModule` call
   * already for the contents of that chosen directory.
   */
  chooseModule: (resolvedPath, parsedPath, directoryContent) => {
    const fileName = `${parsedPath.base}.ts`;

    if (fileName in directoryContent) {
      return fileName;
    }

    throw new Error(`Cannot choose module for \`${resolvedPath}\``);
  },
  /**
   * List of relative paths to directories for recursive traversal in depth
   * and by their dependencies.
   */
  directories: [],
  /**
   * If `true`, then `import(...)` expression with static strings will be taken into account
   * (as importing the namespace of the entire module).
   */
  includeDynamicImports: true,
  /**
   * If `true`, then `require(...)` expression with static strings will be taken into account
   * (as importing the namespace of the entire module).
   */
  includeRequires: true,
  /**
   * List of relative paths to modules for recursive traversal by their dependencies.
   */
  modules: ['./index.ts'],
  /**
   * The callback on completing (adding) dependencies to a module.
   * Called when all module dependencies have been recursively completed.
   * Always called before the `onAddModule` callback.
   * Should return the data obtained from the dependencies of the module,
   * which will be stored in the `dependenciesData` module field.
   */
  onAddDependencies: () => {},
  /**
   * The callback on adding a module. Called as soon as the module has been parsed
   * and added to the module graph (without its own dependencies).
   * Should return the data obtained from the source code of the module,
   * which will be stored in the `sourceData` module field.
   */
  onAddModule: (_module, source) => source.length,
  /**
   * Resolves raw path to dependency module (after the `from` keyword) to relative path
   * from current working directory or to bare path to the package.
   * The returned relative path to the module must begin with a dot,
   * otherwise it will be considered the path to the package.
   * The returned path must use `path.sep` as path segment separator.
   * If returns `undefined`, dependency module is skipped.
   */
  resolvePath: (modulePath, rawPath) => {
    if (rawPath[0] === '.') {
      return `.${sep}${join(dirname(modulePath), rawPath)}`;
    }

    return rawPath;
  },
  /**
   * If `true`, then we respect string literals when parsing, that is,
   * we ignore the expressions inside them (but parsing will be a little slower).
   */
  respectStringLiterals: true,
  /**
   * If returns `true`, directory is skipped in recursive directories traversal.
   */
  skipDirectory: () => false,
  /**
   * If returns `true`, module is skipped in recursive directories traversal.
   */
  skipModule: () => false,
  /**
   * Transforms source of module.
   */
  transformSource: (_path, source) => source,
});

console.log(modulesGraph.circularDependencies);
console.log(modulesGraph.errors);
console.log(modulesGraph.modules);
console.log(modulesGraph.packages);

Install

Requires node version 10 or higher:

npm install get-modules-graph

get-modules-graph works in any environment that supports ES2018 (because package uses RegExp Named Capture Groups).

License

MIT