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

package-import

v1.0.0-1

Published

Imports ESM packages hosted via HTTP by referencing its package.json

Downloads

1

Readme

Package Import

Import ESM packages from registries in the browser.

Quick Usage

Package Import provides a simple interface for importing packages using importmaps and registrymaps to pull packages from their various sources.

import PackageImport from "package-import";

const importer = new PackageImport();

await importer.get('my-package');

By default, if no importmap is defined and no modulePath is specified, Package Import will look at the root of the current website for a node_modules directory.

Options

new PackageImport(modulePath, {
  importMap, // optional
  registryMap, // optional
  dom: true, // optional
});

string modulePath: Module Resolution Path

The default module resolution path is set to node_modules, relative to the base URL of the current site.

To specify the module resolution path, pass a URL to PackageImport:

const importer = new PackageImport("https://unpkg.com/");
await importer.get("my-package");

This will locate the package at https://unpkg.com/my-package/ and import the package's main script.

object importMap: Importmaps

An importmap can be specified in the constructor, alleviating any need to add additional script tags on the page:

const importer = new PackageImport("https://unpkg.com/", {
  importMap: {
    "imports": {
      "my-package": "https://some-other-registry.com/my-package/index.js"
    }
  }
});

await importer.get("my-package");

In the example above, my-package always resolving to https://some-other-registry.com/my-package/index.js.

object registryMap: Registrymaps

Registrymaps are similar to importmaps, except that Package Import will attempt to resolve the package through its Script Resolution model by first fetching the module's package.json file to discover the main script.

const importer = new PackageImport("/node_modules", {
  registryMap: {
    "registries": {
      "unpkg": "https://unpkg.com/"
    },
    "imports": {
      "my-package": "unpkg",
    }
  }
});

await importer.get("my-package");

In the example above, my-package will be resolved by fetching the package.json from the package that lives at https://unpkg.com/my-package/.

bool dom = true: DOM Parsing

In the event that DOM parsing is not wanted, the dom option can be overridden to false. This will prevent the importmap and registrymap from scanning the DOM for scripts to use when resolving imports.

<script type="importmap">
{
  "imports": {
    "my-package": "https://unpkg.com/my-package/index.js"
  }
}
</script>
const importer = new PackageImport("/node_modules", { dom: false });
await importer.get("my-package");

In the example above, the module will be resolved from /node_modules/my-package instead of pulling it from unpkg.

script type="importmap"

Package Import will attempt to parse all scripts with type="importmap" on the page and use it as an importmap unless dom is set to false.

For example, the following:

<script type="importmap">
{
  "imports": {
    "my-package": "https://unpkg.com/my-package/index.js"
  }
}
</script>

Will result in my-package always resolving to https://unpkg.com/my-package/index.js.

script type="registrymap"

While non-standard, Package Import will attempt to parse all scripts with type="registrymap" on the page and use it as a registrymap.

For example, the following:

<script type="registrymap">
{
  "registries": {
    "unpkg": "https://unpkg.com/"
  },
  "imports": {
    "my-package": "unpkg",
  }
}
</script>

The example above will retrieve my-package by fetching the package.json from the package that lives at https://unpkg.com/my-package/.

Script Resolution

Package Import attempts to determine the correct script to import through a series of steps:

When calling await importer.get("my-package"), Package Import will walk through the following steps:

  1. Search any importmap for "my-package". If it exists, treat it like a direct URL.
  2. Search any registrymap for "my-package". If it exists, use it to resolve the package.json.
  3. Use the modulePath to resolve the package.json.

If the package.json needs to be resolved, the script is determined by using checking the main property in the package.json, or falling back to the browser property in the package.json.

Specifying Versions

Modules with an accompanying registrymap will attempt to resolve at the specified version.

const importer = new PackageImport("/node_modules", {
  registryMap: {
    "registries": {
      "unpkg": "https://unpkg.com/"
    },
    "imports": {
      "my-package": "unpkg",
    }
  }
});

await importer.get("[email protected]");
await importer.get("[email protected]");

In the example above, my-package will be resolved by fetching the package.json from the package that lives at https://unpkg.com/[email protected]/. However, unmapped-pkg will resolve using the default module resolution path at: /node_modules/unmapped-pkg without the version included in the fetch URL.