Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing Webpack cache

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?

like image 437
Katana24 Avatar asked Nov 22 '16 13:11

Katana24


People also ask

What is cache busting in webpack?

Cache busting means making sure the users browser loads the newest version of our app, if it has been updated, instead of using a cached version. A naive webpack setup would just use a single output file with a fixed name. const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.

How do I reduce webpack build time?

Use the uglifyjs-webpack-plugin v1 It is one of the main methods used to reduce the build time. But this modification process itself can take a considerable amount of time as the project size increases. So if your project is scaling, you can use uglifyjs-webpack-plugin v1 to optimize the modification time.

Why is my webpack build slow?

Over the past eight years, webpack has become increasingly powerful. However, due to the additional packaging process, the building speed is getting slower as the project grows. As a result, each startup takes dozens of seconds (or minutes), followed by a round of build optimization.


5 Answers

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

like image 156
Fernando Vieira Avatar answered Oct 05 '22 18:10

Fernando Vieira


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.

like image 29
Joe.b Avatar answered Oct 05 '22 17:10

Joe.b


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.

like image 41
Benny Neugebauer Avatar answered Oct 05 '22 17:10

Benny Neugebauer


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.

like image 40
Seyhan Avatar answered Oct 05 '22 19:10

Seyhan


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.

like image 42
John Mee Avatar answered Oct 05 '22 19:10

John Mee