React - ES6 - Unexpected token [duplicate]

百般思念 提交于 2019-12-12 05:26:22

问题


I'm getting this error

ERROR in ./src/components/Header/index.js
Module build failed: SyntaxError: C:/Users/Gil/Documents/Projects/ecommerce/src/components/Header/index.js: Unexpected token (16:7)

  14 |   }
  15 |
> 16 |   test = () => {
     |        ^
  17 |     console.log('pass!');
  18 |   };
  19 |

i think its something with ES6, but i dont know, anyway here's my config

webpack.config.js

loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader'
      },...

package.json

"devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react-scripts": "1.0.7"

so my problem is, when i try to use arrow function i get this error, do i need an extra config or i'm doing something wrong?


回答1:


You need https://babeljs.io/docs/plugins/transform-class-properties/

npm install --save-dev babel-plugin-transform-class-properties

and inside .babelrc:

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


来源:https://stackoverflow.com/questions/44360178/react-es6-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!