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

resolve-npm-dependency-graph

v1.3.2

Published

Resolve npm dependency graph in-memory and support deterministic flattening of these

Downloads

6

Readme

Resolve npm package dependency graphs in-memory

This module's basic functionality is to calculate the dependency graph of an npm module. It exposes a Client class that facilitates the caching and deduplication of in-memory Package objects.

The module also provides support for the calculation of a (relatively) optimal and deterministic dependency tree that can be thought of as the equivalent of an npm package-lock.json or yarn.lock tree via a separate entrypoint require('resolve-npm-dependency-graph/dist/optimize').

Usage

const Resolver = require('resolve-npm-dependency-graph');
const CdnLoader = require('resolve-npm-dependency-graph/dist/cdnLoader');
const Optimizer = require('resolve-npm-dependency-graph/dist/optimizer');

const cdnLoader = CdnLoader.createLoader();
const client = new Resolver.Client({ packageMetadataLoader: cdnLoader });

// Load the dependency graph of npm@5 and hapi@17
const npmPkg = await client.load('npm@5');
const hapiPkg = await client.load('hapi@17');

// Now flatten the dependency graphs of these two modules into an optimized,
// deduplicated tree
const root = Optimizer.flatten([npmPkg, hapiPkg]);

API

Client

Represents an instance of the dependency graph client. Holds a cache of resolved Package instances and can be configured with a custom packageMetadataLoader function.

new Client(options)

Create an instance of the resolver client.

  • options - (required) options for the resolver client having:
    • packageInstanceCache - (optional) a custom cache for Package instances that supports the has, get and set methods of a Map
    • packageMetadataLoader - (required) a function that will take an npm package spec like hapi@17 the return a Promise that resolves to a json object having the following properties:
      • name - (required) the name of the module
      • version - (required) the version of the module
      • dependencies - (optional) a map of dependency names to semver ranges

An instance of the resolver client

load(spec)

Load the dependency graph for a given package spec where:

  • spec (required) spec for the npm package to be loaded. The types of specs supported really depend on the packageMetadataLoader.

Returns a Promise that resolves to a Package instance.

Package

Represents an instance of a loaded package having the following properties:

  • name - the name of the package
  • version - the version of the package
  • dependencies - a mapping of dependency names to required specs (dependencies from package.json)
  • children - a Map of child package names to resolved Package instances

require('resolve-npm-dependency-graph/dist/npmLoader')

Provides a packageMetadataLoader based on the npm registry api. Since the registry does not (currently) support CORS, this loader is only suitable for use from the server-side.

createLoader(options)

Create an instance of the npm package metadata loader with the following optional options:

  • registryUrl - The base url that points to an npm-compatibly registry. Defaults to 'https://registry.npmjs.org'.

Returns a loader function.

require('resolve-npm-dependency-graph/dist/cdnLoader')

Provides a packageMetadataLoader based on a public npm cdn like jsDelivr or unpkg. Both of these services should be suitable for use in a browser.

createLoader(options)

Create an instance of the npm package metadata loader with the following optional options:

  • baseUrl - The base url that points an endpoint to which a packageName@spec/package.json can be appended and will resolve to the contents of the package.json file for the best matching version of that package. Defaults to 'https://cdn.jsdelivr.net/npm' and is compatible with 'https://unpkg.com'.

Returns a loader function.

require('resolve-npm-dependency-graph/dist/optimizer')

Provides a tool to build an optimized, deterministic module tree suitable for planning a node_modules directory structure.

flatten(pkgs)

Flatten a set of Package instances into a tree of DependencyTreeNodes where:

  • pkgs - is an array of Package instances.

Returns a 'root' DependencyTreeNode with a dummy pkg property that has the tree of modules as children.

DependencyTreeNode

Represents a node in the flattened dependency tree and has the following properties:

  • pkg - the instance of the Package at that node in the tree
  • children - a Map of the names of child DependencyTreeNodes to their instances
  • parent - a reference back to the node's parent