Skip to content
the poor coder

clear cache for require module

Poor Man
Published
Reading time
1 min read
On this page

Clear Cache for Require Module#

If you are a developer who uses Node.js, then you must be familiar with the require() function which is used to import modules in your code. Sometimes, you might face issues with the modules due to cache. In such cases, you need to clear the cache for the require module.

Method 1: Using delete#

Text

delete require.cache[require.resolve('./your_module')];

This method is very simple and easy to understand. In this method, you just need to use the delete keyword followed by require.cache and then specify the path of the module which needs to be cleared from cache using require.resolve().

Method 2: Using purge-module-cache Package#

Text

const purgeCache = require('purge-module-cache');
purgeCache.all();

This method is more efficient and easy to use if you have multiple modules that need to be cleared from cache. In this method, you need to install the purge-module-cache package using npm install purge-module-cache. After installation, you just need to require it in your code and call its all() method to clear cache for all modules.

When to Clear Cache?#

You might wonder when you should clear cache for the require module. Well, there are some situations where you might need to do it:

  • If you made changes to the module and it's not reflecting in your code
  • If you are using a library that caches the modules
  • If you are running multiple instances of your application and don't want to share cache between them

In conclusion, clearing cache for the require module is an important aspect of Node.js development. You can use either of the methods mentioned above depending on your need and preference.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC