Uncaught Error: [HMR] Hot Module Replacement is disabled
Uncaught Error: [HMR] Hot Module Replacement is disabled
我正在使用 webpack
进行构建,使用 webpack-dev-server
(npm run watch
) 可以正常工作,但是当我尝试创建生产构建时 (npm run build
) 当我尝试加载网站并且屏幕上什么也没有显示时,我似乎在控制台中收到错误消息。
Uncaught Error: [HMR] Hot Module Replacement is disabled.
我对此有几个问题:
我对使用 Hot Module Replacement
的理解是,它旨在让开发过程中的生活更轻松,不应在生产部署中使用。对吗?
鉴于以下,为什么要使用 Hot Module Replacement
?我不明白是什么在驱动它。
关于生产构建的最佳实践是什么,我是否应该为生产和开发设置单独的 webpack
配置?理想情况下,我希望纯粹使用单个配置来简化维护。
package.json
{
// ...
"scripts": {
"build": "webpack --progress --colors --production",
"watch": "webpack-dev-server --inline --hot --progress --colors"
},
"dependencies": {
"bootstrap": "^3.3.6",
"bootstrap-sass": "^3.3.6",
"bootstrap-webpack": "0.0.5",
"extract-text-webpack-plugin": "^1.0.1",
"html-webpack-plugin": "^2.15.0",
"jquery": "^2.2.3",
"node-sass": "^3.4.2",
"react": "^15.0.1",
"react-bootstrap": "^0.28.5",
"react-dom": "^15.0.1",
"react-redux": "^4.4.1",
"react-router": "^2.0.1",
"react-router-redux": "^4.0.1",
"redux": "^3.3.1",
"redux-thunk": "^2.0.1",
"webpack": "^1.12.14"
},
"devDependencies": {
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"exports-loader": "^0.6.3",
"file-loader": "^0.8.5",
"imports-loader": "^0.6.5",
"less": "^2.6.1",
"less-loader": "^2.2.3",
"redux-devtools": "^3.2.0",
"redux-logger": "^2.6.1",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack-dev-server": "^1.14.1"
}
}
webpack.config.js
var webpack = require('webpack');
var htmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
module.exports = {
entry: [
'webpack/hot/only-dev-server',
path.resolve(__dirname, 'app/index.js')
],
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel', query: { presets: [ 'es2015', 'react' ] } },
{ test: /\.scss$/, loader: 'style!css!sass?includePaths[]=' + path.resolve(__dirname, './node_modules/bootstrap-sass/assets/stylesheets/') },
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, loader: 'file' }
]
},
resolve: {
modulesDirectories: ['node_modules']
},
plugins: [
new webpack.NoErrorsPlugin(),
new htmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
inject: false
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.optimize.OccurenceOrderPlugin()
]
};
在生产构建中包含热加载位并不是一个特别好的主意。它们在那里几乎毫无用处,只会膨胀你的文件大小。
有多种策略可以解决这个问题。有些人将他们的 webpack 配置按文件分开,然后通过 --config
指向它。我更喜欢通过 npm 维护单个文件和分支。我使用 webpack-merge 在分支之间共享配置(免责声明:我是作者)。
您需要启用 HMR 插件。将 HotModuleReplacementPlugin 添加到你 webpack.config 中的插件数组中。您可以为开发和生产设置单独的 webpack.config。
类似于
plugins: [
new webpack.NoErrorsPlugin(),
new htmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
inject: false
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
]
我收到这个错误是因为我在 webpack.config.js
中有这样的代码。
devServer: {
...
hot: true,
inline: true
}
改为使用命令 webpack-dev-server --hot --inline
,然后我就不必使用 new webpack.HotModuleReplacementPlugin()
来膨胀我的配置了。
https://github.com/webpack/webpack/issues/1151#issuecomment-111972680
您需要启用热模块更换功能才能使其正常工作,例如:
webpack.config.js
module.exports = {
//...
devServer: {
hot: true
},
plugins: [
//...
new webpack.HotModuleReplacementPlugin()
]
};
来自网络包:
Note that webpack.HotModuleReplacementPlugin is required to fully enable HMR. If webpack or webpack-dev-server are launched with the --hot option, this plugin will be added automatically, so you may not need to add this to your webpack.config.js. See the HMR concepts page for more information.
如上所述,否则您可以通过 package.json
启用它,将 --hot
添加到您的脚本中,例如
"start": "webpack-dev-server --hot",
我正在使用 webpack
进行构建,使用 webpack-dev-server
(npm run watch
) 可以正常工作,但是当我尝试创建生产构建时 (npm run build
) 当我尝试加载网站并且屏幕上什么也没有显示时,我似乎在控制台中收到错误消息。
Uncaught Error: [HMR] Hot Module Replacement is disabled.
我对此有几个问题:
我对使用
Hot Module Replacement
的理解是,它旨在让开发过程中的生活更轻松,不应在生产部署中使用。对吗?鉴于以下,为什么要使用
Hot Module Replacement
?我不明白是什么在驱动它。关于生产构建的最佳实践是什么,我是否应该为生产和开发设置单独的
webpack
配置?理想情况下,我希望纯粹使用单个配置来简化维护。
package.json
{
// ...
"scripts": {
"build": "webpack --progress --colors --production",
"watch": "webpack-dev-server --inline --hot --progress --colors"
},
"dependencies": {
"bootstrap": "^3.3.6",
"bootstrap-sass": "^3.3.6",
"bootstrap-webpack": "0.0.5",
"extract-text-webpack-plugin": "^1.0.1",
"html-webpack-plugin": "^2.15.0",
"jquery": "^2.2.3",
"node-sass": "^3.4.2",
"react": "^15.0.1",
"react-bootstrap": "^0.28.5",
"react-dom": "^15.0.1",
"react-redux": "^4.4.1",
"react-router": "^2.0.1",
"react-router-redux": "^4.0.1",
"redux": "^3.3.1",
"redux-thunk": "^2.0.1",
"webpack": "^1.12.14"
},
"devDependencies": {
"babel-core": "^6.7.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"exports-loader": "^0.6.3",
"file-loader": "^0.8.5",
"imports-loader": "^0.6.5",
"less": "^2.6.1",
"less-loader": "^2.2.3",
"redux-devtools": "^3.2.0",
"redux-logger": "^2.6.1",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack-dev-server": "^1.14.1"
}
}
webpack.config.js
var webpack = require('webpack');
var htmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
module.exports = {
entry: [
'webpack/hot/only-dev-server',
path.resolve(__dirname, 'app/index.js')
],
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel', query: { presets: [ 'es2015', 'react' ] } },
{ test: /\.scss$/, loader: 'style!css!sass?includePaths[]=' + path.resolve(__dirname, './node_modules/bootstrap-sass/assets/stylesheets/') },
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, loader: 'file' }
]
},
resolve: {
modulesDirectories: ['node_modules']
},
plugins: [
new webpack.NoErrorsPlugin(),
new htmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
inject: false
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.optimize.OccurenceOrderPlugin()
]
};
在生产构建中包含热加载位并不是一个特别好的主意。它们在那里几乎毫无用处,只会膨胀你的文件大小。
有多种策略可以解决这个问题。有些人将他们的 webpack 配置按文件分开,然后通过 --config
指向它。我更喜欢通过 npm 维护单个文件和分支。我使用 webpack-merge 在分支之间共享配置(免责声明:我是作者)。
您需要启用 HMR 插件。将 HotModuleReplacementPlugin 添加到你 webpack.config 中的插件数组中。您可以为开发和生产设置单独的 webpack.config。
类似于
plugins: [
new webpack.NoErrorsPlugin(),
new htmlWebpackPlugin({
filename: 'index.html',
template: './index.html',
inject: false
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
]
我收到这个错误是因为我在 webpack.config.js
中有这样的代码。
devServer: {
...
hot: true,
inline: true
}
改为使用命令 webpack-dev-server --hot --inline
,然后我就不必使用 new webpack.HotModuleReplacementPlugin()
来膨胀我的配置了。
https://github.com/webpack/webpack/issues/1151#issuecomment-111972680
您需要启用热模块更换功能才能使其正常工作,例如:
webpack.config.js
module.exports = {
//...
devServer: {
hot: true
},
plugins: [
//...
new webpack.HotModuleReplacementPlugin()
]
};
来自网络包:
Note that webpack.HotModuleReplacementPlugin is required to fully enable HMR. If webpack or webpack-dev-server are launched with the --hot option, this plugin will be added automatically, so you may not need to add this to your webpack.config.js. See the HMR concepts page for more information.
如上所述,否则您可以通过 package.json
启用它,将 --hot
添加到您的脚本中,例如
"start": "webpack-dev-server --hot",