Coder Perfect

SyntaxError: Unexpected token [duplicate] babel-loader jsx


I’m new to React and Webpack.

In my hello world web project, I discovered a strange problem.

I’m using webpack’s babel-loader to assist me convert jsx to js, however it appears that babel doesn’t recognize jsx syntax.

My dependencies are as follows:

"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"

My webpack.config.js file is attached.

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 the main.js file for my app.

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

Here’s 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)

Thank you for your help.

Asked by Keyu Lin

Solution #1

Add “babel-preset-react”

npm install babel-preset-react

and in your webpack.config.js file, add the “presets” option to babel-loader

(Alternatively, Add it to your.babelrc or package.js file.

An example of webpack.config.js is as follows:

    test: /\.jsx?$/,         // Match both .js and .jsx files
    exclude: /node_modules/, 
    loader: "babel", 

Babel 6 was recently launched, and there was a significant change:

ReactDOM.render() (from require(‘react-dom’)) should be used instead of React.render() if you’re running react 0.14:


Rule.query is no longer supported in favor of Rule.options. The following is how to use webpack 4:

npm install babel-loader babel-preset-react

Then in your webpack configuration (as an entry in the module.rules array in the module.exports object)

    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: [
        loader: 'babel-loader',
        options: {
          presets: ['react']

Answered by Yoon

Solution #2

When switching from Babel 5 to Babel 6, I ran into a similar problem.

babel src —out-dir lib babel src —out-dir lib babel src —out-dir lib babel src —out-dir lib babel src —out-dir lib babel sr

I’ll show you how I set up Babel 6:

Make sure you have the devDependencies for Babel 6 installed.

"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0"

Add your .babelrc file to the project:

  "presets": ["es2015", "stage-0", "react"]

Answered by svnm

Solution #3

Because the explanation above may still leave some folks in the dark, here’s an example of a complete webpack.config.js:

Answered by Everett

Solution #4

The file was created for me as a solution. This is the content of babelrc:

  "presets": ["react", "es2015", "stage-1"]

Answered by albanx

Solution #5

The following method (which involves react-hot, babel loaders, and es2015, react presets) has worked for me:

loaders: [
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loaders: ['react-hot', 'babel?presets[]=es2015&presets[]=react']

Answered by Vladyslav Babenko

Post is based on