【问题标题】:I have error '] Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema"我有错误'] 无效的配置对象。 Webpack 已使用与 API 架构不匹配的配置对象进行初始化”
【发布时间】:2019-05-12 09:12:50
【问题描述】:

当我尝试使用命令 npm start 时,出现错误

"] 配置对象无效。Webpack 已使用与 API 架构不匹配的配置对象进行初始化。"

代码:https://next.plnkr.co/edit/hEHTiPYQXQ7POWIH?open=lib%2Fscript.js&deferRun=1

这是我对 webpack 的完整配置代码

    const HtmlWebpackPlugin = require('html-webpack-plugin');

    module.exports = {
        entry: './src/app/app.js',
        output: {
            path: './dist',
            filename: 'app.bundle.js',
        },
        module: {
            loaders: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: 'babel-loader',
                },
                {
                    test: /\.html$/,
                    use: 'raw-loader'
                },
                {
                    test: /\.scss$/,
                    use: [
                        {
                            loader: 'style-loader'
                        },
                        {
                            loader: 'css-loader'
                        },
                        {
                            loader: 'sass-loader',
                        }
                    ]
                },
                {
                    test: /\.css$/,
                    use: [
                        {
                            loader: 'style-loader'
                        },
                        {
                            loader: 'css-loader'
                        }
                    ]
                },
            ]
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: 'src/index.html',
                inject: 'body',
                hash: true
            }),
        ],
        devtool: "#inline-source-map"
    }

错误:

Did not detect a `bs-config.json` or `bs-config.js` override file. Using lite-server defaults...
[0] ** browser-sync config **
[0] { injectChanges: false,
[0]   files: [ './**/*.{html,htm,css,js}' ],
[0]   watchOptions: { ignored: 'node_modules' },
[0]   server: { baseDir: './', middleware: [ [Function], [Function] ] } }

] Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
[1]  - configuration.module has an unknown property 'loaders'. These properties are valid:
[1]    object { defaultRules?, exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, strictExportPresence?, strictThisContextOnImports?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp? }
[1]    -> Options affecting the normal modules (`NormalModuleFactory`).
[1]  - configuration.output.path: The provided value "./dist" is not an absolute path!
[1]    -> The output directory as **absolute path** (required).

【问题讨论】:

  • 为了能够为您提供帮助,您最好在此处复制您的代码,并使用最少的可重现代码量并将其指向失败的地方。
  • 这是太嵌套的结构,无法粘贴到此处。我想我在 webpack 中设置了“条目”错误
  • 你能显示完整的错误吗?
  • @Tony Ngo 你能帮帮我吗?
  • 我去看看

标签: javascript angularjs webpack


【解决方案1】:

我发现你的问题是 webpack 使用规则配置而不是加载器配置,所以将你的代码改成这个

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require("path");

module.exports = {
    entry: 'src/app/app.js',
    output: {
        path:  path.resolve(__dirname, 'dist'),
        filename: 'index_bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
            },
            {
                test: /\.html$/,
                use: 'raw-loader'
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: 'sass-loader',
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    }
                ]
            },
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html',
            inject: 'body',
            hash: true
        }),
    ],
    devtool: "#inline-source-map"
}

更新:将您的 index.html 文件移动到同一级别的 webpack.config.js

我没有在文档中看到它们允许用户更改为不同的路径

如果还有问题请告诉我

【讨论】:

  • 我还有问题:[Browsersync] Serving files from: ./ [0] [Browsersync] Watching files... [1] Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. [1] - configuration.output.path: The provided value "./dist" is not an absolute path! [1] -> The output directory as **absolute path** (required). [1] webpack --watch exited with code 1 [0] 19.05.12 13:12:59 404 GET /index.html
  • 尝试使用path: path.resolve(__dirname, "dist"),
  • @Umbro 再次检查我的答案。也许你必须使用 path.resolve
  • [1] [0] ./src/app/app.js 2.63 KiB {0} [not cacheable] [built] [failed] [1 error] [1] [1] WARNING in configuration [1] The 'mode' option has not been set, webpack will fallback to 'production' for; ERROR in ./src/app/app.js [1] Module build failed (from ./node_modules/babel-loader/lib/index.js): [1] Error: Cannot find module '@babel/core'
  • @Umbro 再次更新了我的答案,抱歉忘了提及 :)
猜你喜欢
  • 2017-06-22
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2017-10-21
  • 2019-10-12
  • 2021-09-03
  • 2022-07-30
相关资源
最近更新 更多