# context-require

> Allows you to require files in a custom vm context.

Latest version **1.4.3** (published 2026-06-23) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.4.3 |
| Published | 2026-06-23 |
| First published | 2018-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Michael Rawlings |
| Maintainers | mlrawlings, dylanpiercey |
| Keywords | require, context, runInContext, vm, bundle |

## Links

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

## Dependencies (1)

- [builtins](https://npm.io/package/builtins.md) ^5.0.1

## 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

- 1.4.3 (latest) — 2026-06-23
- 1.4.2 — 2024-01-24
- 1.4.0 — 2023-08-10
- 1.3.0 — 2023-07-31
- 1.2.2 — 2021-09-01
- 1.2.1 — 2021-08-25
- 1.2.0 — 2021-08-25
- 1.1.4 — 2021-08-07
- 1.1.3 — 2021-06-24
- 1.1.2 — 2021-06-24
- 1.1.1 — 2021-06-24
- 1.1.0 — 2021-06-23
- 1.0.4 — 2018-12-11
- 1.0.3 — 2018-07-10
- 1.0.2 — 2018-06-28
- … 2 more at https://npm.io/package/context-require/versions

## README

<h1 align="center">
  <!-- Logo -->
  context-require
  <br/>

  <!-- Stability -->
  <a href="https://nodejs.org/api/documentation.html#documentation_stability_index">
    <img src="https://img.shields.io/badge/stability-stable-brightgreen.svg" alt="API Stability"/>
  </a>
  <!-- TypeScript -->
  <a href="http://typescriptlang.org">
    <img src="https://img.shields.io/badge/%3C%2F%3E-typescript-blue.svg" alt="TypeScript"/>
  </a>
  <!-- Prettier -->
  <a href="https://github.com/prettier/prettier">
    <img src="https://img.shields.io/badge/styled_with-prettier-ff69b4.svg" alt="Styled with prettier"/>
  </a>
  <!-- Travis build -->
  <a href="https://travis-ci.org/mlrawlings/context-require">
  <img src="https://img.shields.io/travis/mlrawlings/context-require.svg" alt="Build status"/>
  </a>
  <!-- Coveralls coverage -->
  <a href="https://coveralls.io/github/mlrawlings/context-require">
    <img src="https://img.shields.io/coveralls/mlrawlings/context-require.svg" alt="Test Coverage"/>
  </a>
  <!-- NPM version -->
  <a href="https://npmjs.org/package/context-require">
    <img src="https://img.shields.io/npm/v/context-require.svg" alt="NPM Version"/>
  </a>
  <!-- Downloads -->
  <a href="https://npmjs.org/package/context-require">
    <img src="https://img.shields.io/npm/dm/context-require.svg" alt="Downloads"/>
  </a>
</h1>

Creates a new require function which runs all required modules in a context other than `global`.
Supports custom `require extensions` and `resolvers`. Useful for things like mocking the `package.json`
browser field without a bundler and using JSDOM (without using jsdom global) along side the native
commonjs require system.

Although other uses are possible this module was built to run tests in a browser like context
with out using a bundler. Because modules are cached once per context this tool also makes it easy to
isolate globals and state between tests running in the same process.

# Installation

```console
npm install context-require
```

# Example

**./index.js**
```javascript
import createRequire from "context-require";

const browserRequire = createRequire({
  dir: __dirname,
  context: new JSDOM('<div>Hello World</div>').window, // This object becomes the context for any required files.
  extensions: ..., // Same as require.extensions but only used in the above context.
  resolve(from, request, parentModule) {...} // Override file resolution for this context.
});

browserRequire("./get-document-body").innerHTML; // <div>Hello World</div>
```

**./get-document-body.js**
```js
typeof global; // undefined
module.exports = document.body;
```

### Contributions

* Use `npm test` to build and run tests.

Please feel free to create a PR!

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