# load-from-cwd-or-npm

> Load a module from either CWD or npm CLI directory

Latest version **3.0.4** (published 2019-07-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install load-from-cwd-or-npm
pnpm add load-from-cwd-or-npm
yarn add load-from-cwd-or-npm
bun add load-from-cwd-or-npm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.4 |
| Published | 2019-07-09 |
| First published | 2015-12-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Shinnosuke Watanabe |
| Maintainers | shinnn |
| Keywords | load, resolve, require, fallback, failsafe, cwd, npm, module, compare, comparison, find, promise, promises, then |

## Links

- npm: https://www.npmjs.com/package/load-from-cwd-or-npm
- Repository: https://github.com/shinnn/load-from-cwd-or-npm
- Homepage: https://github.com/shinnn/load-from-cwd-or-npm#readme
- Issues: https://github.com/shinnn/load-from-cwd-or-npm/issues
- npm.io page: https://npm.io/package/load-from-cwd-or-npm

## Dependencies (4)

- [optional](https://npm.io/package/optional.md) ^0.1.4
- [npm-cli-dir](https://npm.io/package/npm-cli-dir.md) ^3.0.1
- [resolve-from-npm](https://npm.io/package/resolve-from-npm.md) ^3.1.0
- [inspect-with-kind](https://npm.io/package/inspect-with-kind.md) ^1.0.5

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 3.0.4 (latest) — 2019-07-09
- 4.0.0-0 (next) — 2019-07-09
- 3.0.1 — 2018-07-10
- 3.0.0 — 2018-06-18
- 3.0.0-0 — 2018-06-12
- 2.2.2 — 2018-06-12
- 2.2.1 — 2017-06-26
- 2.2.0 — 2017-06-12
- 2.1.0 — 2017-06-08
- 2.0.1 — 2017-06-02
- 2.0.0 — 2016-07-01
- 1.0.0 — 2015-12-09
- 0.0.1 — 2015-12-07
- 0.0.0 — 2015-12-04

## README

# load-from-cwd-or-npm

[![npm version](https://img.shields.io/npm/v/load-from-cwd-or-npm.svg)](https://www.npmjs.com/package/load-from-cwd-or-npm)
[![Build Status](https://travis-ci.com/shinnn/load-from-cwd-or-npm.svg?branch=master)](https://travis-ci.com/shinnn/load-from-cwd-or-npm)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/load-from-cwd-or-npm.svg)](https://coveralls.io/github/shinnn/load-from-cwd-or-npm?branch=master)

Load a module from either CWD or [`npm` CLI](https://github.com/npm/cli) directory

```javascript
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](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).

```
npm install load-from-cwd-or-npm
```

## API

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

### loadFromCwdOrNpm(*moduleId*)

*moduleId*: `string` (a module ID without path separators (`/`, `\\`))  
Return: `Promise<any>`

It loads a module with the given module ID from either of these two directories:

1. [`node_modules`](https://docs.npmjs.com/files/folders#node-modules) in the [current working directory](https://nodejs.org/api/process.html#process_process_cwd)
2. `node_modules` in the directory where [`npm` CLI](https://github.com/npm/npm) is installed

If the module ins't installed in CWD but included in the [npm CLI dependencies](https://github.com/npm/npm/blob/v5.5.1/package.json#L36-L129), it loads the module from npm CLI directory.

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

(async () => {
  const nopt = await loadFromCwdOrNpm('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.

```javascript
// $ npm ls eslint
// > └── eslint@4.11.0

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

If the module exists in both directories, it compares their [package versions](https://docs.npmjs.com/files/package.json#version) and loads the newer one.

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

(async () => {
  // Loaded from npm CLI directory because the CWD version is older
  const rimraf = await loadFromCwdOrNpm('rimraf');
})();
```

The returned promise will be [fulfilled](http://promisesaplus.com/#point-26) with the loaded module, or [rejected](http://promisesaplus.com/#point-30) when it fails to find the module from either directories.

## License

[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe

---
_Source: https://npm.io/package/load-from-cwd-or-npm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
