I've set up WebPack successfully - it's compiling my babel and SCSS files just fine, and I got the watch functionality to work fine. But I'd also like to work with the Hot Module Replacement - and I'm having difficulties getting it going. When I load the dev server in my browser it shows Cannot resolve module 'webpack/hot/dev-server'
. My config looks like this:
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() {});
and my index.html contains:
<script src="http://localhost:8080/webpack-dev-server.js"></script>
<script src='main.js'></script>
Does anyone have any ideas?
To enabling HMR in your project, you need to let your application know how to handle hot updates. You can do so by implementing the module. hot API exposed by Webpack. Once the hot update is accepted, the HMR runtime and the loaders will take over to handle the update.
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways: Retain application state which is lost during a full reload. Save valuable development time by only updating what's changed.
If you installed webpack-dev-server
globally, which is npm install webpack-dev-server -g
, try install it locally (just remove the option -g
).
I solved this problem by doing so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With