我试图用几种不同的方法解决这个问题,所以我必须从头开始。webpack-dev-server运行两次
我有一个名为webpack.dev.js
一个配置文件,如下图所示:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
module.exports = {
entry: "./src/script.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
devtool: "inline-source-map",
devServer: {
contentBase: path.join(__dirname, "dist")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["env"]
}
}
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ["css-loader", "postcss-loader", "sass-loader"]
})
}
]
},
plugins: [
new HtmlWebpackPlugin({template: path.join("src", "index.html")}),
new ExtractTextPlugin("style.css"),
new CopyWebpackPlugin([{from: "src/images", to: "images"}])
]
};
所以,我成立的package.json启动脚本用于启动开发服务器
"start": "webpack-dev-server --config webpack.dev.js"
现在是问题开始的地方。当我跑了剧本,我有以下错误
Invalid configuration object. webpack-dev-server has been initialized using a configuration object that does not match the API schema.
- configuration has an unknown property 'error'. These properties are valid:
object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, noInfo?, quiet?, serverSideRender?, index?, log?, warn? }
正如你所看到的,这个错误是非常令人困惑,因为没有任何error
财产上的配置文件
尝试不同的方法来后解决这个问题,我试图删除devServer
属性,并使用默认设置启动dev服务器。
但现在是它变得奇怪时。输出看起来就像如果Web服务器启动了两次:
Project is running at http://localhost:8080/
webpack output is served from/
Project is running at http://localhost:8081/
webpack output is served from/
之后,它会记录有关有被multiple modules with names that only differ in casing
几个警告然后一些谷歌上搜索后,我发现了别人也有这个unknown property 'error'
问题,他发生的原因是他有http-server
在后台运行。
所以现在,我的理论是,由于某种原因,webpack-dev-server并行运行两次,并且会产生竞争条件或错误,从而引发此unknown property 'error'
问题。
我只发现了另外两个有类似问题的人,他们通过将inject: false
添加到HtmlWebpackPlugin
的配置对象中而被修复。这样做并没有使错误消失,并且在没有devServer配置的情况下运行时,它只是从页面中删除所有js和css,因为它不会将<link>
和<script>
标签注入到html中。
在这一点上,我不知道如何解决这个问题,这就是为什么我问是否有人可以帮助我。
我看到一个已经工作好几个月的现有项目的同样的事情。一位同事正在试图起床并运行,并且正在用这种短暂的'错误'属性来解决这个问题。它没有任何意义,因为我们在docker中运行所有的东西,我们使用npm-shrinkwrap ...因此他具有与其他人相同的环境。确认他与团队的其他成员拥有相同的webpack和webpack-dev-server版本...... – Ghazgkull
获得同样的结果。也许它与最新版本有关? –
同样的错误今天开始发生在我身上。 – corsen