Webpack Dev Server(webpack-dev-server)热模块替换(HMR)不起作用

时间:2016-03-25 00:27:22

标签: javascript reactjs webpack webpack-dev-server react-hot-loader

我在StackOverflow&在GitHub问题上,但是,我仍然陷入Webpack中的热模块替换。我使用npm start使用webpack-dev-server --hot --inline运行我的服务器。 我正在尝试更改React组件中的代码,但浏览器中没有任何内容

我在Ubuntu 14.04LTS上使用谷歌浏览器版本49.0.2623.87(64位)。

在我的浏览器console中,我收到日志消息

  

[HMR]等待来自WDS的更新信号...

     

[WDS]启用热模块更换。

但是,没有热/重新加载正在发生。当我在React组件文件中更改代码时,没有显示任何内容。我正在关注本教程的第一个视频Egghead.io/ReactFundamentals,其中一切正常。

以下是我的package.json& webpack.config.js文件。

的package.json

{
  "name": "react-fundamentals",
  "version": "1.0.0",
  "description": "Fundamentals of ReactJS",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot --inline"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.0.0-rc.2",
    "react-dom": "^15.0.0-rc.2"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.7.2",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  }
}

webpack.config.js

module.exports = {
  context: __dirname,
  entry: "./main.js",
  output: {
    path: __dirname,
    filename: "bundle.js"
  },
  devServer: {
    port: 7777
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel",
        query: {
          presets: ["es2015", "react"]
        }
      }
    ]
  }
}

如果有人可以指导我完成这个问题,那将会很棒。因为我无法进一步有效地完成本教程。

  

更新我已在下面发布了答案。

6 个答案:

答案 0 :(得分:5)

我自己想出了解决方案。

我必须使用sudo运行我的服务器。而不是npm start,它必须是sudo npm start

希望它有所帮助!

答案 1 :(得分:3)

devServer: {
 inline: true, // you missed this line which will reload the browser
 port : 7777
}

答案 2 :(得分:3)

您的网络包配置已关闭。请查看react-transform-boilerplate以获取正确的设置。

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"]
    }
  }
}

答案 3 :(得分:0)

我使用以下版本: “webpack”:“~1.12.14”  “webpack-hot-middleware”:“^ 2.10.0” “webpack-dev-middleware”:“^ 1.6.1”

我在react.js项目的app.js中使用了以下代码。

    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);
        });
       }

相同的代码在其他员工计算机上被热替换,但并非总是如此,但很多时候热替换在我的计算机中不起作用。

答案 4 :(得分:0)

我刚刚删除了node_modules文件夹和package-lock.json文件。然后重新运行npm install。奏效了!

答案 5 :(得分:-2)

尝试将模块加载器更新为:

loaders: [
      {
        test: /\.jsx$/,
        exclude: /node_modules/,
        loaders: ["react-hot", "babel"],
        query: {
          presets: ["es2015", "react"]
        }
      }
    ]