0

I have seen several issues posted that look a lot like this, but none of the solutions have helped so far. I am trying to come up to speed on React. I have tried following every tutorial I could find on the web including some pretty simple, straightforward ones. Not matter what I try, I keep getting an unexpected token error.

index.js:

import React from "react";
import ReactDOM from "react-dom";

const title = 'My React page';

ReactDOM.render(
  <div>{title}</div>,
  document.getElementById('app')
)

package.json:

{
  "name": "swvreact",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode development --display-error-details",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --mode development --open"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-prop-types": "^0.4.0",
    "webpack": "^4.3.0",
    "webpack-cli": "^2.0.13",
    "webpack-dev-server": "^3.1.1"
  }
}

webpack.config.js:

const webpack = require('webpack');

module.exports = {
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: ['babel-loader']
      },
      {
        test: /\.js?$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      }
    ]
  },
};

.babelrc:

{
    "presets": [
        "env", 
        "react", 
        "es2015", 
        "stage-2"
    ]
}

The error:

ERROR in ./src/index.js
Module build failed: SyntaxError: D:/work/swvreact/swvreact/src/index.js: 
Unexpected token (7:4)

   5 |
   6 | ReactDOM.render(
>  7 |     <div>{title}</div>,
     |     ^
   8 |     document.getElementById('app')
   9 | )
  10 |

BabelLoaderError: SyntaxError: D:/work/swvreact/swvreact/src/index.js: 
Unexpected token (7:4)

   5 |
   6 | ReactDOM.render(
>  7 |     <div>{title}</div>,
     |     ^
   8 |     document.getElementById('app')
   9 | )
  10 |

    at transpile (D:\work\swvreact\swvreact\node_modules\babel- 
   loader\lib\index.js:65:13)

I have tried moving files around, playing with the configurations, playing with .js and .jsx file types, everything I can think of. The posted code is from about the simplest tutorial I could find and seemed to be the newest.

My set up is a PC running Windows 10, Visual Studio Code, Chrome and Firefox, and the Windows command prompt. All applications are current. I'm sure I'm missing some little thing, but I sure can't find it.

1
  • By the way u don't have to use es2015 preset since you have env preset. Commented Mar 29, 2018 at 11:56

1 Answer 1

1

You get this error if webpack can't find your .babelrc. Make sure you don't have a typo in the filename.
You can also try to put the babel config into you package.json like this:

"babel": {
  "presets": [
    "env",
    "react",
    "stage-2"
  ]
},

Or add the options in your webpack.config.js like this:

test: /\.jsx?$/,
use: {
    loader: 'babel-loader',
    options: {
        presets: [
            'env',
            'react',
            'es2015',
            'stage-2',
        ]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That did it. Figured it was something easy. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.