Coder Social home page Coder Social logo

invalidate-module's Introduction

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().

Now the next time you call require('./my-module'), it will return a new copy of ./my-module instead of a cached copy.

Example

Example when used with a watcher like chokidar:

// watch.js
const chokidar = require('chokidar');
const invalidate = require('invalidate-module');
const path = require('path');

const watcher = chokidar.watch('*.js', { 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 watch.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:

// a.js
require('./b');
console.log('this is module a, version 2');

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 version 2

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

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

// b.js
console.log('this is module b version 2');

b.js and its dependent a.js will be invalidated and re-running require('./a') in the watch callback 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.

Further Reading

invalidate-module's People

Contributors

kentor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

studocu

invalidate-module's Issues

Does not work

Nor simply nor with path.resolve nor require.resolve does not work. Your module is error prone.
I am on linux, nodejs v7.10.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.