GithubHelp home page GithubHelp logo

clear-module's Introduction

clear-module

Clear a module from the cache

Useful for testing purposes when you need to freshly import a module.

Install

$ npm install clear-module

Usage

// foo.js
let i = 0;
module.exports = () => ++i;
const clearModule = require('clear-module');

require('./foo')();
//=> 1

require('./foo')();
//=> 2

clearModule('./foo');

require('./foo')();
//=> 1

API

clearModule(moduleId)

moduleId

Type: string

What you would use with require().

clearModule.all()

Clear all modules from the cache.

clearModule.match(regex)

Clear all matching modules from the cache.

regex

Type: RegExp

Regex to match against the module IDs.

clearModule.single(moduleId)

Clear a single module from the cache non-recursively. No parent or children modules will be affected.

This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects.

moduleId

Type: string

What you would use with require().

Related

clear-module's People

Contributors

bendingbender avatar mrchief avatar omrilotan avatar richienb avatar sindresorhus avatar sondregj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clear-module's Issues

Doesn't work for mongoose

Issuehunt badges

For other libraries it's OK but I have problems with clearing mongoose.

OS: Mac OS X 10.12.6
Node.js: 10.13.0
clear-module: 3.0.0
mongoose: 5.3.13

const mongoose = require('mongoose');
const clearModule = require('clear-module');

clearModule('mongoose');
console.log(require('mongoose') === mongoose); // I expect false but it's true

IssueHunt Summary

omrilotan omrilotan has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

(node:1550) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

I am trying to remove the cache for mocha test. When I run the below code I get the error

(node:1550) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

const glob = require('glob-promise');
const clearModule = require('clear-module');

const specs = await glob('./testfiles/*.js', { absolute: true });
console.log(specs);
specs.map(spec => {
  clearModule(spec);
  mocha.addFile(spec);
});

I checked more to find the error and found out that when I use following code

console.log(callerPath());

I get null. So it seems callerPath can't get the folder. I tested above code with NodeJS v10.4.1

Add `filter` option

See the previous attempt and the feedback given there: #15

If you decide to work on this, you should have a lot of JS experience and be willing to do a good effort.

Having some troubles making clear-module working

Hi, i'm having some problems trying to make this module running on my code (i'm quite sure i didn't fully get the whole cache concept)

Maybe it is a stackoverflow question, in that case feel free to tell me :)

Here i prepared a small repo as example

git clone [email protected]:giggioz/test-clear-module.git

install the modules with
npm i
and then run it with
node index.js

This code prints every 500ms a value.
This value comes from a delegate.
Now if i change while the code is running the value returned by get-points.js it does not do anything... i would expect a change, right?

Thanks for your considerations!

Recursively delete child modules

I have a case where the entry point of a module was not the file storing the state, it only surfaces it. This caused me to recurse module.children and delete, which solved the problem. Would you like me to add this feature, perhaps behind an option?

Clear single module

Could you add a possibility co clear only single module, without the recursion? I have a system that has a state holding singleton and with that recursive clear it creates multiple instances of that singleton with a different state. I would like to have an option to clear it manually one by one.

BUG : Clear-module doesn't skip native module

Hey we've been using this module a bit, and I've encountered an issue with it.

It seems you don't filter out native module, and as per nodejs documentation this causes bad behavior when re-requiring code later on.

This documentation as been added there : nodejs/node@5c14d69

To reproduce, clear-module and reimport any library that internally uses a native library ( '.node' file )

I'm getting an Error: Unknown failure when requiring it back and more specifically when the codes tries to call a function (that's within the .node )

clear-module does not clear modules deep in the stack trace

Due to v8 stack traces only going so many deep by default, modules deep enough in the dependency tree such that clear has been called >~8 times do not get cleared. This is because resolve/parentModule relies on the v8 stack trace.

It seems to me we don't need to resolve moduleId when calling it recursively from within clear, since children are absolute paths.

I've copied this module into our project and fixed as so:

const clear = (moduleId: string) => {
	clearInternal(moduleId, true);
}

const clearInternal = (moduleId: string, doResolve: boolean) => {
	if (typeof moduleId !== 'string') {
		throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``);
	}

	const filePath = doResolve ? resolve(moduleId) : moduleId;

       ...


		for (const id of children) {
			clearInternal(id, false);
		}

Endless recursion on circular dependencies

Hello,

I have an issue when using clear-module within a Mocha unit test: I get an endless recursion. However if I run it outside of a test I don't.

I have successfully used clear-module in tests before. It looks to be related to:

  • upgrading to clear-module: "4.1.0" as the example below works with "3.1.0"
  • clearing sshpk as this has circular dependencies defined

This works fine:

// demo.js
require('sshpk');
const clearModule = require('clear-module');
console.log('before');
clearModule('sshpk');
console.log('after');

produces:

% node demo.js
before
after

However if I run using mocha, it hangs on the call to clearModule:

require('sshpk');
const clearModule = require('clear-module');
console.log('before');
clearModule('sshpk');
console.log('after');

describe('yyy', () => {

    it('xxx', function () {
    });
});

produces:

% ./node_modules/.bin/mocha test/services/demo.spec.js
before

When I debug this I can see an endless recursion of clearing the sshpk modules which have a circular reference.

I am using node 12.6.0 and the following lib versions:

  • "mocha": "7.1.0"
  • "clear-module": "4.1.0" (previously 3.1.0 which worked)
  • "sshpk": "1.16.1"

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.