├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture-circular-1.js ├── fixture-circular-2.js ├── fixture-empty.js ├── fixture-match.js ├── fixture-with-dependency.js ├── fixture.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | custom: https://sindresorhus.com/donate 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | - 10 16 | - 8 17 | steps: 18 | - uses: actions/checkout@v2 19 | - uses: actions/setup-node@v1 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - run: npm install 23 | - run: npm test 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture-circular-1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const other = require('./fixture-circular-2'); 4 | 5 | module.exports = other; 6 | -------------------------------------------------------------------------------- /fixture-circular-2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const other = require('./fixture-circular-1'); 4 | 5 | module.exports = other; 6 | -------------------------------------------------------------------------------- /fixture-empty.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = {}; 4 | -------------------------------------------------------------------------------- /fixture-match.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | let i = 0; 3 | module.exports = () => ++i; 4 | -------------------------------------------------------------------------------- /fixture-with-dependency.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // eslint-disable-next-line no-unused-vars 4 | const _ = require('./fixture-empty'); 5 | const fixture = require('./fixture'); 6 | 7 | module.exports = () => fixture(); 8 | -------------------------------------------------------------------------------- /fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | let i = 0; 3 | module.exports = () => ++i; 4 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare const clear: { 2 | /** 3 | Clear a module from the [cache](https://nodejs.org/api/modules.html#modules_caching). 4 | 5 | @param moduleId - What you would use with `require()`. 6 | 7 | @example 8 | ``` 9 | // foo.ts 10 | let i = 0; 11 | module.exports = () => ++i; 12 | 13 | // test.ts 14 | import clearModule = require('clear-module'); 15 | 16 | require('./foo')(); 17 | //=> 1 18 | 19 | require('./foo')(); 20 | //=> 2 21 | 22 | clearModule('./foo'); 23 | 24 | require('./foo')(); 25 | //=> 1 26 | ``` 27 | */ 28 | (moduleId: string): void; 29 | 30 | /** 31 | Clear all modules from the cache. 32 | */ 33 | all(): void; 34 | 35 | /** 36 | Clear all matching modules from the cache. 37 | 38 | @param regex - Regex to match against the module IDs. 39 | */ 40 | match(regex: RegExp): void; 41 | 42 | /** 43 | Clear a single module from the cache non-recursively. No parent or children modules will be affected. 44 | 45 | This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects. 46 | 47 | @param moduleId - What you would use with `require()`. 48 | */ 49 | single(moduleId: string): void; 50 | }; 51 | 52 | export = clear; 53 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const path = require('path'); 3 | const resolveFrom = require('resolve-from'); 4 | const parentModule = require('parent-module'); 5 | 6 | const resolve = moduleId => { 7 | try { 8 | return resolveFrom(path.dirname(parentModule(__filename)), moduleId); 9 | } catch (_) {} 10 | }; 11 | 12 | const clear = moduleId => { 13 | if (typeof moduleId !== 'string') { 14 | throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``); 15 | } 16 | 17 | const filePath = resolve(moduleId); 18 | 19 | if (!filePath) { 20 | return; 21 | } 22 | 23 | // Delete itself from module parent 24 | if (require.cache[filePath] && require.cache[filePath].parent) { 25 | let i = require.cache[filePath].parent.children.length; 26 | 27 | while (i--) { 28 | if (require.cache[filePath].parent.children[i].id === filePath) { 29 | require.cache[filePath].parent.children.splice(i, 1); 30 | } 31 | } 32 | } 33 | 34 | // Remove all descendants from cache as well 35 | if (require.cache[filePath]) { 36 | const children = require.cache[filePath].children.map(child => child.id); 37 | 38 | // Delete module from cache 39 | delete require.cache[filePath]; 40 | 41 | for (const id of children) { 42 | clear(id); 43 | } 44 | } 45 | }; 46 | 47 | clear.all = () => { 48 | const directory = path.dirname(parentModule(__filename)); 49 | 50 | for (const moduleId of Object.keys(require.cache)) { 51 | delete require.cache[resolveFrom(directory, moduleId)]; 52 | } 53 | }; 54 | 55 | clear.match = regex => { 56 | for (const moduleId of Object.keys(require.cache)) { 57 | if (regex.test(moduleId)) { 58 | clear(moduleId); 59 | } 60 | } 61 | }; 62 | 63 | clear.single = moduleId => { 64 | if (typeof moduleId !== 'string') { 65 | throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``); 66 | } 67 | 68 | delete require.cache[resolve(moduleId)]; 69 | }; 70 | 71 | module.exports = clear; 72 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import clear = require('.'); 2 | 3 | clear('my-module'); 4 | clear.all(); 5 | clear.match(/^.*$/); 6 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "clear-module", 3 | "version": "4.1.2", 4 | "description": "Clear a module from the cache", 5 | "license": "MIT", 6 | "repository": "sindresorhus/clear-module", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "engines": { 14 | "node": ">=8" 15 | }, 16 | "scripts": { 17 | "test": "xo && ava && tsd" 18 | }, 19 | "files": [ 20 | "index.js", 21 | "index.d.ts" 22 | ], 23 | "keywords": [ 24 | "clear", 25 | "module", 26 | "require", 27 | "import", 28 | "cache", 29 | "uncache", 30 | "uncached", 31 | "unrequire", 32 | "derequire", 33 | "delete", 34 | "remove", 35 | "rm", 36 | "fresh" 37 | ], 38 | "dependencies": { 39 | "parent-module": "^2.0.0", 40 | "resolve-from": "^5.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^2.1.0", 44 | "tsd": "^0.7.2", 45 | "xo": "^0.24.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # clear-module 2 | 3 | > Clear a module from the [cache](https://nodejs.org/api/modules.html#modules_caching) 4 | 5 | Useful for testing purposes when you need to freshly import a module. 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install clear-module 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | // foo.js 17 | let i = 0; 18 | module.exports = () => ++i; 19 | ``` 20 | 21 | ```js 22 | const clearModule = require('clear-module'); 23 | 24 | require('./foo')(); 25 | //=> 1 26 | 27 | require('./foo')(); 28 | //=> 2 29 | 30 | clearModule('./foo'); 31 | 32 | require('./foo')(); 33 | //=> 1 34 | ``` 35 | 36 | ## API 37 | 38 | ### clearModule(moduleId) 39 | 40 | #### moduleId 41 | 42 | Type: `string` 43 | 44 | What you would use with `require()`. 45 | 46 | ### clearModule.all() 47 | 48 | Clear all modules from the cache. 49 | 50 | ### clearModule.match(regex) 51 | 52 | Clear all matching modules from the cache. 53 | 54 | #### regex 55 | 56 | Type: `RegExp` 57 | 58 | Regex to match against the module IDs. 59 | 60 | ### clearModule.single(moduleId) 61 | 62 | Clear a single module from the cache non-recursively. No parent or children modules will be affected. 63 | 64 | This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects. 65 | 66 | #### moduleId 67 | 68 | Type: `string` 69 | 70 | What you would use with `require()`. 71 | 72 | ## Related 73 | 74 | - [import-fresh](https://github.com/sindresorhus/import-fresh) - Import a module while bypassing the cache 75 | - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path 76 | - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory 77 | - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily 78 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import clearModule from '.'; 3 | 4 | test('clearModule()', t => { 5 | const id = './fixture'; 6 | t.is(require(id)(), 1); 7 | t.is(require(id)(), 2); 8 | clearModule(id); 9 | t.is(require(id)(), 1); 10 | }); 11 | 12 | test('clearModule.all()', t => { 13 | t.true(Object.keys(require.cache).length > 0); 14 | clearModule.all(); 15 | t.is(Object.keys(require.cache).length, 0); 16 | }); 17 | 18 | test('clearModule.match()', t => { 19 | const id = './fixture'; 20 | const match = './fixture-match'; 21 | t.is(require(id)(), 1); 22 | t.is(require(match)(), 1); 23 | clearModule.match(/match/); 24 | t.is(require(id)(), 2); 25 | t.is(require(match)(), 1); 26 | }); 27 | 28 | test('clearModule() recursively', t => { 29 | clearModule.all(); 30 | const id = './fixture-with-dependency'; 31 | t.is(require(id)(), 1); 32 | t.is(require(id)(), 2); 33 | delete require.cache[require.resolve(id)]; 34 | t.is(require(id)(), 3); 35 | clearModule(id); 36 | t.is(require(id)(), 1); 37 | }); 38 | 39 | test('clearModule() recursively, multiple imports', t => { 40 | clearModule.all(); 41 | const id = './fixture-with-dependency'; 42 | t.is(require(id)(), 1); 43 | t.is(require(id)(), 2); 44 | t.is(require(id)(), 3); 45 | clearModule(id); 46 | t.is(require(id)(), 1); 47 | }); 48 | 49 | test('clearModule.single()', t => { 50 | clearModule.all(); 51 | const id = './fixture-with-dependency'; 52 | t.is(require(id)(), 1); 53 | t.is(require(id)(), 2); 54 | clearModule.single(id); 55 | t.is(require(id)(), 3); 56 | clearModule(id); 57 | t.is(require(id)(), 1); 58 | }); 59 | 60 | test('works with circular dependencies', t => { 61 | const id1 = './fixture-circular-1'; 62 | require(id1); 63 | 64 | let parentCalls = 0; 65 | let childrenCalls = 0; 66 | 67 | const {children, parents} = require.cache[require.resolve(id1)]; 68 | 69 | Object.defineProperty( 70 | require.cache[require.resolve(id1)], 71 | 'children', 72 | { 73 | get: () => { 74 | childrenCalls++; 75 | return children; 76 | } 77 | } 78 | ); 79 | 80 | Object.defineProperty( 81 | require.cache[require.resolve(id1)], 82 | 'parent', 83 | { 84 | get: () => { 85 | parentCalls++; 86 | return parents; 87 | } 88 | } 89 | ); 90 | 91 | clearModule(id1); 92 | t.is(parentCalls, 1); 93 | t.is(childrenCalls, 4); 94 | clearModule.all(); 95 | }); 96 | --------------------------------------------------------------------------------