# module-library

> Caches module singletons and orchestrates dependencies between them

Latest version **0.96.0** (published 2021-01-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install module-library
pnpm add module-library
yarn add module-library
bun add module-library
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.96.0 |
| Published | 2021-01-01 |
| First published | 2016-12-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | erikpukinskis |

## Links

- npm: https://www.npmjs.com/package/module-library
- Repository: https://github.com/erikpukinskis/module-library
- Issues: https://github.com/erikpukinskis/module-library/issues
- npm.io page: https://npm.io/package/module-library

## Dependencies (1)

- [string-tree](https://npm.io/package/string-tree.md) *

## Recent versions

- 0.96.0 (latest) — 2021-01-01
- 0.95.0 — 2019-04-26
- 0.94.0 — 2019-04-22
- 0.93.0 — 2018-10-30
- 0.92.0 — 2018-10-30
- 0.91.0 — 2018-10-30
- 0.90.0 — 2018-10-30
- 0.89.0 — 2018-03-28
- 0.88.0 — 2018-03-28
- 0.87.0 — 2018-03-26
- 0.86.0 — 2018-03-26
- 0.85.0 — 2018-03-24
- 0.84.0 — 2018-03-24
- 0.83.0 — 2018-03-24
- 0.82.0 — 2018-03-19
- … 25 more at https://npm.io/package/module-library/versions

## README

Most build systems use config files to define dependencies separate from code.

**module-library** allows you to define your dependencies in code:

```javascript
var library = require("module-library")(require)

library.define(
  "people",
  function() {
    return [{name: "erik", age: 35}, {name: "alex", age:39}, {name: "kate", age: 30}]
  }
)

library.define(
  "say-hello",
  ["people", "querystring"],
  function(people, querystring) {
    return function hi() {
      people.forEach(function(person) {
        console.log(querystring.stringify(person))
      })
    }
  }
)

library.using(
  ["say-hello"],
  function(hi) {
    hi()
  }
)
```

You can also export these modules so they are accessible via commonjs:

```javascript
var library = require("module-library")(require)

module.exports = library.export(
  "say-hello",
  ["people", "http"],
  function(people, http) {
    ...
  }
)
```

## Dependencies inside your module

If you want to maintain good encapsulation but only export one module, you can define as many extra modules as you like:

```javascript

library.define(
  "hot-dog-stand/inventory",
  function() {
    return function stock(item) {
      if (isPerishable(item)) {}
      ...
    }
  }
)

library.define(
  "hot-dog-stand/triage",
  function() {
    return function expedite(order) {
      var lowPri = lowestDollarValueCustomer()
      order.position = lowPri.position
      lowPri.position = 1000
    }
  }
)
```

And then require them from the module you're exporting:

```javascript
module.exports = library.export(
  "hot-dog-stand",
  ["hot-dog-stand/triage", "hot-dog-stand/inventory", ...],
  function(stock, expedite, ...) {
    openProcess(function(deliveries) {
      if (deliveries.length > 0) { stock(deliveries[0]) }
    })
    ...
  }
)
```

## Why

* We have an explicit reference, in software, of which dependencies are needed for a piece of code. This makes it easy to load that code in other places, like in the browser (see [bridge-module](https://github.com/erikpukinskis/bridge-module)), without any kind of elaborate, declarative, filesystem-based, side-effect ridden build process.

* We can pause and debug any part of the module loading process in the same process as our app

* We will (later on) be able to hot reload modules without refreshing the whole tree

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