Home > Blockchain >  Webpack is compiling successfully but changes are not reflected in React App
Webpack is compiling successfully but changes are not reflected in React App

Time:08-07

I am using Django to host the react app which is being compiled by babel and bundled using webpack. The webpack shows me a compiled successfully message like below but the changes aren't reflected. The funny thing is when I wait for a bit, it works but that too sometimes with no linear pattern. I am getting mad about this, please help! I saw that, since I am using OSX, it is getting corrupted as the github issue says: https://github.com/webpack/webpack-dev-server/issues/24 but it is not helping!

npm run dev

> [email protected] dev
> webpack --mode development --watch

asset main.js 1.32 MiB [compared for emit] [minimized] (name: main) 1 related asset
runtime modules 1.04 KiB 5 modules
modules by path ./node_modules/ 1.2 MiB
  modules by path ./node_modules/axios/ 56.8 KiB 32 modules
  modules by path ./node_modules/react-dom/ 1000 KiB 3 modules
  modules by path ./node_modules/react/ 85.7 KiB 2 modules
  modules by path ./node_modules/scheduler/ 17.3 KiB 2 modules
    5 modules
modules by path ./src/ 36.6 KiB
  modules by path ./src/components/*.js 30.3 KiB
    ./src/components/App.js 1.58 KiB [built] [code generated]
      9 modules
  modules by path ./src/*.js 6.33 KiB
    ./src/index.js 35 bytes [built] [code generated]
      2 modules
webpack 5.74.0 compiled successfully in 2567 ms
assets by status 1.32 MiB [cached] 1 asset
cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules
./src/components/ProfilePage.js 4.05 KiB [built]
webpack 5.74.0 compiled successfully in 209 ms
assets by status 1.32 MiB [cached] 1 asset
cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules
./src/components/ProfilePage.js 4.05 KiB [built]
webpack 5.74.0 compiled successfully in 96 ms
assets by status 1.32 MiB [cached] 1 asset
cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules
./src/components/ProfilePage.js 4.05 KiB [built]
webpack 5.74.0 compiled successfully in 119 ms
assets by status 1.32 MiB [cached] 1 asset
cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules
./src/components/ProfilePage.js 4.05 KiB [built]
webpack 5.74.0 compiled successfully in 91 ms
assets by status 1.32 MiB [cached] 1 asset
cached modules 1.23 MiB (javascript) 1.04 KiB (runtime) [cached] 61 modules

Webpack config:

const path = require("path");
const webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "./static/frontend/"),
    filename: "[name].js",
    publicPath: "/static/frontend/",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  },
  optimization: {
    minimize: true,
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": {
        // This has effect on the react lib size
        NODE_ENV: JSON.stringify("development"),
      },
    }),
  ],
};

Babel config

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": ["@babel/plugin-proposal-class-properties"]
}

package.json

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development --watch",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.18.9",
    "@babel/preset-env": "^7.18.9",
    "@babel/preset-react": "^7.18.6",
    "babel-loader": "^8.2.5",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0"
  },
  "dependencies": {
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@emotion/react": "^11.10.0",
    "@emotion/styled": "^11.10.0",
    "@mui/icons-material": "^5.8.4",
    "@mui/material": "^5.9.2",
    "axios": "^0.27.2",
    "bootstrap-icons": "^1.9.1",
    "jwt-decode": "^3.1.2",
    "react-router-dom": "^6.3.0"
  }
}

CodePudding user response:

You can try this!

module.exports = {
  mode: 'development',
  devServer: {
    port:  process.env.PORT || 3000,
    historyApiFallback: true,
    open: true,
    compress: true,
    
    // Setup output directory
    static: {
      directory: path.join(__dirname, 'public'),
      watch: {
        ignored: /node_modules/,
      },
    },
  },
}

CodePudding user response:

I have faced this problem before and our webpack is quite similar. for me, I'm using Chrome and I just clicked hard reload in the browser then the problem was gone. your browser might keep your cache.

for more information: https://stackoverflow.com/a/3951207/12439451

If it's not fixed, please check your path in webpack again.

  • Related