Webpack Dev Server (webpack-dev-server) Hot Module Replacement (HMR) Not Working

前端 未结 6 2762
执笔经年
执笔经年 2021-02-19 22:48

I have gone through many answers on StackOverflow & on GitHub issues as well but, I am still stuck in Hot Module Replacement in Webpack. I am using npm start to

相关标签:
6条回答
  • 2021-02-19 23:08

    Try updating your module loader to this:

    loaders: [
          {
            test: /\.jsx$/,
            exclude: /node_modules/,
            loaders: ["react-hot", "babel"],
            query: {
              presets: ["es2015", "react"]
            }
          }
        ]
    
    0 讨论(0)
  • 2021-02-19 23:12
    devServer: {
     inline: true, // you missed this line which will reload the browser
     port : 7777
    }
    
    0 讨论(0)
  • 2021-02-19 23:18

    I use the following version : "webpack": "~1.12.14" "webpack-hot-middleware": "^2.10.0" "webpack-dev-middleware": "^1.6.1"

    And I use following code in app.js in my react.js project.

        var webpackconfig =require('./webpack.config');
        var webpack = require('webpack');
        var webpackMiddleware = require('webpack-dev-middleware');
        var webpackHotMiddleware = require('webpack-hot-middleware');
    
        var http = require('http');
        var express = require('express');
        var app = require('express')();
        var isDeveloping = process.env.NODE_ENV != 'production';
        // var isDeveloping = false;
         console.log("IS developing ",isDeveloping);
       var serverConfig = require('./globalconfig.js')
    
       var serverPort = serverConfig.port
    
       app.get('/css/bootstrap.min.css', function (req, res) {
       res.sendFile(path.join(__dirname,           'public/lib/bootstrap/css/bootstrap.min.css'));
       });
    
    
    
         // swaggerRouter configuration
         var options = {
         controllers: './controllers',
         useStubs: process.env.NODE_ENV === 'development' ? true : false    // Conditionally turn on stubs (mock mode)
         }
    
         var config = {
          appRoot: __dirname // required config
          }
    
    
         // Start the server
         app.listen(serverPort, function () {
           console.log('Your server is listening * on port %d        (http://localhost:%d)', serverPort, serverPort);
    });
    
    
         if (isDeveloping) {
            app.use('/node_modules', express.static('/node_modules'));
            app.use(express.static('src/web-ui/public/'));
            app.use(express.static('src/web-ui/public/'));
            const compiler = webpack(webpackconfig);
            const middleware = webpackMiddleware(compiler,{
             publicPath: webpackconfig.output.publicPath,
            headers: {
              "Cache-Control" : "public, max-age=604800"
            },
           constentBase:'dist',
           stats:{
             color:true,
             hash:false,
             timings:true,
              chunks:false,
             chunkModules:false,
             modules:false
           }
    
          });
          app.use(middleware);
          app.use(webpackHotMiddleware(compiler));
          app.get('/',function response(req,res){
                             res.write(middleware.fileSystem.readFileSync(path.join(_dirname,'dist/index.html')));
           res.end();
           });
        } else {
           app.use('/node_modules', express.static('/node_modules'));
           app.use(express.static('dist/public'));
           app.use(express.static('dist'));
    
           app.get('/', function response(req, res,next) {
             console.log("Processing req");
             var entryFile = path.join(__dirname, 'dist', 'index.html');
              console.log("Hitting the Root",entryFile);
              res.sendFile(entryFile);
            });
           }
    

    Same code get hot replaced on other employees computer,but not always,but many times hot replace won't work in my computer.

    0 讨论(0)
  • 2021-02-19 23:19

    Your webpack config is off. Take a look at react-transform-boilerplate for a correct setup.

    webpack.config.js

    var path = require('path');
    var webpack = require('webpack');
    
    module.exports = {
      // or devtool: 'eval' to debug issues with compiled output:
      devtool: 'cheap-module-eval-source-map',
      entry: [
        // necessary for hot reloading with IE:
        'eventsource-polyfill',
        // listen to code updates emitted by hot middleware:
        'webpack-hot-middleware/client',
        // your code:
        './src/index'
      ],
      output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/dist/'
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
      ],
      module: {
        loaders: [{
          test: /\.js$/,
          loaders: ['babel'],
          include: path.join(__dirname, 'src')
        }]
      }
    };
    

    .babelrc

    {
      "presets": ["react", "es2015"],
      "env": {
        "development": {
          "presets": ["react-hmre"]
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-19 23:19

    I just deleted node_modules folder and package-lock.json file. And then re-run npm install. It worked!

    0 讨论(0)
  • 2021-02-19 23:20

    I figured out the solution myself.

    I have to run my server with sudo. Instead of npm start, it has to be sudo npm start.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题