17

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
  • 7,554
  • 16
  • 67
  • 110

5 Answers5

3

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
  • 372
  • 3
  • 16
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
  • 44,003
  • 31
  • 133
  • 171
1

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
  • 605
  • 7
  • 9
  • 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
0

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

Fernando Vieira
  • 2,302
  • 22
  • 23
-1

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
  • 317
  • 2
  • 11