load-plugin
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 :worried:
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 require
ing the first path that exists.
Throws
If require
ing 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