2
votes

Wanted to use ES8 async/await for my project. Have been using it recently on ReactNative with Expo so didn't expect any problems on ReactJS. Although, the app can't build now... This is the error I get:

    Syntax error: C:/projects/project1/src/containers/OfferOverview.js: Unexpected token (84:40)
      83 |
    > 84 |   initialGetProduct = async (productId) => {
         |                                         ^
      85 |     const { dispatch } = this.props;
      86 |     await dispatch(resetOfferStateAction());
      87 |     dispatch(getProductAction(productId));

This is the Component, I have cut most of it's contents as I doubt they could be related to the issue:

export class OfferOverview extends Component {
  componentWillMount() {
    this.initialGetProduct(this.props.location.query.product_id);
  }

  // same error will be using async initialGetProduct() {
  initialGetProduct = async (productId) => {
    const { dispatch } = this.props;
    await dispatch(resetOfferStateAction());
    dispatch(getProductAction(productId));
    this.selectProduct(productId);
  }

  render() { ... }
}

I have tried setting es2017 preset in babel configuration, same as using "transform-async-to-generator" plugin. This is what I have in .babelrc file:

{
  "presets": [
    "es2017",
    "react",
    "stage-0"
  ],
  "plugins": [
    "react-hot-loader/babel",
    "transform-async-to-generator",
    "transform-class-properties",
    ["import", { "libraryName": "antd", "style": "css" }]
  ]
}

This is my eslint configuration. On eslint discussion they said this is more babel-eslint issue although I've added parserOptions ecmaVersion = 2017:

module.exports = {
  root: true,

  parser: 'babel-eslint',

  // import plugin is termporarily disabled, scroll below to see why
  plugins: [/*'import', */'flowtype', 'jsx-a11y', 'react'],

  env: {
    browser: true,
    commonjs: true,
    node: true
  },

  parserOptions: {
    ecmaVersion: 2017,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
      generators: true,
      experimentalObjectRestSpread: true
    }
  },

  settings: {
    'import/ignore': [
      'node_modules',
      '\\.(json|css|jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$',
    ],
    'import/extensions': ['.js'],
    'import/resolver': {
      node: {
        extensions: ['.js', '.json']
      }
    }
  },

  rules: { ... }
};

and package json dependencies:

"devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.13.2",
    "babel-eslint": "^6.1.2",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.12.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-es2016": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.24.1",
    "coveralls": "^2.11.12",
    "eslint": "^3.2.2",
    "eslint-config-airbnb": "^10.0.0",
    "eslint-plugin-react": "^6.0.0",
    "ignore-styles": "^4.0.0",
    "istanbul": "^1.0.0-alpha.2",
    "mocha": "^3.0.2",
    "nock": "^8.0.0",
    "react-addons-test-utils": "^15.3.0",
    "react-scripts": "0.2.1",
    "redux-mock-store": "^1.1.2",
    "redux-saga-devtools": "^0.1.2"
},
"dependencies": {
    "babel-polyfill": "^6.26.0",
    "enzyme": "^2.4.1",
    "eslint-plugin-flowtype": "^2.39.1",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "expect": "latest",
    "isomorphic-fetch": "^2.2.1",
    "jsdom": "^9.9.1",
    "lodash": "^4.17.4",
    "nuka-carousel": "^2.3.0",
    "pushstate-server": "latest",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "react-redux": "^4.4.5",
    "react-router": "^2.6.0",
    "react-router-redux": "^4.0.8",
    "react-sidebar": "^2.3.2",
    "redux": "^3.6.0",
    "redux-logger": "3.0.1",
    "redux-saga": "^0.14.3"
},

Would appreciate any hint what can be wrong?

2
I believe you need the transform-class-properties Babel pluginWing
have added it to dev dependencies and to babelrc plugins but unfortunately it didn't helpKirill Stiopin
What is the exact error with async initialGetProduct()? It can't be the same exact error since there is no arrow.ReyHaynes
error is the same although it points to another place of the row (because no arrow anymore) prntscr.com/hgge4iKirill Stiopin
@VMAtm: using stage-2 will include presets for stage-3 and stage-4 (reference) – a stage preset will include all stages greater than itself. Also OP has the ES2017 preset which includes covers the functionality they need since it includes the transform-async-to-generator Babel plugin (reference). However your comment has led to this important note for OP: stage-0 is configured as a preset however dev dependencies has stage-2 – this dependency mismatch needs to be resolved.Wing

2 Answers

1
votes

I didn't run the code, but you're declaring the initialGetProduct function incorrectly. Should be

async initialGetProduct() {...
1
votes

So the problem was in little bit different area than I thought. I'm using react-scripts to start my app and my mistake was that I thought that configuration that I have in my project (I'm not the one who started it initially) has been used somehow. But it wasn't. So all I had to do to make this work is to update react-scripts version. It was 0.2.1 and now it's ^1.0.17, no wonder it couldn't work with ES8... Sorry for any inconvenience to everyone who tried to help, thank you all, your advices taught me much. In react-scripts config they have only this for babel:

          // Process JS with Babel.
          {
            test: /\.(js|jsx|mjs)$/,
            include: paths.appSrc,
            loader: require.resolve('babel-loader'),
            options: {
              // @remove-on-eject-begin
              babelrc: false,
              presets: [require.resolve('babel-preset-react-app')],
              // @remove-on-eject-end
              // This is a feature of `babel-loader` for webpack (not Babel itself).
              // It enables caching results in ./node_modules/.cache/babel-loader/
              // directory for faster rebuilds.
              cacheDirectory: true,
            },
          },