TheGabornator
TheGabornator

Reputation: 551

SyntaxError in Webpack / React.js Unexpected token = { } in React

In one of my .js file, I'm getting the following error from Webpack:

  4 | export default function(ComposedComponent) {
  5 |   class Authentication extends Component {
> 6 |     static contextTypes = {
    |                         ^
  7 |       router: React.PropTypes.object
  8 |     }
  9 | 

These are my devdependencies:

  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-3": "^6.24.1",
    "css-loader": "^0.26.1",
    "file-loader": "^1.1.4",
    "html-webpack-plugin": "^2.30.1",
    "image-webpack-loader": "^3.4.2",
    "rimraf": "^2.6.2",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.9",
    "webpack": "2.2.0-rc.0",
    "webpack-dev-server": "^2.2.0-rc.0"
  }

And this is my Webpack configuration:

{
  "presets": ["env", "react", "stage-3"],
  "plugins": ["transform-object-rest-spread"]
}

Could anybody tell which plugin or package is missing? Thank you!

Upvotes: 1

Views: 574

Answers (2)

Aymeric Bouzy aybbyk
Aymeric Bouzy aybbyk

Reputation: 2261

Class properties is a stage 2 proposal at the moment, so it's not included in babel-preset-env. You can either:

  • include only this specific transform with .babelrc:

    { "plugins": ["transform-class-properties"] }

  • include all stage 2 proposals with :

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

Please consider the risks before adding all stage-2 proposals : https://babeljs.io/docs/plugins/preset-stage-2/

Upvotes: 1

Daniel
Daniel

Reputation: 6491

Nothing is missing, it's just incorrect syntax.

Static methods should be (as the name implies) functions.

Try:

class Authentication extends Component {
   ...
}

Authentication.contextTypes = {
    router: React.PropTypes.object
}

Upvotes: 1

Related Questions