Webpack Clear Require Cache Plugin
raw JSON → 0.0.5 verified Sat Apr 25 auth: no javascript maintenance
A webpack plugin that clears modules from Node.js require.cache after the webpack compilation's afterEmit hook. Version 0.0.5, stable but limited maintenance. It accepts an array of regex patterns; during compilation, each cached module path is tested and removed if it matches. Primarily useful for development with npm-linked packages that need cache invalidation on rebuild. Works with webpack and Next.js servers. Simpler than using Nodemon or chokidar-based solutions, but limited to webpack-based projects.
Common errors
error TypeError: clearRequireCachePlugin is not a constructor ↓
cause Using 'new' on the module export, which is a function, not a class.
fix
Use clearRequireCachePlugin([...]) without 'new'.
error Cannot find module 'webpack-clear-require-cache-plugin' ↓
cause Missing npm install or incorrect import path.
fix
Run npm install --save-dev webpack-clear-require-cache-plugin and require with correct casing.
error Module not found: Error: Can't resolve 'webpack-clear-require-cache-plugin' ↓
cause Using ES import syntax for a CommonJS-only package.
fix
Change to const clearRequireCachePlugin = require('webpack-clear-require-cache-plugin');
Warnings
gotcha Plugin only works in Node.js environments (e.g., webpack dev server, Next.js server-side builds). Does not affect client-side webpack bundles. ↓
fix Ensure you only use this plugin in server-side webpack configurations.
deprecated Package has not been updated since 2018; may not be compatible with webpack 5+ or Next.js built-in features. ↓
fix Consider using native webpack module federation or dedicated cache-clearing plugins for modern webpack.
gotcha Regex patterns are tested against full module paths, not just module names. Include path separators or anchors if needed. ↓
fix Use patterns like /node_modules\/my-module/ to avoid clearing unrelated modules.
gotcha Plugins that rely on require.cache may not work with webpack 5's persistent caching (filesystem cache). ↓
fix Disable filesystem cache when using this plugin: config.cache = false.
Install
npm install webpack-clear-require-cache-plugin yarn add webpack-clear-require-cache-plugin pnpm add webpack-clear-require-cache-plugin Imports
- clearRequireCachePlugin wrong
import clearRequireCachePlugin from 'webpack-clear-require-cache-plugin';correctconst clearRequireCachePlugin = require('webpack-clear-require-cache-plugin'); - plugin instance wrong
new clearRequireCachePlugin({ patterns: [/my-module/] })correctclearRequireCachePlugin([/my-module/])
Quickstart
const clearRequireCachePlugin = require('webpack-clear-require-cache-plugin');
module.exports = {
// ... other webpack config
plugins: [
clearRequireCachePlugin([
/my-module/,
]),
],
};