npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

auto-reload

v0.2.0

Published

To clear require cache, and auto reload module

Readme

node-reload

CI coverage npm node license

For node.js to reload the module, which was loaded by require, automatically when its file changes.

Works on every Node.js version from v4 up to the latest (regression-tested in CI).

Install

npm install auto-reload

Example

Reload Json

test/data/json.json

{
  "rand": 0,
  "list": [{
    "name": "Alan"
  }]
}

test/json.js

const fs = require('fs');
const path = require('path');
const reload = require('auto-reload');
const data = reload('./data/json'); // reload after file changed

// print data every sec
setInterval(() => {
  console.log(data.rand, data.list[0].name);
}, 1000);

// update data.json after startup
setTimeout(() => {
  const text = `{
  "rand": ${Math.random()},
  "list": [{
    "name": "Test"
  }]
}`;
  fs.writeFileSync(path.join(__dirname, './data/json.json'), text);
}, 0);

Result:

0 'Alan'
0 'Alan'
0.41179045320583496 'Test'

Reload Js file

test/data/code.js

module.exports = {
  num: 0,
  str: 'string',
  obj: {
    name: 'Alan',
    age: 18
  },
  list: [1,3,5,7,9]
};

test/code.js

const reload = require('../');
const data = reload('./data/code');

// print data from module every sec
setInterval(function() {
  console.log(new Date, data);
}, 1000);

// If you update test/data/code.js
// the output will change immediately

How it works

reload(path[, interval]) returns the module exports and keeps updating that same object in place whenever the file changes, so every reference you handed out (including nested ones) stays current:

| usage | behavior | | --- | --- | | reload(path) | watch the file (stat polling, ~500ms), update on change | | reload(path, n) with n <= 1000 | same as above | | reload(path, n) with n > 1000 | check the file every n ms, update only when it changed |

Notes:

  • Relative paths resolve against the directory of the file that calls reload().
  • Fresh require() calls of the same file always return the new data too.
  • When an update fails (e.g. the file was saved half-written / has a syntax error), the old data is kept and the error is logged once instead of spamming on every retry; logging resumes when a different error occurs.
  • Debug output can be enabled with the -d CLI flag or NODE_DEBUG=auto-reload.

Limitations

  • Function exports can not be hot-swapped. JavaScript function objects keep their code forever, so module.exports = function ... stays on the old implementation (a warning is logged once). Wrap it in an object instead:

    // route.js
    module.exports = { handler: function (req, res) { res.send('hello'); } };
    
    // app.js
    const route = reload('./route');
    app.get('/', (req, res) => route.handler(req, res));

    The same applies to function properties inside an exported object.

  • Primitive exports (module.exports = 42) can not update old references; the old value is kept and a warning is logged once.

  • Editing a file without changing its size and its mtime granularity (within the same second on filesystems with 1s mtime resolution) is detected on the next change.

Aims

  • [x] auto reload json file
  • [x] auto reload js file
  • [x] fix memory leak (no more vm context per reload)
  • [x] deep-renew nested references (and survive circular references)
  • [x] relative require() inside reloaded modules resolves correctly
  • [x] unit test suite without dependencies (runs on Node >= 4)
  • [x] CI matrix from Node v4 to the current release
  • [ ] provide more options (include logger etc.)
  • [ ] rewrite with promise (include improve exception catch)

Development

Run the test suite (no dependencies to install):

npm test

Run the tests with coverage — c8 enforces 100% coverage (statements, branches, functions, lines) over lib/ + index.js. CI runs this on every push and publishes the badge to the badges branch:

npm run coverage

The files under test/ (other than test/run.js, test/helpers.js and *.test.js) are manual demos — start one, edit the watched file, and watch it update. The express demo needs npm i express first.