首页 > 解决方案 > 在最新版本的 webpack 上运行脚本监视 - 无效的配置对象

问题描述

运行此命令时

npm run-script watch

我有这个错误

npm ERR! command sh -c webpack --watch -d --output ./target/classes/static/built/bundle.js

和这个

[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration.devtool should match pattern "^(inline-|hidden-|eval-)?(nosources-)?(cheap-(module-)?)?source-map$".
   BREAKING CHANGE since webpack 5: The devtool option is more strict.
   Please strictly follow the order of the keywords in the pattern.

我的 webpack.config.js


var path = require('path');

module.exports = {
    entry: './src/main/js/App.js',
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/bundle.js'
    },
    module: {
        rules: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|svg|jpg|gif|eot|otf|ttf|woff|woff2)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {}
                    }
                ]
            }
        ]
    }
};

知道为什么吗?

使用

“webpack”:“^5.25.0”,“webpack-cli”:“^4.5.0”

谢谢,

标签: reactjswebpackdevtools

解决方案


它写在你的错误中。您必须为“devtool”指定值。如果你想使用 flag -d(缩写--devtool,你必须指出它的值:https ://webpack.js.org/configuration/devtool/#devtool


推荐阅读