无法让webpack热模块更换工作

时间:2015-03-26 22:51:58

标签: webpack webpack-dev-server

我已经成功设置了WebPack - 它正在编译我的babel和SCSS文件就好了,我的手表功能正常工作。但是我也想使用热模块更换 - 而且我遇到了困难。当我在浏览器中加载开发服务器时,它会显示Cannot resolve module 'webpack/hot/dev-server'。我的配置如下所示:

import webpack from 'webpack';
import wpServer from 'webpack-dev-server';

var compiler = webpack({
    entry: [
        './src/core.js',
        'webpack/hot/dev-server'
    ],
    output: {
        path: outPath,
        filename: '[name].js'
    },
    resolveLoader: { root: path.join(MODULE_PATH, "node_modules") },
    module: {
        loaders: [
          { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
          { test: /\.scss$/, loader: "style!css!sass" }
        ]
    },
    plugins: [new webpack.HotModuleReplacementPlugin()],
    watch: true
});

var server = new wpServer(compiler, {
    contentBase: outPath,
    hot: true,
    quiet: false,
    noInfo: false,
    lazy: true,
    filename: "main.js",
    watchDelay: 300,
    headers: { "X-Custom-Header": "yes" },
    stats: { colors: true },
});
server.listen(8080, "localhost", function() {});

我的index.html包含:

<script src="http://localhost:8080/webpack-dev-server.js"></script>
<script src='main.js'></script>

有没有人有任何想法?

3 个答案:

答案 0 :(得分:14)

如果您在全球范围内webpack-dev-server安装了npm install webpack-dev-server -g,请尝试在本地安装(只需删除选项-g)。

我这样解决了这个问题。

答案 1 :(得分:6)


IMPORTANT


If you are using webpack@2.x.x.beta make sure you install

webpack-dev-server@2.0.0-beta 

Just running npm install webpack-dev-server won't play nice with webpack 2.

This will probably only be true while version 2 is still in beta.


I also struggled getting this working since the documentation on this topic is very fragmented.

Found this simple working example:

https://github.com/ahfarmer/webpack-hmr-starter-dev-server-api

Code is pretty self explanatory.

答案 2 :(得分:4)

我遇到了类似的问题。我通过在本地安装webpack来修复它。 要将webpack安装为本地dev依赖项: npm i -D webpack