reactjs - webpack.config 有一个未知属性“publicPath”
问题描述
无法启动 my-react-app,因为 webpack.cofig 文件有错误并且无法修复。到处搜索,但无法找到解决方案。谁能建议我该怎么做?
代码:
npx webpack-dev-server --mode development
错误:"[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options has an unknown property 'publicPath'. These properties are valid: object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, setupExitSignals?, static?, watchFiles?, webSocketServer? }"
Webpack.cofig 文件代码:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
options: { presets: ["@babel/env"] }
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
resolve: { extensions: ['*', 'js', 'jsx'] },
output: {
path: path.resolve(__dirname, '.dist/'),
publicPath: '/dist/',
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, 'public/'),
port: 3000,
publicPath: 'http://localhost:3000/dist/',
hot: 'only'
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
解决方案
该属性publicPath
应该用于静态内容,即devServer.static
开发服务器中间件选项devServer.devMiddleware
,而不是devServer
直接用于。阅读此处了解更多详情。
删除publicPath
和 webpack 应该可以正常工作。此外,publicPath
指向您尝试提供的目录。您使用了URL
无效的。
推荐阅读
- php - 插件停用代码不起作用后删除页面
- javascript - $.jsonCall 导致错误,但接收到正确的数据
- php - 在下载时使用 FPD 在 PDF 上添加密码
- javascript - 如何使用jquery对div内的所有值求和
- php - 我需要知道如何在 codeignator 中调用同一文件夹的备份?
- date - pyspark :将日期从这种格式 dd-MMM-yyyy hh:mm:ss 转换为 yyyy-mm-dd
- android - FCM 推送通知即使在实现 dat 消息后也会显示原始 json
- javascript - 在 if else jquery 中添加一个新函数
- c# - 正则表达式从 C# 中的字符串中提取 2 个整数值
- ios - iOS 应用程序在打开和关闭 WebView (WKWebView) 时崩溃