【问题标题】:I can't make work webpack correctly我无法正常工作 webpack
【发布时间】:2018-05-29 08:21:30
【问题描述】:

我收到了这条消息,请帮助...提前致谢。 ...

    Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module has an unknown property 'loaders'. These properties are valid:
   object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, defaultRules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence?, strictThisContextOnImports? }
   -> Options affecting the normal modules (`NormalModuleFactory`).
 - configuration.resolve.extensions[0] should not be empty.
   -> A non-empty string

我在 webpack 中的配置是这样的,我还在试图弄清楚为什么会这样……我的文件是这样的:https://github.com/NodeioLabs/Theatherflix/blob/master/webpack.config.js

【问题讨论】:

  • 请提供您的 webpack 配置。就错误读取而言,您的“模块”定义中有一个“加载器”键,但如果没有更多信息,很难进一步帮助您。
  • 尝试更新 webpack?
  • 我正在使用最新的....仍然坚持这个消息:/

标签: node.js webpack


【解决方案1】:

从第 7-14 和 37-46 行,您注意到:

  resolve: {
    root: __dirname,
    alias: {
      Main: 'app/components/Main.jsx',
      Nav: 'app/components/Nav.jsx'
    },
    extensions: ['', '.js', '.jsx']
  },

  resolve: {
    root: __dirname,
    alias: {
      Main: 'app/components/Main.jsx',
      Nav: 'app/components/Nav.jsx',
      ListM: 'app/components/ListM.jsx',
      About: 'app/components/About.jsx'
    },
    extensions: ['', '.js', '.jsx']
  },

webpack不接受的,建议你使用webpack resolve.extensions推荐的

resolve.extensions

数组

自动解析某些扩展。这默认为:

extensions: [".js", ".json"]

这里:

    root: __dirname,
    alias: {
      Main: 'app/components/Main.jsx',
      Nav: 'app/components/Nav.jsx',
      ListM: 'app/components/ListM.jsx',
      About: 'app/components/About.jsx'
    },

按照webpack #resolve-alias的建议改用这种格式

alias: {
  Main : path.resolve(__dirname, 'app/components/Main'),
  Nav: path.resolve(__dirname, 'app/components/Nav'),
  ListM: path.resolve(__dirname, 'app/components/ListM'),
  About: path.resolve(__dirname, 'app/components/About')
},

我希望这会有所帮助。

【讨论】:

  • 再次尝试运行npm install --save-dev webpack
  • 我尝试了很多东西......现在,我收到了这条消息...... ash:d67645a8642f21f1a34e 版本:webpack 4.10.2 时间:272ms 构建于:06/09/2018 3:53:02 AM 1 资产入口点应用程序 = bundle.js [0] ./app/app.jsx 278 字节 {0} [built] [failed] [1 error] ./app/app.jsx 模块解析失败:意外令牌( 10:2) 你可能需要一个合适的加载器来处理这个文件类型。 | | ReactDOM.render( | | |
【解决方案2】:

让我们单独看一下消息:

  1. configuration.module has an unknown property 'loaders'.

    表示您目前拥有:

    {
      module: {
        loaders: {}
      }
    }
    

    因此将loaders 更改为rules(根据webpack documentation):

    {
      module: {
        rules: {}
      }
    }
    
  2. configuration.resolve.extensions[0] should not be empty.

    表示你有:

    {
      resolve: {
        extensions: ['']
      }
    }
    

    所以从extensions中删除空字符串:

    {
      resolve: {
        extensions: []
      }
    }
    

【讨论】:

    【解决方案3】:

    我一直在努力寻找答案……我不得不进行大量研究。但是,我找到了答案,或者我找到了解决这种情况的方法:

    首先:创建一个文件.babel-rc

        /* 
        ./.babelrc
    */  
    {
        "presets":[
            "es2015", "react"
        ]
    }
    

    也可以考虑这个包 json 文件:

    {
      "name": "theatherflix",
      "description": "",
      "main": "server.js",
      "scripts": {
        "webpack": "webpack --progress",
        "test": "echo \"Error: no test specified\" && exit 1",
        "build": "webpack --config webpack.config.js"
      },
      "author": "Nodeio Labs",
      "license": "GPL-3",
      "dependencies": {
        "@babel/preset-react": "^7.0.0-beta.51",
        "axios": "^0.18.0",
        "babel": "^6.23.0",
        "docker": "^1.0.0",
        "express": "^4.13.4",
        "extract-text-webpack-plugin": "^3.0.2",
        "find-process": "^1.1.0",
        "loader": "^2.1.1",
        "material-ui": "^0.20.1",
        "modules": "^0.4.0",
        "path": "^0.12.7",
        "react": "^0.14.7",
        "react-dom": "^0.14.7",
        "react-router": "^2.0.0",
        "redux": "^4.0.0",
        "redux-thunk": "^2.2.0",
        "ts-loader": "^4.4.1",
        "uuid": "^3.2.1",
        "webpack-dev-server": "^3.1.4"
      },
      "devDependencies": {
        "@babel/core": "^7.0.0-beta.51",
        "@babel/preset-env": "^7.0.0-beta.51",
        "babel-cli": "^6.26.0",
        "babel-core": "^6.26.3",
        "babel-loader": "^7.1.4",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-react": "^6.24.1",
        "babel-preset-stage-0": "^6.5.0",
        "fuse-box": "^3.2.2",
        "gulp-babel": "^7.0.1",
        "jsx-loader": "^0.13.2",
        "node-sass": "^4.9.0",
        "typescript": "^2.9.2",
        "webpack": "^4.12.0",
        "webpack-cli": "^3.0.2"
      }
    }
    

    *在这个模块包中使用你需要的东西。

    然后我更正了我的 webpack 中的几行:

    /*
        ./webpack.config.js
    */
    const path = require('path');
    module.exports = {
      mode: "none", 
      entry: './app/app.jsx',
      output: {
        path: path.resolve('public'),
        filename: 'bundle.js'
      },
      module: {
        rules: [{
            test: /\.css$/,
            use: [{
                loader: "style-loader"
              },
              {
                loader: "css-loader"
              }
            ]
          },
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: "babel-loader"
          }, {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            use: "babel-loader"
          }
        ]
      }
    }
    

    这是我的 app.jsx :

    var React = require('react');
    var ReactDOM = require('react-dom');
    var {Route, Router, IndexRoute, Main, About, MovieList, hashHistory} = require('react-router');
    var Main = require('./components/Main.jsx');
    var MovieList = require('./components/MovieList/MovieList.jsx');
    var About = require('./components/About/About.jsx');
    //import './app.scss';
    
    ReactDOM.render(
      <Router history={hashHistory}>
        <Route path="/" component={Main}>
        <Route path="about" component={About}/>
              <IndexRoute component={MovieList}/>
        </Route>
      </Router>,
      document.getElementById('app')
    );
    

    我希望它可能对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2018-12-31
      • 2023-03-06
      • 2019-09-01
      • 2018-11-03
      • 2018-08-29
      • 2016-12-01
      • 2012-12-28
      • 1970-01-01
      相关资源
      最近更新 更多