2

I am new to react/redux/webpack and am trying to setup a new project from scratch and I encountered Module build failed: SyntaxError: Unexpected token

I have tried answers to similar questions but havent been able to solve it.

Stacktrace

ERROR in ./Counter.js
Module build failed: SyntaxError: Unexpected token (16:6)

  14 |   render() {
  15 |     return (
> 16 |       <h1>Hello</h1>
     |       ^
  17 |     );
  18 |   }
  19 | }

 @ ./app.js 3:0-32
 @ multi ./app.js

.bablelrc

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

webpack.config.js

const path = require('path');

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: [
    './app.js',
  ],
  output: {
    path: path.join(__dirname, 'www'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          'babel-loader',
        ],
      },
    ],
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: [ 'react', 'es2015' ]
        }
      }
    ]
  },
  resolve: {
    modules: [
      path.join(__dirname, 'node_modules'),
    ],
  },
};

package.json

{
  "name": "starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "webpack",
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-latest": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "express": "^4.15.3",
    "immutable": "^3.8.1",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-redux": "^5.0.5",
    "redux": "^3.6.0",
    "webpack": "^2.6.0",
    "webpack-dev-middleware": "^1.10.2"
  },
  "devDependencies": {
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^2.6.0"
  }
}

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import Counter from './Counter';

document.addEventListener('DOMContentLoaded', function() {
  ReactDOM.render(
    React.createElement(Counter),
    document.getElementById('mount')
  );
});

counter.js

import React from 'react';
class Counter extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }

  render() {
    return (
      <h1>Hello</h1>
    );
  }
}
export default Counter;
4
  • 1) Show the full error and stacktrace 2) Show the relevant code Commented May 23, 2017 at 23:29
  • Added the stacktrace and the app.js file Commented May 23, 2017 at 23:31
  • Why are you using both rules and loaders? Use one or the other Commented May 23, 2017 at 23:32
  • Thanks that solved it Commented May 23, 2017 at 23:36

2 Answers 2

1

Modified my .babelrc code with the following and it solved it:

{
    "presets": [
      "env",
      "react",
      "stage-2"
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

Modified my webpack.config.js with the below code and solved it.

const path = require('path');

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: [
    './app.js',
  ],
  output: {
    path: path.join(__dirname, 'www'),
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: [ 'react', 'es2015' ]
        }
      }
    ]
  },
  resolve: {
    modules: [
      path.join(__dirname, 'node_modules'),
    ],
  },
};

Comments

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.