Socket
Socket
Sign inDemoInstall

load-from-cwd-or-npm

Package Overview
Dependencies
14
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    load-from-cwd-or-npm

Load a module from either CWD or npm CLI directory


Version published
Weekly downloads
595
decreased by-40.68%
Maintainers
1
Install size
113 kB
Created
Weekly downloads
 

Readme

Source

load-from-cwd-or-npm

NPM version Build Status Build status Coverage Status

Load a module from either CWD or npm CLI directory

const loadFromCwdOrNpm = require('load-from-cwd-or-npm');

// $ npm ls validate-npm-package-name
// > └── (empty)

(async () => {
  require('validate-npm-package-name'); // throws a `MODULE_NOT_FOUND` error
  const RegistryClient = await loadFromCwdOrNpm('validate-npm-package-name'); // doesn't throw
})();

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:

  1. node_modules in the current working directory
  2. node_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.

// $ npm ls nopt
// > └── (empty)

loadFromCwdOrNpm('nopt').then(nopt => {
  nopt; //=> {[Function: nopt], clean: [Function: clean] ...}
});

If the module ins't included in the npm CLI dependencies but installed in CWD, it loads the module from CWD.

// $ npm ls eslint
// > └── eslint@2.13.1

// npm doesn't depend on `eslint` module.
loadFromCwdOrNpm('eslint').then(eslint => {
  eslint; //=> {linter: EventEmitter { ... }, ...}
});

If the module exists in both directories, it compares their package versions and loads the newer one.

// $ npm ls rimraf
// > └── rimraf@1.0.0

loadFromCwdOrNpm('rimraf').then(rimraf => {
  rimraf; // Loaded from npm CLI directory because the CWD version is older
});

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);
// inverse to the default behavior (the older will be loaded)

License

Copyright (c) 2015 - 2017 Shinnosuke Watanabe

Licensed under the MIT License.

Keywords

FAQs

Last updated on 08 Jun 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc