12

我在 StackOverflow 和 GitHub 问题上也经历了很多答案,但是,我仍然被困在 Webpack 中的热模块替换中。我npm start用来运行我的服务器webpack-dev-server --hot --inline我正在尝试更改我的 React 组件中的代码,但浏览器中没有任何反应

我在 Ubuntu 14.04LTS 上使用 Google Chrome 版本 49.0.2623.87(64 位)。

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

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

[WDS] 热模块更换已启用。

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

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

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

如果有人能指导我解决这个问题,那就太好了,因为我无法进一步有效地学习本教程。

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

4

6 回答 6

5

我自己想出了解决方案。

我必须使用sudo. 而不是npm start,它必须是sudo npm start

希望能帮助到你!

于 2016-04-26T07:46:27.633 回答
3
devServer: {
 inline: true, // you missed this line which will reload the browser
 port : 7777
}
于 2016-03-25T03:33:35.073 回答
3

您的 webpack 配置已关闭。查看react-transform-b​​oilerplate以获得正确的设置。

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"]
    }
  }
}
于 2016-03-25T04:40:41.430 回答
1

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

于 2019-06-10T03:49:28.520 回答
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);
        });
       }

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

于 2016-11-01T12:29:52.240 回答
-2

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

loaders: [
      {
        test: /\.jsx$/,
        exclude: /node_modules/,
        loaders: ["react-hot", "babel"],
        query: {
          presets: ["es2015", "react"]
        }
      }
    ]
于 2016-03-25T02:50:01.330 回答