2

I'm using ES6's spread operator with Babel as a transpiler to ES5. When I use the spread operator like so:

const { height, weight, radius, ...otherValues } = sphere;

I get a SyntaxError:

ERROR in ./src/sphere.js
Module build failed: SyntaxError: Unexpected token (7:36)

   6 | 
>  7 |   const { height, weight, radius, ...otherValues } = sphere;
     |                                   ^
   8 | 

Why is this?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • 1
    I already know the answer to my question but had a hard time finding the answer on StackOverflow, so [I'm posting the question and answer I would have found helpful](https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/). – Kevin Jun 02 '17 at 17:46
  • 2
    FYI, [`...` is not an operator](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508). In your example it's a "rest property". – Felix Kling Jun 03 '17 at 03:44

1 Answers1

2

Babel doesn't transpile the spread operator out of the box. You also need to install the babel-plugin-transform-object-rest-spread plugin for Babel. Once you've installed that package, your code should compile with no further changes.

Kevin
  • 14,655
  • 24
  • 74
  • 124
  • You need to install stage preset as well. "npm install --save-dev babel-preset-stage-0", If there is babelrc file, add the stage-0 preset. – Pavan Sep 29 '20 at 03:54