# jsdom-context-require

> Allows you to require files in a jsdom window context.

Latest version **5.2.8** (published 2026-03-05) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 55/100 (C)** — status: stable.

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

Warnings: low downloads; no esm support.

## Facts

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

## Links

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

## Dependencies (2)

- [context-require](https://npm.io/package/context-require.md) ^1.4.2
- [resolve.exports](https://npm.io/package/resolve.exports.md) ^2.0.2

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 5.2.8 (latest) — 2026-03-05
- 5.2.7 — 2025-11-17
- 5.2.5 — 2025-01-15
- 5.2.4 — 2024-09-08
- 5.2.3 — 2024-02-26
- 5.2.2 — 2024-02-02
- 5.2.1 — 2024-02-02
- 5.2.0 — 2024-02-02
- 5.1.1 — 2024-01-24
- 5.1.0 — 2024-01-24
- 5.0.0 — 2024-01-23
- 4.0.9 — 2024-01-09
- 4.0.8 — 2023-05-16
- 4.0.7 — 2023-03-31
- 4.0.6 — 2022-10-25
- … 14 more at https://npm.io/package/jsdom-context-require/versions

## README

<h1 align="center">
  <!-- Logo -->
  jsdom-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/jsdom-context-require">
  <img src="https://img.shields.io/travis/mlrawlings/jsdom-context-require.svg" alt="Build status"/>
  </a>
  <!-- NPM version -->
  <a href="https://npmjs.org/package/jsdom-context-require">
    <img src="https://img.shields.io/npm/v/jsdom-context-require.svg" alt="NPM Version"/>
  </a>
  <!-- Downloads -->
  <a href="https://npmjs.org/package/jsdom-context-require">
    <img src="https://img.shields.io/npm/dm/jsdom-context-require.svg" alt="Downloads"/>
  </a>
</h1>

Creates a new require function which runs all required modules in a new `jsdom window` context instead of `global`.
Supports custom `require extensions` and `resolvers`. Also automatically resolves `browser` fields in `package.json`.

Ultimately this allows you to run tests with JSDOM without exposing your nodejs globals, or using a bundler.

# Installation

```console
npm install jsdom jsdom-context-require -D
```

Note: JSDOM is required as a peerDependency as of `jsdom-context-require@4`

# Example

**./index.js**
```javascript
import { createBrowser } from "jsdom-context-require";

const browser = createBrowser({
  dir: __dirname, // The path to resolve new requires from.
  html: "<div>Hello World</div>" // Initial jsdom html.
  extensions: ..., // Same as require.extensions but only used in the jsdom context.
  // All other options forwarded to jsdom.
});

const titleSetter = browser.require("./set-document-title");

titleSetter("Updated!");

assert.equal(browser.window.document.title, "Updated!");
```

**./set-document-title.js**
```js
const $ = require("jquery"); // Any subsequent requires are evaluated in the jsdom window as well.

typeof global; // undefined

module.exports = (title) => {
  document.title = title;
}
```

# Additional browser apis

The result of `createBrowser` api gives you a [jsdom instance](https://github.com/jsdom/jsdom?tab=readme-ov-file#jsdom-object-api).
The following methods are added to the jsdom instance:

## browser.require(id: string): Exports

This is the same as the `require` function in node, except with all modules evaluated in the jsdom window's context.

## browser.yield(): Promise<void>

Waits for one macro task (`setImmediate`) to occur in the browser.

## browser.act<T>(fn?: () => T): Promise<Awaited<T>>

Runs and awaits a given (optional) function, then waits for a macro task (see [browser.yield](#browseryield-promise)).
This helper also keeps track of any uncaught errors within the browser context and will reject the promise with the error, or an AggregateError if there are any.

### Contributions

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

Please feel free to create a PR!

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