【问题标题】:Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema配置对象无效。 Webpack 已使用与 API 架构不匹配的配置对象进行初始化
【发布时间】:2017-06-22 22:47:27
【问题描述】:

我有一个从在线课程创建的简单 helloworld react 应用程序,但是我收到此错误:

配置对象无效。 Webpack 已使用 与 API 架构不匹配的配置对象。 - 配置具有未知属性“postcss”。这些属性是有效的:object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry, externals?, loader?, module?, name?, 节点?,输出?,性能?,插件?,配置文件?,记录输入路径?, recordsO utputPath?, recordsPath?, resolve?, resolveLoader?, stats?, 目标?,看?,看选项? } 错别字:请更正。
对于加载器选项:webpack 2 不再允许在 配置。 应该更新加载器以允许通过 module.rules 中的加载器选项传递选项。 在更新加载器之前,可以使用 LoaderOptionsPlugin 将这些选项传递给加载器: 插件:[ 新的 webpack.LoaderOptionsPlugin({ // test: /.xxx$/, // 可能仅适用于某些模块 选项: { 邮编:... } }) ] - configuration.resolve 具有未知属性“root”。这些属性是有效的:object { alias?, aliasFields?, cachePredicate?、descriptionFiles?、enforceExtension?、 enforceModuleExtension?, extensions?, fileSystem?, mainFields?, mainFiles?、moduleExtensions?、modules?、plugins?、resolver?、 符号链接?、unsafeCache?、useSyncFileSystemCalls? } - configuration.resolve.extensions[0] 不能为空。

我的 webpack 文件是:

// work with all paths in a cross-platform manner
const path = require('path');

// plugins covered below
const { ProvidePlugin } = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// configure source and distribution folder paths
const srcFolder = 'src';
const distFolder = 'dist';

// merge the common configuration with the environment specific configuration
module.exports = {

    // entry point for application
    entry: {
        'app': path.join(__dirname, srcFolder, 'ts', 'app.tsx')
    },

    // allows us to require modules using
    // import { someExport } from './my-module';
    // instead of
    // import { someExport } from './my-module.ts';
    // with the extensions in the list, the extension can be omitted from the 
    // import from path
    resolve: {
        // order matters, resolves left to right
        extensions: ['', '.js', '.ts', '.tsx', '.json'],
        // root is an absolute path to the folder containing our application 
        // modules
        root: path.join(__dirname, srcFolder, 'ts')
    },

    module: {
        loaders: [
            // process all TypeScript files (ts and tsx) through the TypeScript 
            // preprocessor
            { test: /\.tsx?$/,loader: 'ts-loader' },
            // processes JSON files, useful for config files and mock data
            { test: /\.json$/, loader: 'json' },
            // transpiles global SCSS stylesheets
            // loader order is executed right to left
            {
                test: /\.scss$/,
                exclude: [path.join(__dirname, srcFolder, 'ts')],
                loaders: ['style', 'css', 'postcss', 'sass']
            },
            // process Bootstrap SCSS files
            {
                test: /\.scss$/,
                exclude: [path.join(__dirname, srcFolder, 'scss')],
                loaders: ['raw', 'sass']
            }
        ]
    },

    // configuration for the postcss loader which modifies CSS after
    // processing
    // autoprefixer plugin for postcss adds vendor specific prefixing for
    // non-standard or experimental css properties
    postcss: [ require('autoprefixer') ],

    plugins: [
        // provides Promise and fetch API for browsers which do not support
        // them
        new ProvidePlugin({
            'Promise': 'es6-promise',
            'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
        }),
        // copies image files directly when they are changed
        new CopyWebpackPlugin([{
            from: path.join(srcFolder, 'images'),
            to: path.join('..', 'images')
        }]),
        // copies the index.html file, and injects a reference to the output JS 
        // file, app.js
        new HtmlWebpackPlugin({
            template: path.join(__dirname, srcFolder, 'index.html'),
            filename:  path.join('..', 'index.html'),
            inject: 'body',
        })
    ],

    // output file settings
    // path points to web server content folder where the web server will serve 
    // the files from file name is the name of the files, where [name] is the 
    // name of each entry point 
    output: {
        path: path.join(__dirname, distFolder, 'js'),
        filename: '[name].js',
        publicPath: '/js'
    },

    // use full source maps
    // this specific setting value is required to set breakpoints in they
    // TypeScript source in the web browser for development other source map
    devtool: 'source-map',

    // use the webpack dev server to serve up the web application
    devServer: {
        // files are served from this folder
        contentBase: 'dist',
        // support HTML5 History API for react router
        historyApiFallback: true,
        // listen to port 5000, change this to another port if another server 
        // is already listening on this port
        port: 5000,
        // proxy requests to the JSON server REST service
        proxy: {
            '/widgets': {
                // server to proxy
                target: 'http://0.0.0.0:3010'
            }
        }
    }

};

【问题讨论】:

标签: reactjs typescript webpack


【解决方案1】:

只需将“webpack.config.js”中的“loaders”改成“rules”

因为 Webpack 1 中使用了加载器,而 Webpack2 中使用了规则。 你可以看到有differences

【讨论】:

    【解决方案2】:

    我通过从解析数组中删除空字符串解决了这个问题。查看webpack's site 上的解决文档。

    //Doesn't work
    module.exports = {
      resolve: {
        extensions: ['', '.js', '.jsx']
      }
      ...
    }; 
    
    //Works!
    module.exports = {
      resolve: {
        extensions: ['.js', '.jsx']
      }
      ...
    };
    

    【讨论】:

    • 为什么不再工作了?在旧版本的 webpack 中,我总是在 extensions 数组值的第一个索引处看到空字符串
    【解决方案3】:

    我不完全知道是什么原因造成的,但我是这样解决的。
    重新安装整个项目,但请记住 webpack-dev-server 必须全局安装。
    我浏览了一些服务器错误,例如找不到 webpack,所以我使用链接命令链接了 Webpack。
    在输出中解决一些绝对路径问题。

    在开发服务器object: inline: false

    webpack.config.js

    module.exports = {
        entry: "./src/js/main.js",
        output: {
            path:__dirname+ '/dist/',
            filename: "bundle.js",
            publicPath: '/'
        },
        devServer: {
            inline: false,
            contentBase: "./dist",
        },
        module: {
            loaders: [
                {
                    test: /\.jsx?$/,
                    exclude:/(node_modules|bower_components)/,
                    loader: 'babel-loader',
                    query: {
                        presets: ['es2015', 'react']
                    }
                }
            ]
        }
    
    };
    

    package.json

    {
      "name": "react-flux-architecture-es6",
      "version": "1.0.0",
      "description": "egghead",
      "main": "index.js",
      "scripts": {
        "start": "webpack-dev-server"
      },
      "repository": {
        "type": "git",
        "url": "git+https://github.com/cichy/react-flux-architecture-es6.git"
      },
      "keywords": [
        "React",
        "flux"
      ],
      "author": "Jarosław Cichoń",
      "license": "ISC",
      "bugs": {
        "url": "https://github.com/cichy/react-flux-architecture-es6/issues"
      },
      "homepage": "https://github.com/cichy/react-flux-architecture-es6#readme",
      "dependencies": {
        "flux": "^3.1.2",
        "react": "^15.4.2",
        "react-dom": "^15.4.2",
        "react-router": "^3.0.2"
      },
      "devDependencies": {
        "babel-core": "^6.22.1",
        "babel-loader": "^6.2.10",
        "babel-preset-es2015": "^6.22.0",
        "babel-preset-react": "^6.22.0"
      }
    }
    

    【讨论】:

    • 只需删除 webpack-dev-server 的本地安装并全局安装它即可为我解决此问题。
    • 我认为 loaders 选项已替换为 rules 参见 webpack.js.org/concepts/loaders
    【解决方案4】:

    尝试以下步骤:

    npm uninstall webpack --save-dev
    

    紧随其后

    npm install webpack@2.1.0-beta.22 --save-dev
    

    然后你应该可以再次吞咽。为我解决了这个问题。

    【讨论】:

      【解决方案5】:

      多年来,Webpack 的配置文件发生了变化(可能随着每个主要版本)。问题的答案:

      为什么会出现这个错误

      Invalid configuration object. Webpack has been initialised using a 
      configuration object that does not match the API schema
      

      是因为配置文件与使用的webpack版本不匹配。

      接受的答案没有说明这一点,其他答案暗示了这一点,但没有明确说明npm install webpack@2.1.0-beta.22Just change from "loaders" to "rules" in "webpack.config.js"this。 所以我决定提供我对这个问题的答案。

      卸载并重新安装 webpack,或者使用 webpack 的全局版本都不会解决这个问题。 为正在使用的配置文件使用正确版本的 webpack 很重要。

      如果在使用全局版本时这个问题得到了解决,这可能意味着您的全局版本是“旧的”并且您使用的 webpack.config.js 文件格式是“旧的”,因此它们匹配并且viola 现在一切正常。我完全赞成工作,但希望读者知道他们为什么工作。

      每当你得到一个你希望能解决你的问题的 webpack 配置...问问你自己这个配置是用于什么版本的 webpack。

      有很多学习 webpack 的好资源。有些是:

      例子学习webpack4的好资源还有很多,有知道的请添加cmets。希望未来的 webpack 文章能够说明正在使用/解释的版本。

      【讨论】:

      • 我希望我能更多地支持这个答案。
      【解决方案6】:

      我猜你的 webpack 版本是 2.2.1。我认为您应该使用此迁移指南 --> https://webpack.js.org/guides/migrating/

      另外,你可以使用TypeSCript + Webpack 2这个例子。

      【讨论】:

      • 如果这仍然是 webpack 3 的问题怎么办?
      【解决方案7】:

      对于像我这样最近开始的人:loaders 关键字是 replacedrules;即使它仍然代表装载机的概念。所以我的webpack.config.js,对于一个 React 应用,如下:

      var webpack = require('webpack');
      var path = require('path');
      
      var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
      var APP_DIR = path.resolve(__dirname, 'src/client/app');
      
      var config = {
        entry: APP_DIR + '/index.jsx',
        output: {
          path: BUILD_DIR,
          filename: 'bundle.js'
        },
        module : {
          rules : [
            {
              test : /\.jsx?/,
              include : APP_DIR,
              loader : 'babel-loader'
            }
          ]
        }
      };
      
      module.exports = config;
      

      【讨论】:

        【解决方案8】:

        当您有冲突的版本(角度 js)时,通常会发生此错误。所以 webpack 无法启动应用程序。您可以通过删除 webpack 并重新安装来简单地修复它。

        npm uninstall webpack --save-dev
        npm install webpack --save-dev
        

        重新启动您的应用程序,一切都很好。

        我希望能够帮助别人。干杯

        【讨论】:

        • 我在将项目升级到较新版本的 Angular 时遇到了这个问题。只需重新安装 Webpack 即可!谢谢!
        【解决方案9】:

        它使用rules 而不是loaders 起作用

        module : {
          rules : [
            {
              test : /\.jsx?/,
              include : APP_DIR,
              loader : 'babel-loader'
            }
          ]
        }
        

        【讨论】:

          【解决方案10】:

          在 webpack.config.js 中,将加载器:[..] 替换为规则:[..] 它对我有用。

          【讨论】:

            【解决方案11】:

            我遇到了同样的问题,我通过安装最新的 npm 版本解决了它:

            npm install -g npm@latest

            然后改webpack.config.js文件解决

            - configuration.resolve.extensions[0] 不能为空。

            现在解析扩展应该如下所示:

            resolve: {
                extensions: [ '.js', '.jsx']
            },
            

            然后运行npm start

            【讨论】:

            • 这对我有用。在我的 webpack.config.js 文件中,我有一个条目,例如 extensions:[ '', '.js', '.jsx']。我删除了空项目''并且它起作用了。 configuration.resolve.extensions[0] 指的是 webpack.config.js 文件中 resolve: { extensions:['', '.js', '.jsx']} 下的第一项。
            【解决方案12】:

            使用npm i -S webpack@latest 安装"webpack": "^5.41.1" 将解决此问题。

            【讨论】:

              【解决方案13】:

              在将 webpack 引入我使用 npm init 创建的项目时,我收到了相同的错误消息。

              Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.output.path: The provided value "dist/assets" is not an absolute path!

              我重新开始使用 yarn 解决了我的问题:

              brew update
              brew install yarn
              brew upgrade yarn
              yarn init
              yarn add webpack webpack-dev-server path
              touch webpack.config.js
              yarn add babel-loader babel-core babel-preset-es2015 babel-preset-react --dev
              yarn add html-webpack-plugin
              yarn start
              

              我发现以下链接很有帮助: Setup a React Environment Using webpack and Babel

              【讨论】:

              • 我试过了,除了有一些警告外,一切正常,但是当我最后一次推荐“纱线启动”时,它给了我一个错误“找不到命令启动”,你知道如何解决吗这?谢谢!
              【解决方案14】:

              我将webpack.config.js文件中的loaders更改为rules,并将包html-webpack-pluginwebpackwebpack-cliwebpack-dev-server更新到最新版本然后它对我有用!

              【讨论】:

                【解决方案15】:

                我遇到了同样的问题,我通过在我的 web.config.js 文件中进行一些更改解决了这个问题。仅供参考,我使用的是最新版本的 webpack 和 webpack-cli。这个技巧拯救了我的一天。我附上了我的 web.config.js 文件前后版本的例子。

                之前:

                module.exports = {
                    resolve: {
                        extensions: ['.js', '.jsx']
                    },
                    entry: './index.js',
                    output: {
                         filename: 'bundle.js'
                    },
                    module: {
                        loaders : [
                           { test: /\.js?/, loader: 'bable-loader', exclude: /node_modules/ }
                        ]
                    }
                }
                

                之后:我刚刚将模块对象中的 loaders 替换为 rules,正如您在我的代码 sn-p 中看到的那样。

                module.exports = {
                    resolve: {
                        extensions: ['.js', '.jsx']
                    },
                    entry: './index.js',
                    output: {
                        filename: 'bundle.js'
                    },
                    module: {
                        rules : [
                            { test: /\.js?/, loader: 'bable-loader', exclude: /node_modules/ }
                        ]
                    }
                }
                

                希望这将帮助某人摆脱这个问题。

                【讨论】:

                • babel-loader,不是bable-loader
                【解决方案16】:

                当我使用 path.resolve() 设置“入口”和“输出”设置时,会出现此错误。 entry: path.resolve(__dirname + '/app.jsx')。试试entry: __dirname + '/app.jsx'

                【讨论】:

                  【解决方案17】:

                  在我的情况下,问题是包含项目的文件夹的名称,它带有符号“!”我所做的只是重命名文件夹,一切就绪。

                  【讨论】:

                    【解决方案18】:

                    我也遇到了同样的问题,就我而言,我所要做的就是做好人

                    阅读错误信息...

                    我的错误信息说:

                    配置对象无效。 Webpack 已使用与 API 模式不匹配的配置对象进行初始化。 - configuration.entry 应该是以下之一: 功能 |对象 { : 非空字符串 | [非空字符串] } |非空字符串 | [非空字符串] -> 编译的入口点。 细节: * configuration.entry 应该是函数的一个实例 -> 一个返回入口对象、入口字符串、入口数组或对这些东西的承诺的函数。 * configuration.entry['styles'] 应该是一个字符串。 -> 字符串被解析为启动时加载的模块。 * configuration.entry['styles'] 不应包含项目 'C:\MojiFajlovi\Faks\11Master\1Semestar\UDD-UpravljanjeDigitalnimDokumentima\Projekat\ nc-front\node_modules\bootstrap\dist\css\bootstrap.min.css' 两次。

                    正如粗体错误消息行所说,我刚刚打开angular.json 文件,发现styles 看起来像这样:

                    "styles": [
                          "./node_modules/bootstrap/dist/css/bootstrap.min.css",
                          "src/styles.css",
                          "node_modules/bootstrap/dist/css/bootstrap.min.css" <-- **marked line**
                        ],
                    

                    ...所以我只是删除了标记线...

                    一切顺利。 :)

                    【讨论】:

                      【解决方案19】:

                      使用不同的语法(flags ...),可能会在 webpack(3、4、5...)中从版本到另一个版本导致此问题,您必须阅读新的 webpack 配置更新和弃用的功能。

                      【讨论】:

                        【解决方案20】:

                        对我来说,我必须改变:

                        cheap-module-eval-source-map

                        到:

                        eval-cheap-module-source-map

                        v4v5 的细微差别。

                        【讨论】:

                          【解决方案21】:

                          在 webpacker.yml 中检查您的 source_path。我在从另一个项目复制 webpacker.yml 的项目中遇到了同样的错误。它应该指向包含你的包目录的目录。

                          【讨论】:

                            【解决方案22】:

                            在我的情况下,这是由于从另一个项目复制 webpack.js 而没有更改“入口”和“输出”路径以匹配新项目结构。一旦我修复了路径,一切都会再次运行。

                            【讨论】:

                              【解决方案23】:

                              我和你有同样的错误。

                              npm 卸载 webpack --save-dev

                              &

                              npm install webpack@2.1.0-beta.22 --save-dev

                              解决它!

                              【讨论】:

                                【解决方案24】:

                                我有 webpack 版本 3,所以我根据当前的https://www.npmjs.com/package/webpack-dev-server“版本”页面安装了 webpack-dev-server 版本 2.11.5。然后问题就消失了。

                                【讨论】:

                                  【解决方案25】:

                                  有点不太可能的情况。

                                  我删除了 yarn.lock 文件,它引用了旧版本的 webpack。

                                  因此,请检查您的yarn.lock 文件中的差异。

                                  【讨论】:

                                    【解决方案26】:

                                    如果您来自单一的 SPA 世界,并且遇到此错误。我意识到这个问题是由为应用程序提供服务的脚本引起的。

                                    一个例子是这样的:

                                    "scripts": {
                                        "start": "ng serve",
                                        "serve:single-spa:play": "ng s --project play --disable-host-check --port 4202 --deploy-url http://localhost:4202/ --live-reload false"
                                      },
                                    

                                    要完成这项工作,请将启动脚本更改为以下脚本:

                                    "scripts": {
                                        "start": "npm run serve:single-spa:play",
                                        "serve:single-spa:play": "ng s --project play --disable-host-check --port 4202 --deploy-url http://localhost:4202/ --live-reload false"
                                      },
                                    

                                    【讨论】:

                                      【解决方案27】:

                                      试试这个,问题是,规则必须是列表或数组……

                                      module:{
                                          rules:[
                                              {
                                                  test:/\.js$/,
                                                  exclude: /node_modules/,
                                                  use:{
                                                      loader:'babel-loader'
                                                  },
                                              }
                                          ]
                                      }
                                      

                                      【讨论】:

                                        猜你喜欢
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2017-10-21
                                        • 2019-10-12
                                        • 2021-09-03
                                        • 2022-07-30
                                        • 2018-09-03
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多