ReactJS - Module build failed: SyntaxError: Unexpected token react components not rendering

kawnah

I return a react component like this:

return (
    <section style={styleObj} id="Profile" >
      <h2 className="colorHeader">{this.state.color}</h2>
      <input id="colorInput" placeholder="Enter Hex Code Here" onChange={this.changeColor.bind(this)}/>
      <div id="slider"></div>
    </section>
  );

This is resulting in:

ERROR in ./app/js/modules/colorpicker.js
Module build failed: SyntaxError: Unexpected token (31:4)

  29 |
  30 |   return (
> 31 |     <section style={styleObj} id="Profile" >
     |     ^
  32 |       <h2 className="colorHeader">{this.state.color}</h2>
  33 |       <input id="colorInput" placeholder="Enter Hex Code Here" onChange={this.changeColor.bind(this)}/>
  34 |       <div id="slider"></div>

To my understanding as of now, you need a babel transpiler to convert this correctly. The relevant part of my webpack config looks like this:

module: {
  rules: [{
      test: /\.js$/,
      use: 'babel-loader'
    }]
  },

I did some more research and tried this:

loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        query  :{
          presets:['react','es2015']
        }
        exclude: /node_modules/
      }
    ]

This returns:

Module build failed: Error: Couldn't find preset "react" relative to directory

So I referenced this question and tried this:

{
  "presets": ["es2015", "react"]
}

Still get:

Module build failed: Error: Couldn't find preset "react" relative to directory

How do I get react components to render correctly with a webpack config?

The linked answer suggests npm ls babel-preset-es2015 which gets:

[email protected] /projects/new-platform-prototype └── [email protected]

Bojan Ivanac
npm install -D babel-preset-react babel-preset-es2015

This should fix your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ReactJS Module build failed: SyntaxError: Unexpected token - ReactDOM.render

ReactJS: "Uncaught SyntaxError: Unexpected token <"

Create-React-App build - "Uncaught SyntaxError: Unexpected token <"

Uncaught SyntaxError: Unexpected token & while rendering a Django template

ReactJS: Uncaught SyntaxError: Unexpected token (

Webpack module build failed unexpected token (rails react build)

React JS Jest causing "SyntaxError: Unexpected token ."

React Native bundling: SyntaxError: Unexpected token: operator (*)

React, semantic-ui-react. Test suite failed to run: SyntaxError: Unexpected token import

Module parse failed: Unexpected token

Html-Webpack-Plugin Template: Module build failed: SyntaxError: Unexpected token

Module build failed: SyntaxError: Unexpected token

Uncaught SyntaxError: Unexpected token < with React

Module parse failed: Unexpected token on React Component Render

React/Node Uncaught SyntaxError: Unexpected token <

Reactjs - Uncaught SyntaxError: Unexpected token <

ReactJS: Importing symlinked components error: Module parse failed: Unexpected token: You may need an appropriate loader to handle this file type

"npm run build" fails with SyntaxError:Unexpected token

react-native init syntaxerror:unexpected token )

Module build failed: SyntaxError: Unexpected token (11:16)

Module build failed: SyntaxError: Unexpected character '@' when using react-dnd in laravel (gulp+webpack+babel)

react-router-redux: Module build failed: SyntaxError: Unexpected token ...reducers

Webpack, React JSX, Babel: Module build failed, "unexpected token" empty?

SyntaxError: Unexpected token react-native init

Module build failed: SyntaxError: Unexpected token, expected ,

Vue-Router error: "Module build failed: SyntaxError: Unexpected token"

SyntaxError: Unexpected token import - reactjs

Nuxt build on production failed: SyntaxError: Invalid or unexpected token

Unexpected token in conditional rendering in React