Ad

Babel : SyntaxError: Test.jsx: Unexpected Token (3:11)

- 1 answer

I need to transform jsx to js.So I used babel to do that,But it gives an Error as shown below.Someone please help me to resolve this.

test.jsx:

var HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

ReactDOM.render(<HelloMessage name="John" />, mountNode);

On command babel test.jsx :

SyntaxError: test.jsx: Unexpected token (3:11)

  1 | var HelloMessage = React.createClass({
  2 |   render: function() {
> 3 |     return <div>Hello {this.props.name}</div>;
    |            ^
  4 |   }
  5 | });
  6 | 
Ad

Answer

You need to have the React preset installed to convert JSX to createElement calls.

From the website:

Installation

npm install babel-preset-react

Usage

Add the following line to your .babelrc file:

{
  "presets": ["react"]
}
Ad
source: stackoverflow.com
Ad