# require-resolve-hook

> Module to hook into the Node.js require and require.resolve function

Latest version **1.1.0** (published 2020-10-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install require-resolve-hook
pnpm add require-resolve-hook
yarn add require-resolve-hook
bun add require-resolve-hook
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-10-31 |
| First published | 2020-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | imcuttle |
| Maintainers | moyuyc |
| Keywords | imcuttle, require, resolve, hook, require-resolve-hook |

## Links

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

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-10-31
- 1.0.1 — 2020-10-27
- 1.0.0 — 2020-10-27

## README

# require-resolve-hook

[![Build status](https://img.shields.io/travis/imcuttle/require-resolve-hook/master.svg?style=flat-square)](https://travis-ci.org/imcuttle/require-resolve-hook)
[![Test coverage](https://img.shields.io/codecov/c/github/imcuttle/require-resolve-hook.svg?style=flat-square)](https://codecov.io/github/imcuttle/require-resolve-hook?branch=master)
[![NPM version](https://img.shields.io/npm/v/require-resolve-hook.svg?style=flat-square)](https://www.npmjs.com/package/require-resolve-hook)
[![NPM Downloads](https://img.shields.io/npm/dm/require-resolve-hook.svg?style=flat-square&maxAge=43200)](https://www.npmjs.com/package/require-resolve-hook)
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://prettier.io/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=flat-square)](https://conventionalcommits.org)

> Module to hook into the Node.js require and require.resolve function

## Installation

```bash
npm install require-resolve-hook
# or use yarn
yarn add require-resolve-hook
```

## Usage

```javascript
import requireResolveHook, { bypass, unhook } from 'require-resolve-hook'

const { unhook: unhookReact, bypass: bypassReact } = requireResolveHook('react', (id, parent, isMain) => {
  return '/path/to/your/custom-react' // absolute path
})

require.resolve('react') // => '/path/to/your/custom-react'
require('react') // => export '/path/to/your/custom-react'

bypassReact(() => require.resolve('react')) // => '/path/to/real-react'

unhookReact()
require.resolve('react') // => '/path/to/real-react'
```

## API

#### `requireResolveHook(match: Match, onResolve: OnResolve, options: Options)`

- **Returns:** `{ bypass: (fn) => ReturnType<fn>, unhook: () => void }`

  `bypass` means:

  ```javascript
  bypass = (fn) => {
    unhook()
    const res = fn()
    hook()
    return res
  }
  ```

#### `StrictMatch`

- **Type:** `string | RegExp | (id: string) => boolean`

#### `Match`

- Type: `StrictMatch | StrictMatch[]`

#### `OnResolve`

- **Type:** `(id: string, parent: null | Module, isMain: boolean) => string | false`

#### `Options`

##### `ignoreModuleNotFoundError`

Should ignore `MODULE_NOT_FOUND` error?

```javascript
requireResolveHook('react', () => require.resolve('not_found_package'))
requireResolveHook('react', () => require.resolve('founded_react'))

require.resolve('react') === require.resolve('founded_react')
```

```javascript
requireResolveHook('react', () => require.resolve('not_found_package'), { ignoreModuleNotFoundError: false })
requireResolveHook('react', () => require.resolve('founded_react'))

require.resolve('react') // Throws MODULE_NOT_FOUND error
```

- **Type:** `boolean`
- **Default:** `true`

## FAQ

1. How to require the actual module in the mock file?

```javascript
import requireResolveHook, { bypass } from 'require-resolve-hook'
import * as Module from 'module'

requireResolveHook(/^react/, (id, parent, isMain, opts) => {
  if (parent && parent.filename === '/path/to/mock/react') {
    // Access the actual react filename
    return bypass(() => Module._resolveFilename(id, parent, isMain, opts))
  }
  return '/path/to/mock/react'
})
```

## Contributing

- Fork it!
- Create your new branch:  
  `git checkout -b feature-new` or `git checkout -b fix-which-bug`
- Start your magic work now
- Make sure npm test passes
- Commit your changes:  
  `git commit -am 'feat: some description (close #123)'` or `git commit -am 'fix: some description (fix #123)'`
- Push to the branch: `git push`
- Submit a pull request :)

## Authors

This library is written and maintained by imcuttle, <a href="mailto:imcuttle@163.com">imcuttle@163.com</a>.

## License

MIT - [imcuttle](https://github.com/imcuttle) 🐟

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