# require-load

> Asynchronously require files in node js.

Latest version **2.2.0** (published 2017-02-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install require-load
pnpm add require-load
yarn add require-load
bun add require-load
```

## 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 | 2.2.0 |
| Published | 2017-02-11 |
| First published | 2016-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Dylan Piercey |
| Maintainers | dylanpiercey |
| Keywords | async, load, require, require-async, require-load |

## Links

- npm: https://www.npmjs.com/package/require-load
- Repository: https://github.com/DylanPiercey/require-load
- Issues: https://github.com/DylanPiercey/require-load/issues
- npm.io page: https://npm.io/package/require-load

## Dependencies (3)

- [callsite](https://npm.io/package/callsite.md) ^1.0.0
- [strip-bom](https://npm.io/package/strip-bom.md) ^3.0.0
- [enhanced-resolve](https://npm.io/package/enhanced-resolve.md) ^3.1.0

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.2.0 (latest) — 2017-02-11
- 2.0.3 — 2017-01-14
- 2.0.2 — 2017-01-14
- 2.0.1 — 2017-01-13
- 2.0.0 — 2016-10-30
- 1.2.0 — 2016-10-17
- 1.1.0 — 2016-10-17
- 1.0.3 — 2016-10-10
- 1.0.2 — 2016-10-05
- 1.0.1 — 2016-10-04
- 1.0.0 — 2016-10-04

## README

<h1 align="center">
  Require-Load
	<br/>

  <!-- Stability -->
  <a href="https://nodejs.org/api/documentation.html#documentation_stability_index">
    <img src="https://img.shields.io/badge/stability-stable-brightgreen.svg?style=flat-square" alt="API stability"/>
  </a>
  <!-- Standard -->
  <a href="https://github.com/feross/standard">
    <img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square" alt="Standard"/>
  </a>
  <!-- NPM version -->
  <a href="https://npmjs.org/package/require-load">
    <img src="https://img.shields.io/npm/v/require-load.svg?style=flat-square" alt="NPM version"/>
  </a>
  <!-- Downloads -->
  <a href="https://npmjs.org/package/require-load">
    <img src="https://img.shields.io/npm/dm/require-load.svg?style=flat-square" alt="Downloads"/>
  </a>
</h1>

An asynchronous module loader for nodejs that integrates with the existing module system.

## Installation

#### Npm
```bash
npm install require-load
```

## API

### load(file: String, options: Object) -> {Promise}
Asynchronously require a specific file. File name resolution works exactly the same as the native `require` function.


```javascript
import load from 'require-load'

load('./example.js').then(result => {
  // Result will be the exports of `example.js`, subsequent calls will be cached.
  // You can even get the cached version using plain old require.
  require('./example.js') === result
}).catch(err => {
  // Handle file load error.
})
```

### load.resolve(file: String, options: Object) -> {Promise}
Asynchronously resolve a file path. (Like require.resolve but async.).


```javascript
import { resolve } from 'require-load'

resolve('./example.js').then(fullpath => {
  // Result will be the full path of './example.js'
}).catch(err => {
  // Handle file resolve error.
})
```

## Options (same for both load and load.resolve)

### cache=false (default true)
When the cache option is false the module will be re-evaluated and not cached for the next load call.

```javascript
load('./example.js', { cache: false }).then(result1 => {
  load('./example.js', { cache: false }).then(result2 => {
    result1 !== result2 // Module was not cached.
  })
})
```

### `directory=__dirname` (default relative to the function calling load)

You can optionally choose which directory the resolve files from.
By default this will be relative to where ever this module is required.

```javascript
load('./main.test.js', { directory: __dirname + '/test' }).then(result => {
  // Will resolve modules from the test folder instead of where this was required.
})
```

### `file=__filename` (default relative to the function calling load)

You can also optionally specify a file from which the loader should run. (This also defaults the directory option to be the dirname of the file).

```javascript
load('./main.test.js', { file: __dirname + '/test/custom.test.js' }).then(result => {
  // Will resolve modules relative to the custom.test.js file instead of where this was required.
})
```

### `resolve.fileSystem=MemoryFileSystem` (default to nodeFileSystem.)

You can choose the file system to use when resolving and compiling files. The default will just be the standard 'fs' module but you can also get fancy and use other files systems like 'memory-fs'.

```javascript
import path from 'path'
import MemoryFileSystem from 'memory-fs'
const memoryFs = new MemoryFileSystem()

// Save a file to memory fs.
memoryFs.writeFileSync(path.join(__dirname, './main.test.js'), 'module.exports = "hello world"')

// Load the file from memory.
load('./main.test.js', { resolve: { fileSystem: memoryFs } }).then(result => {
  result === 'hello world'
})
```

## Clearing the cache
Clearing a file from the cache is the exact same as any other node module (once the file has loaded).

```javascript
  load('./example.js').then(result => {
    // Remove from the require cache manually.
    delete require.cache[require.resolve('./example.js')]
  })
```

## Custom extensions
Just like node's require you can add or overwrite the file extensions, however instead of calling the extension function with a filename it will be called with the file's contents.

```javascript
// A naive babel loader for babel es6 files.
import babel from 'babel-core'
load.extensions['.es6'] = function parseCustomFile (module, script) {
  module._compile(babel.transform(script), module.filename)
}
```

### Contributions

* Use `npm test` to run tests.

Please feel free to create a PR!

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