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

load-plugin

v6.0.3

Published

Load a submodule, plugin, or file

Downloads

1,658,646

Readme

load-plugin

Build Coverage Downloads

Load a submodule, plugin, or file.

Contents

What is this?

This package is useful when you want to load plugins. It resolves things like Node.js does, but supports a prefix (when given a prefix remark and the user provided value gfm it can find remark-gfm), can load from several places, and optionally global too.

When to use this?

This package is particularly useful when you want users to configure something with plugins. One example is remark-cli which can load remark plugins from configuration files.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install load-plugin

Use

Say we’re in this project (with dependencies installed):

import {loadPlugin, resolvePlugin} from 'load-plugin'

console.log(await resolvePlugin('lint', {prefix: 'remark'}))
// => 'file:///Users/tilde/Projects/oss/load-plugin/node_modules/remark-lint/index.js'

console.log(
  await resolvePlugin('validator-identifier', {prefix: '@babel/helper'})
)
// => 'file:///Users/tilde/Projects/oss/load-plugin/node_modules/@babel/helper-validator-identifier/lib/index.js'

console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
// => 'file:///Users/tilde/Projects/oss/load-plugin/index.js'

console.log(await loadPlugin('lint', {prefix: 'remark'}))
// => [Function: remarkLint]

API

This package exports the identifiers loadPlugin and resolvePlugin. There is no default export.

It exports the TypeScript types LoadOptions and ResolveOptions.

loadPlugin(name[, options])

Import name from from (and optionally the global node_modules directory).

Uses the Node.js resolution algorithm (through import-meta-resolve) to resolve CJS and ESM packages and files.

If a prefix is given and name is not a path, $prefix-$name is also searched (preferring these over non-prefixed modules). If name starts with a scope (@scope/name), the prefix is applied after it: @scope/$prefix-name.

Parameters
  • name (string) — specifier
  • options (LoadOptions, optional) — configuration
Returns

Promise to a whole module or specific export (Promise<unknown>).

resolvePlugin(name[, options])

Resolve name from from.

Parameters
  • name (string) — specifier
  • options (ResolveOptions, optional) — configuration
Returns

Promise to a file URL (Promise<string>).

LoadOptions

Configuration for loadPlugin (TypeScript type).

This type extends ResolveOptions and adds:

Fields
  • key (boolean or string, default: 'default') — identifier to take from the exports; for example when given 'x', the value of export const x = 1 will be returned; when given 'default', the value of export default … is used, and when false the whole module object is returned

ResolveOptions

Configuration for resolvePlugin (TypeScript type).

Fields
  • from (Array<URL | string> | URL | string, optional) — place or places to search from; defaults to the current working directory
  • global (boolean, default: whether global is detected) — whether to look for name in global places; if this is nullish, load-plugin will detect if it’s currently running in global mode: either because it’s in Electron or because a globally installed package is running it; note that Electron runs its own version of Node instead of your system Node, meaning global packages cannot be found, unless you’ve set-up a prefix in your .npmrc or are using nvm to manage your system node
  • prefix (string, optional) — prefix to search for

Compatibility

This projects is compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, load-plugin@6, compatible with Node.js 16.

Security

This package reads the file system and imports things into Node.js.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer