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

invalidate-module

v1.0.0

Published

Removes a module and all of its dependents from the require cache

Downloads

3,316

Readme

invalidate-module

Build Status npm

Removes a module and all of its dependents from the require cache, so that subsequent requires of that module or any of its dependents will return new copies.

Useful for implementing a hot-reloading environment for Node.js programs in watch mode.

e.g. iterating on a static site generator that uses server side rendered React components for templating.

Install

npm install invalidate-module

Usage

Start the module dependencies tracking by requiring invalidate-module.

const invalidate = require('invalidate-module');

Call invalidate() with the absolute path of a module to remove it and its dependents from require.cache.

invalidate(require.resolve('./my-module'));

Note that you must provide the absolute path of the module, so use something like require.resolve() or path.resolve().

Example

Example when used with a watcher like chokidar:

index.js

const chokidar = require('chokidar');
const path = require('path');

const invalidate = require('invalidate-module');

const watcher = chokidar.watch('.', { ignoreInitial: true });

require('./a');

watcher.on('all', (event, filename) => {
  invalidate(path.resolve(filename));
  require('./a');
});

a.js

require('./b');
console.log('this is module a');

b.js

console.log('this is module b');

Running index.js will call require('./a') which prints:

this is module b
this is module a

If you make this change to a.js and save:

require('./b');
console.log('this is module a v2');

The watcher callback will fire and invalidate a.js so that require('./a') loads the new version and this gets logged:

this is module a v2

Because b.js is still in require.cache, the require('./b') does nothing.

If you make this change to b.js and save:

console.log('this is module b v2');

b.js and its dependent a.js will be invalidated and it will log:

this is module b v2
this is module a v2

Details

At the time of requiring this module, node's require() is monkey-patched so that subsequent calls will add the caller module and the required module to a graph. When you call invalidate() on a module, it deletes the module from require.cache and then it uses the graph to get the module's dependents and deletes them from require.cache as well.

Debug

Running with env vars DEBUG=invalidate-module will log the modules that are deleted from require.cache.