# require-context

> Require with an expression.

Latest version **1.1.0** (published 2018-03-21) · MIT license · 0 weekly downloads

## Install

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

## 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 | 1.1.0 |
| Published | 2018-03-21 |
| First published | 2017-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 3.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 49 |
| Author | Wilson Lewis |
| Maintainers | wilsonlewis |
| Keywords | node, require, context, modules, webpack |

## Links

- npm: https://www.npmjs.com/package/require-context
- Repository: https://github.com/wilsonlewis/require-context
- Homepage: https://github.com/wilsonlewis/require-context#readme
- Issues: https://github.com/wilsonlewis/require-context/issues
- npm.io page: https://npm.io/package/require-context

## Dependencies (1)

- [node-dir](https://npm.io/package/node-dir.md) ^0.1.17

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-03-21
- 1.0.4 — 2018-02-07
- 1.0.2 — 2017-12-04
- 1.0.1 — 2017-12-04
- 1.0.0 — 2017-12-04
- 0.0.2 — 2017-10-08
- 0.0.1 — 2017-10-08

## README

# require-context v1.1.0

## Installation

Using npm:
```shell
$ npm i -g npm
$ npm i --save require-context
```

In Node.js:
```js
// Load globally into all modules.
require('require-context/register')

// Load locally as a function.
var requireContext = require('require-context');
```

## Usage

It allows you to pass in a directory to search, a flag indicating whether
subdirectories should be searched too, and a regular expression to match files against.

The syntax is as follows:
```js
require.context(directory, useSubdirectories = false, regExp = /^\.\//)
```

Examples
```js
require.context("./test", false, /\.test\.js$/);
// a context with files from the test directory that can be required with a request endings with `.test.js`.
```
```js
require.context("../", true, /\.stories\.js$/);
// a context with all files in the parent folder and descending folders ending with `.stories.js`.
```

## Context API

A context module exports a (require) function that takes one argument: the request.

The exported function has 3 properties: resolve, keys, id.

 * `resolve` is a function and returns the module id of the parsed request.
 * `keys` is a function that returns an array of all possible requests that the context module can handle.
 
This can be useful if you want to require all files in a directory or matching a pattern, Example:

```js
function importAll (r) {
  r.keys().forEach(r);
}

importAll(require.context('../components/', true, /\.js$/));
```

## Why require-context?

 * Take the hassle out of requiring groups of modules
 * Write cross-platform code between [webpack](https://webpack.js.org) and [node](https://nodejs.org)

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