无法启动反应模块

时间:2018-04-15 10:51:37

标签: reactjs

启动npm时获得以下异常。

无效的配置对象。 Webpack已使用与API架构不匹配的配置对象进行初始化。   - configuration.module具有未知属性'加载器'。这些属性是有效的:    object {exprContextCritical?,exprContextRecursive?,exprContextRegExp?,exprContextRequest?,noParse ?, rules?,defaultRules?,unknownContextCritical?,unknownContextRecursive?,unknownContextRegExp?,unknownContextRequest?,unsafeCache?,wrappedContextCritical?,wrappedContextRecursive?,wrappedContextRegExp?,strictExportPresence?, strictThisContextOnImports? }     - >影响正常模块的选项(NormalModuleFactory)。

var path= require('path');

module.exports = {
  entry : './script.jsx',
  output : {
    path : path.resolve(__dirname,''),
    filename: 'transpiled.js'
  },
  module : {
    loaders: [
      {
      test:/\.jsx?$/,
      loaders:'babel-loader',
      exclude : /node_modules/,
      query : {
        presets : ['es2015','react']
      }
    }
  ]
}
}

更新后代码正在运行,但反应组件未在屏幕上呈现。

//Update
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Test title</title>
</head>
<body>
  <div id ="content">
      <h1>This is the demo of your web page</h1>
  </div>
  <script src ="transpiled.js"></script>
</body>
</html>

//webpack config
var path= require('path');

module.exports = {
  entry : './script.jsx',
  output : {
    path : path.resolve(__dirname,'react/index.html'),
    filename: 'transpiled.js'
  },
  module : {
    rules: [ // rules rules
        {
          test: /\.jsx?$/,
          loaders: 'babel-loader',
          //use:'babel-loader', // use here
          exclude : /node_modules/,
          query : {
            presets : ['es2015','react']
          }
        }
      ]
}
}

//script.jsx
import React from 'react';
import ReactDOM from 'react-dom';

class MyComponent extends React.Component {
  render() {
      return (
          <h2>Hello World !!!</h2>
      );
  }
}

ReactDom.render(
  <MyComponent/>,document.getElementById('content')
);

//package.json
{
  "name": "reactjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "it": "webpack-dev-server --hot"
  },
  "author": "chetan",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.5.0",
    "webpack-cli": "^2.0.14",
    "webpack-dev-middleware": "^3.1.2",
    "webpack-dev-server": "^3.1.3",
    "webpack-hot-middleware": "^2.21.2"
  },
  "dependencies": {
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "webpack-sources": "^1.1.0"
  }
}

2 个答案:

答案 0 :(得分:1)

您需要将关键字loaders替换为关键字rules。在每个规则对象中,将loaders替换为关键字use

module.exports = {
  entry : './script.jsx',
  output : {
    path : path.resolve(__dirname,''),
    filename: 'transpiled.js'
  },
  module : {
    rules: [ // rules rules
      {
        test:/\.jsx?$/,
        use:'babel-loader', // use here
        exclude : /node_modules/,
        query : {
          presets : ['es2015','react']
        }
      }
    ]
  }
}

修改

现在您的组件未在屏幕上呈现,因为webpack配置中的output.path不正确。它应该是这样的:

output : {
   path : path.resolve(__dirname),
   filename: 'transpiled.js'
}

因为项目根目录中html引用中的脚本标记:

<script src="transpiled.js"></script>

答案 1 :(得分:1)

您的webpack.config文件格式错误

var path = require('path');

module.exports = {
    entry: './script.jsx',
    output: {
        path: path.resolve(__dirname, ''),
        filename: 'transpiled.js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loaders: 'babel-loader',
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                '@babel/preset-env',
                                // your preset
                            ]
                        }
                    }
                ]
            }
        ]
    }
}

查看更多here