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 🙏

© 2024 – Pkg Stats / Ryan Hefner

require-watch

v2.2.0

Published

Automatically refresh require dependencies in NodeJS

Downloads

6

Readme

require-watch

npm downloads Build Status Dependency Status Coverage Status

Require watch wraps around Node's module loader and watches files as they are loaded. If a change is detected on a watched module, then that module, along with all its parents, are automatically flushed from the require cache.

This was primarily motivated in enabling hot reloading for express routes. See below under Express example

Example usage:

import watch from 'require-watch';

watch(require.resolve('lib/some-root'));

After this, any changes in lib/some-root will automatically flush the require cache. Note that flushing the require cache still requires code that manually requires the module again.

Notes

  • You should require and use require-watch as early as possible in your application, so that it can properly wrap Module._load with its watching mechanisms
  • For verbose output of require-watch, set NODE_DEBUG=require-watch

Limitations

Although the module is flushed from the require cache, there is no guarantee that it will be reloaded. Calling code is responsible for doing this. For example:

index.js

import watch from 'require-watch';
import sayHello from 'lib/hello';

watch(require.resolve('lib/hello'));

setInterval(sayHello, 1000); // this syntax won't be hot reloaded

lib/sayHello.js

export default function() { console.log('hello'); }

If you were to edit lib/sayHello.js, there would be no effect. Instead, you need to introduce a require statement in the calling function:

index.js

setInterval(() => require('lib/sayHello')(), 1000); // this syntax enables hot reloading

This is necessary because this module only flushes the require cache - any existing code loaded in memory remains in memory if it is not re-required. There is no way around this limitation.

The reason why this revised code works is because the caller, setInterval is requesting the module every time from the require cache. There should be minimal performance impact because if a module is already loaded, it is not reloaded from disk.

Express example

If you originally had some code like this:

import express from 'express';

const app = express();

app.get('/', (req, res, next) => {
	// some code you want dynamic
});

app.listen(8080);

First extract the router into a separate file:

import { Router } from 'express';

const router = Router();

router.get('/', (req, res, next) => {
	// some code you want dynamic
});

export default router;

Then update your app entry like this:

import express from 'express';
import watch from 'require-watch'; // use the module

const app = express();

watch(require.resolve('./router')); // watch this file
app.use('/', (req, res, next) => require('./router')(req, res, next)); // force reloading

app.listen(8080);

What happens is that every WEB request causes a fresh REQUIRE request. If you edit ./router.js, require-watch will invalidate the cache, forcing a reload of the module.