当前位置: 首页 > 知识库问答 >
问题:

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

莘聪
2023-03-14

我从一个在线课程中创建了一个简单的helloworld react应用程序,但是我得到了这个错误:

配置对象无效。已使用与API模式不匹配的配置对象初始化Webpack。-配置具有未知属性“PostCSS”。这些属性是有效的:object{amd?,Bail?,Cache?,Context?,Dependencies?,DevServer?,DevTool?,entry,Externals?,loader?,Module?,Name?,Node?,Output?,Performance?,Plugins?,Profile?,RecordsInPutPath?,RecordsPath?,ResolveLoader?,Stats?,Target?,Watch?,WatchOptions?}对于错误:请更正。
对于loader选项:webpack 2不再允许在配置中使用自定义属性。应该更新加载器,以允许通过module.rules中的加载器选项传递选项。在加载器更新之前,可以使用LoaderOptionsPlugin将这些选项传递给加载器:plugins:[new webpack.LoaderOptionsPlugin({//test:/.xxx$/,//可能仅对某些模块应用此选项:{PostCSS:...}})]-Configuration.Resolve具有未知属性“root”。这些属性有效:object{Alias?,AliasFields?,CachePredicate?,DescriptionFiles?,EnforceExtension?,EnforceModuleExtension?,Extensions?,FileSystem?,MainFields?,MainFiles?,ModuleExtensions?,ModuleExtensions?,plugins?,Resolver?,SymLinks?,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'
            }
        }
    }

};

共有1个答案

郜昊苍
2023-03-14

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

在devServerobject: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"
  }
}
 类似资料:
  • 问题内容: 我有一个通过在线课程创建的简单的helloworld react应用,但是出现此错误: 无效的配置对象。已使用与API模式不匹配的配置对象初始化Webpack。-配置具有未知属性“ postcss”。这些属性是有效的:对象{amd,bail,cache,context,dependencies,devServer,devtool,入口,外部,加载程序,模块,名称,节点,输出,性能。 ,

  • 我在网上学习。我写了一个简单的项目。但运行“npm start”命令时出错。 我的网页。配置。js文件如下所示-- 包裹json文件-- 通过follwoing命令安装Webpack NPM安装webpack webpack dev-server Babel-loader Babel-preset-es2015 Babel-preset-report-Babel-preset-ending-2--

  • 我正在学习一门在线课程,当我试图运行我的服务器时,我遇到了这个错误,但我不知道为什么: /applications/xampp/xamppfiles/htdocs/projects/distdesign/node_modules/webpack/lib/webpack.js:19抛出新的WebpackOptionsValidationError(WebpackOptionsValidationEr

  • Spring Security的Java配置没有公开每个配置对象的每一个属性,这简化了广大用户的配置。毕竟如果要配置每一个属性,用户可以使用标准的Bean配置。 虽然有一些很好的理由不直接暴露所有属性,用户可能任然需要更多高级配置,为了解决这个Spring Security引入了 ObjectPostProcessor 概念,用来修改或替换Java配置的对象实例。例如:如果你想在FilterSec

  • Kubernetes 中的各个 Object 的配置指南。