babel-loader jsx SyntaxError: Unexpected token [duplicate]

I’m a beginner in React + Webpack.

I found a weird error in my hello world web app.

I’m using babel-loader in webpack to help me convert jsx into js, but it seems like babel can’t understand jsx syntax.

Here are my dependencies:

"devDependencies": {
  "babel-core": "^6.0.14",
  "babel-loader": "^6.0.0",
  "webpack": "^1.12.2",
  "webpack-dev-server": "^1.12.1"
},
"dependencies": {
  "react": "^0.14.1"
}

Here is my webpack.config.js

var path = require('path');
module.exports = {
  entry: ['webpack/hot/dev-server',path.resolve(__dirname, 'app/main.js')],
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js'
  },
  module: {
      loaders: [
          { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
      ]
  }
};

Here is my app/main.js

var React = require("react");
React.render(<h1>hello world</h1>,document.getElementById("app"));

And this is the error message

ERROR in ./app/main.js
Module build failed: SyntaxError: ~/**/app/main.js: Unexpected token (2:13)
  1 | var React = require("react");
> 2 | React.render(<h1>hello world</h1>,document.getElementById("app"));
    |              ^
at Parser.pp.raise (~/**/node_modules/babylon/lib/parser/location.js:24:13)

Thanks for you guys.

8 Answers
8

Leave a Comment