load-from-cwd-or-npm
Load a module from either CWD or npm
CLI directory
const loadFromCwdOrNpm = require('load-from-cwd-or-npm');
(async () => {
require('validate-npm-package-name');
const RegistryClient = await loadFromCwdOrNpm('validate-npm-package-name');
})();
Installation
Use npm.
npm install load-from-cwd-or-npm
API
const loadFromCwdOrNpm = require('load-from-cwd-or-npm');
loadFromCwdOrNpm(moduleId, [compareFn])
moduleId: string
(a module ID without path separators (/
, \\
))
compareFn: Function
(a function to compare two package versions)
Return: Promise<any>
It loads a module with the given module ID from either of these two directories:
node_modules
in the current working directorynode_modules
in the directory where npm
CLI is installed
If the module ins't installed in CWD but included in the npm CLI dependencies, it loads the module from npm CLI directory.
(async () => {
const nopt = await loadFromCwdOrNpm('nopt');
})();
If the module ins't included in the npm CLI dependencies but installed in CWD, it loads the module from CWD.
(async () => {
const eslint = await loadFromCwdOrNpm('eslint');
})();
If the module exists in both directories, it compares their package versions and loads the newer one.
(async () => {
const rimraf = await loadFromCwdOrNpm('rimraf');
})();
The returned promise will be fulfilled with the loaded module, or rejected when it fails to find the module from either directories.
compareFn(cwdPackageVersion, npmPackageVersion)
Default: node-semver's gte
method
Used as a comparison function when a module with the given ID exists in both directories.
It takes two string
arguments, package versions of the CWD one and the npm dependency one. the former will be loaded when compareFn
returns true
, otherwise the latter will be loaded.
const loadFromCwdOrNpm = require('load-from-cwd-or-npm');
const semver = require('semver');
loadFromCwdOrNpm('rimraf', semver.lt);
License
ISC License © 2017 - 2018 Shinnosuke Watanabe