# nodejs-hmr

> ## A nodejs hot module reload

Latest version **1.3.6** (published 2024-08-30) · 0 weekly downloads

## Install

```sh
npm install nodejs-hmr
pnpm add nodejs-hmr
yarn add nodejs-hmr
bun add nodejs-hmr
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.6 |
| Published | 2024-08-30 |
| First published | 2020-08-20 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 28.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | beven |

## Links

- npm: https://www.npmjs.com/package/nodejs-hmr
- npm.io page: https://npm.io/package/nodejs-hmr

## Recent versions

- 1.3.6 (latest) — 2024-08-30
- 1.3.5 — 2024-07-31
- 1.3.4 — 2024-07-29
- 1.3.3 — 2024-07-29
- 1.3.2 — 2024-05-10
- 1.3.1 — 2024-04-21
- 1.3.0 — 2024-04-21
- 1.1.6 — 2022-09-13
- 1.1.5 — 2020-10-28
- 1.1.4 — 2020-10-28
- 1.1.3 — 2020-10-28
- 1.1.2 — 2020-10-22
- 1.2.0 — 2020-10-22
- 1.1.0 — 2020-10-21
- 1.0.8 — 2020-08-31
- … 8 more at https://npm.io/package/nodejs-hmr/versions

## README

# nodejs-hmr

## A nodejs hot module reload 

### First Step

Add in the first line of code

```js
import hot from 'nodejs-hmr';

// initilaize hot reload api
hot.run();

// or 
// hot.run({ cwd: path.resolve('xx/xx') })
// hot.run({ cwd: [ path.resolve('xx/xx'),... ] })

```

### Second Step

By default, if a module change is detected again, the modified module will be reloaded in turn, and the parent module that references this module will also be reloaded.


#### Life cycle

```js
// preload ---> preend ---> accept ---> postend .......---> Alldone
```

#### preload

Triggered before reloading the module, the event will be dispatched to all parent modules that reference this module

> xxx.js

```js

import hot from 'nodejs-hmr';

hot
  .create(module)
  .preload((old)=>{
    // do somthing at pre reload module
  })
  .preend((old)=>{
    // on preload end
  })

```

##### accept

Accept child module change,

If the change module defines the `accept` function, the parent module will stop reloading.


>  xxx.js

```js

import hot from 'nodejs-hmr';

hot
  .create(module)
  .accept((newModule,oldModule)=>{
    // do somehting
  })

```

##### postend

```js

import hot from 'nodejs-hmr';

hot
  .create(module)
  .postend((newModule,oldModule)=>{
    // do somehting
  })

```

##### clean

```js

import hot from 'nodejs-hmr';

hot
  .create(module)
  // clean hook listener
  .clean('postend','..')
  .postend((newModule,oldModule)=>{
    // do somehting
  })

```

### Updater

Currently, `Array`, `Map`, and `Object` types can be updated through `hot.createHotUpdater`.

```js

import Color from './color';
import Size from './size';

const registrations = new Array<XX>();

registrations.push(new Color());
registrations.push(new Size());

 hot
  .create(module)
  .accept((newModule,oldModule)=>{
    //When the color.js file changes, you can specify the update replacement in the following ways
    hot
      .createHotUpdater(registrations,newModule,oldModule)
      .needHot((item,oldCtor)=> item instanceof oldCtor)
      // set replacement instance constructor
      .creator((ctor)=>{
        return new ctor();
      })
      // do update
      .update();
  })

```

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