51

How do you go about forcing webpack to clear its cache?

I'm doing a-lot of work with threejs and webpack and for some reason, unbeknownst to me, it has two copies of threejs in memory. Here's the error:

enter image description here

This file isn't located in a hidden folder in the app folder but in webpacks memory found via the Chrome Dev tools - i.e.

enter image description here

So is there anyway to force webpack to clear it's cache?

Katana24
  • 8,706
  • 19
  • 76
  • 118

9 Answers9

53

You can delete the folder "node_modules/.cache" to force Webpacker/Babel to rebuild their caches.

Fernando Vieira
  • 3,177
  • 29
  • 26
  • 3
    I had an issue with `eslint-loader` throwing errors about a previous file system state. Removing this directory seems to have fixed the issue. – daniels Jul 15 '21 at 09:12
  • Same as @daniels. Was getting warnings about imports that no longer existed. Deleting .cache and letting it rebuild .eslintcache seemed to sort it out. – Jarrod McGuire Jul 30 '21 at 10:22
9

Webpack 5 stores the cache into node_modules/.cache/webpack (when using npm) or .yarn/.cache/webpack (when using Yarn Plug'n'Play API), see Persistent Caching.

You can deactivate the caching completely by setting cache: false in your webpack config.

If you just want to delete your cache, then please do the following:

  1. Delete node_modules/.cache/webpack or .yarn/.cache/webpack
  2. Restart your webpack build (just hot reloading is not enough as files will still be cached in-memory)

When using the webpackDevMiddleware, then the .cache directory is created on your first page visit. I had to restart my dev server after deleting .cache to see the effects of changed files within my "node_modules" directory.

Benny Code
  • 51,456
  • 28
  • 233
  • 198
6

In case this helps anyone else, I had a similar case and the issue was that in one of the files my import statement had an uppercase in the name. For example, I had

import {Person} from './model/Model';

Note that I had deleted the file ./model/Model.js but was still getting the error due to the import. Just change the import to be

import {Person} from './model/model';

and all is well again.

Joe.b
  • 422
  • 1
  • 6
  • 16
3

I had a very similar problem, except webpack was caching a CSS file. My angular workspace doesn't have a node_modules/.cache folder, but I deleted the .angular/cache folder and that solved my problem. I also had to stop and restart the ng serve command after deleting that folder

NJS
  • 426
  • 4
  • 15
2

You can set cache: false in webpack config. This worked for me on Next.js.

1

As the warning says, you have two copies of three.js in directories which have the same effective name when you ignore capitalization: 'three' vs 'THREE' are the same.

If they are different then rename one of them. Or, if they are the same module, give them both an identical name, in lowercase.

John Mee
  • 50,179
  • 34
  • 152
  • 186
0
ng cache clean

Based on the documentation

Simple Dev
  • 113
  • 9
-2

To force clearing cache in the browser, you could load the generated js bundle file dynamically with javascript with a query parameter in the src attribute, then call a function to execute other scripts when the file is loaded:

<script type="text/javascript">
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.onload = function () {
        init(); // call other scripts when this file is fully loaded
    };
    script.src = 'dist/bundle.js?v='+Date.now();

    document.getElementsByTagName('body')[0].appendChild(script);
</script>

<script>
    function init(argument) {
        // other scripts
        console.log('dist/bundle.js is loaded from the server not from the cache')
    }
</script>

see: Dynamically load JS inside JS

Eissa
  • 325
  • 2
  • 11
-3

Webpack doesn't have caching but browsers have. Files produced by Webpack compilation can remain cached unless their content has changed. On the documentation they explain that to solve this issue you can add [contenthash] to your output file names.

The [contenthash] substitution will add a unique hash based on the content of an asset. When the asset's content changes, [contenthash] will change as well.

module.exports = {
    output: {
        filename: '[name].[contenthash].js',
        path: path.resolve(__dirname, 'dist')
    }
};

PS: I'm using Webpack v4.30.0

For more, checkout Webpack's guide for caching.

Seyhan
  • 632
  • 7
  • 11
  • 1
    My [contenthash] is changing and I confirmed that BUT the browser still uses the previous [contenthash] in the browser. i.e. the browser does not reload automatically. How does the browser detect the new [contenthash] js file?? – logixplayer May 21 '20 at 13:01