JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 401504
  • Score
    100M100P100Q172518F
  • License MIT

Load a submodule / plugin

Package Exports

  • load-plugin

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (load-plugin) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

load-plugin Build Status Coverage Status

Load a submodule / plugin. Accepts a string and looks for files, directories, node modules, optionally global packages too.

Installation

npm:

npm install load-plugin

Usage

Say we have the following project:

project
|-- node_modules
|   |-- load-plugin
|-- package.json
|-- example
    |-- index.js

Where example/index.js looks as follows:

var loadPlugin = require('load-plugin');

console.log(loadPlugin('foo', {prefix: 'bar'}));

And the script is run:

cd example
node index.js

The following paths are checked, in order:

project/node_modules/bar-foo
project/example/node_modules/bar-foo
project/foo
project/foo.js
project/node_modules/foo
project/example/node_modules/foo

And an error is throw because foo isn’t found 😟

module.js:440
    throw err;
    ^

Error: Cannot find module 'foo'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at loadPlugin (~/project/node_modules/load-plugin/index.js:126:12)
    at Object.<anonymous> (~/project/example/index.js:3:13)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

API

loadPlugin(name[, options])

Try to load name. See how Β».

Options
  • prefix (string, optional) β€” Prefix to search for;

  • cwd (string, optional, defaults to process.cwd()) β€” Place to search in;

  • global (boolean, optional, defaults to whether global is detected) β€” Whether to look for name in global places. If this is nully, 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.

Returns

The results of requireing the first path that exists.

Throws

If requireing an existing path fails, or if no existing path exists.

loadPlugin.resolve(name[, options])

Search for name. Accepts the same parameters as loadPlugin but returns an absolute path for name instead of requiring it, and null if it cannot be found.

Algorithm

Looks in the following paths:

  • $root/node_modules/$plugin β€” If prefix is given;
  • $cwd/node_modules/$plugin β€” If prefix is given;
  • $modules/$plugin β€” If prefix is given and in global mode;
  • $root/$name;
  • $root/$name.js;
  • $root/node_modules/$name;
  • $cwd/node_modules/$name;
  • $modules/$name β€” If in global mode.

Where:

  • $cwd β€” Directory to search from (configurable);

  • $root β€” Ancestral directory of $cwd, with a package.json;

  • $name β€” Given name;

  • $plugin β€” When prefix is given, prefix and name joined together with a hyphen;

  • $modules β€” Location of globally installed npm packages.

License

MIT Β© Titus Wormer