JSX没有解析React.js

时间:2016-04-27 07:34:38

标签: reactjs webpack

因此,我的反应应用程序中会弹出这个常见错误,JSX无法通过webpack进行解析。我已经研究了十几个可能的答案,但无济于事,我试图避免使用.babelrc文件,因为我的mac一直隐藏它,我宁愿使用任何查询键。

render: function() {
        return {    
                <div>Hello World</div>
                ^
        }
    } 
Module build failed: SyntaxError:

webpack.config.js

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

var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './app/index.html',
    filename: 'index.html',
    inject: 'body'
});

module.exports = {

    entry: ['./app/index.js'],

    output: {
        path: __dirname + '/dist',
        filename: 'index_bundle.js'
    },

    devServer: {
        inline: true,   
        port: 3333
    },

    module: {
        loaders: [
            { 
                test: /\.js$/, 
                exclude: /node_modules/, 
                loader: 'babel', 
                query: {
                    presets: ['react']          
                }
            }
     ]
},

    plugins: [HtmlWebpackPluginConfig]

}

的package.json

{
  "name": "github_battle",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "production": "webpack -p",
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.0.1",
    "react-dom": "^15.0.1"
  },
  "devDependencies": {
    "babel-core": "^6.7.7",
    "babel-loader": "^6.2.4",
    "babel-preset-react": "^6.5.0",
    "html-webpack-plugin": "^2.16.0",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1"
  }
}

2 个答案:

答案 0 :(得分:1)

问题在于{应该是(

render: function() {
    return (
      <div>Hello World</div>
    );
  }

现在它不应该抛出任何错误。

答案 1 :(得分:0)

您将返回一个尝试将JSX注入的对象。那不行。 您总是可以将JSX包装在括号中,但这不是必需的。

var App = React.createClass({
    render: function () {
        return <div>Hello</div>;
    }
});