Webpack --watch doesn't auto refresh when altering files

49 Views Asked by At

Since updating webpack to webpack 5.90.1 my auto refresh when --watch flag is set on webpack has stopped working. Requiring me to run the command manually every time I make a change to my code.

Here's my devDependencies in my package.json

"devDependencies": {
  "@babel/preset-env": "^7.23.9",
  "css-loader": "^6.10.0",
  "html-webpack-plugin": "^5.6.0",
  "mkdirp": "^1.0.4",
  "style-loader": "^3.3.4",
  "webpack": "^5.90.1",
  "webpack-cli": "^4.10.0",
  "webpack-dev-server": "^4.15.1"}

I call my dev build command using the following webpack command

webpack --config webpack.dev.config.js

This runs fine, but then when I make any changes to my files it does not process the changed files like it used to.

I read online that perhaps it was to do with my paths not pointing to the right files, so I've tried to use path.resolve to help with that.

I'm using a Windows 11 machine running WSL to run my commands. The working directory is in the windows section of the machine.

Here is my webpack.dev.config.js file:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',
  watch: true,

  entry: {
    index: path.resolve(__dirname, "src", "index.js"),
    about: path.resolve(__dirname, "src", "about.js"),
  },

  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "./dev"),
  },

  devServer: {
    static: './dev',
    hot: true
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      inject: true,
      chunks: ['index'],
      filename: 'index.html'
    }),
    new HtmlWebpackPlugin({
      template: './src/about.html',
      inject: true,
      chunks: ['about'],
      filename: 'about.html'
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
    ],
  },
};
1

There are 1 best solutions below

2
biodiscus On

try to change your devServer config:

  devServer: {
    static: path.join(__dirname, 'dev'),
    watchFiles: {
      paths: ['src/**/*.*'], // <= watching files
      options: {
        usePolling: true,
      },
    },
  },