# node-hook

> Run source transform function on Node require

Latest version **1.0.0** (published 2017-07-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-hook
pnpm add node-hook
yarn add node-hook
bun add node-hook
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-07-24 |
| First published | 2014-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/node-hook) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 66 |
| Author | Gleb Bahmutov |
| Maintainers | bahmutov |
| Keywords | require, node, load, hook, transform |

## Links

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

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-07-24
- 0.6.1 — 2017-05-04
- 0.6.0 — 2017-05-04
- 0.5.0 — 2017-05-04
- 0.4.1 — 2017-05-04
- 0.4.0 — 2016-05-02
- 0.3.0 — 2016-04-19
- 0.2.0 — 2015-09-17
- 0.1.0 — 2014-01-11
- 0.0.0 — 2014-01-11

## README

# node-hook

> Run source transform function on Node require

[![NPM][node-hook-icon]][node-hook-url]

[![Build status][node-hook-ci-image]][node-hook-ci-url]
[![dependencies][node-hook-dependencies-image]][node-hook-dependencies-url]
[![devdependencies][node-hook-devdependencies-image]][node-hook-devdependencies-url]
[![semantic-release][semantic-image] ][semantic-url]

## Install and use

```sh
npm install --save node-hook
```

Before loading desired *.js* files, install hook

```js
var hook = require('node-hook');

function logLoadedFilename(source, filename) {
    return 'console.log("' + filename + '");\n' + source;
}
hook.hook('.js', logLoadedFilename);
require('./dummy');
// prints fulle dummy.js filename, runs dummy.js

hook.unhook('.js'); // removes your own transform
```

**remember:** Nodejs caches compiled modules, so if the transform is not
working, you might need to delete the cached entry in `require.cache`,
then call `require(filename)` again to force reload.

Related: Node require replacement [really-need](https://github.com/bahmutov/really-need).

You can hook several transformers thanks to the [code](https://github.com/bahmutov/node-hook/pull/2)
submitted by [djulien](https://github.com/djulien)

## Existing transform

You can get the current transform and run any source through it. For example
to see how the current source looks when loaded but before evaluated

```js
const filename = resolve('./call-foo.js')
const transform = Module._extensions['.js']
const fakeModule = {
  _compile: source => {
    console.log('transformed code')
    console.log(source)
  }
}
transform(fakeModule, filename)
```

## Small print

Author: Gleb Bahmutov &copy; 2013

* [Changelog](History.md)
* [@bahmutov](https://twitter.com/bahmutov)
* [glebbahmutov.com](http://glebbahmutov.com)
* [blog](http://glebbahmutov.com/blog/)

License: [MIT](MIT-license.md) - do anything with the code,
but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

[node-hook-icon]: https://nodei.co/npm/node-hook.svg?downloads=true
[node-hook-url]: https://npmjs.org/package/node-hook
[node-hook-ci-image]: https://travis-ci.org/bahmutov/node-hook.svg?branch=master
[node-hook-ci-url]: https://travis-ci.org/bahmutov/node-hook
[node-hook-dependencies-image]: https://david-dm.org/bahmutov/node-hook.svg
[node-hook-dependencies-url]: https://david-dm.org/bahmutov/node-hook
[node-hook-devdependencies-image]: https://david-dm.org/bahmutov/node-hook/dev-status.svg
[node-hook-devdependencies-url]: https://david-dm.org/bahmutov/node-hook#info=devDependencies
[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-url]: https://github.com/semantic-release/semantic-release

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