# require-webpack-compat

> Monkey-patches to make Node's require behave like Webpack's require

Latest version **3.0.0** (published 2016-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install require-webpack-compat
pnpm add require-webpack-compat
yarn add require-webpack-compat
bun add require-webpack-compat
```

## 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.0 |
| Published | 2016-01-22 |
| First published | 2015-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Johannes Stein |
| Maintainers | stoney |
| Keywords | require, webpack, isomorphic, universal |

## Links

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

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2016-01-22
- 2.0.1 — 2016-01-22
- 2.0.0 — 2016-01-22
- 1.0.1 — 2016-01-22
- 1.0.0 — 2015-12-09

## README

# require-webpack-compat
:monkey: Monkey-patches `require.context` into Node.js require

This is meant to simplify Universal JS apps that use Webpack and Node.js.

:warning: Caution: NEVER use it in libraries, only for applications

## Installation

```
npm install require-webpack-compat --save
```

## Usage
```javascript
require('require-webpack-compat')(module, require);
```

Afterwards, we have `.context` function on our `require` function. This allows us to do all the shenanigans we do with it in our Webpack projects.

Inside your webpack configuration, we need to add these lines
```javascript
node: {
  fs: 'empty'
}
```
Otherwise we run into an error while generating the build.

## Example

#### index.js
```javascript
require('require-webpack-compat')(module, require);

var req = require.context('./', false, /\.js$/);

console.log(req.keys().reduce(function(obj, name) {
  var moduleName = path.basename(name).split(path.extname(name))[0];

  obj[moduleName] = req(name);
  return obj;
}, {}));

// {a: 5, b: 6}
```

#### a.js
```javascript
module.exports = 5;
```

#### b.js
```javascript
module.exports = 6;
```

See `/example`.

## License
MIT

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