Reputation: 484
I've been searching a solution for a while, but nothing seems to work.
I've read in an old article (Node v6 failing on object spread) that this issue has been solve as of node v8.3, but it doesn't seems to work for me.
I have a problem with Nodejs (v10.15.3) and spread operators. In fact, whenever I try to use them, I get this error:
SyntaxError: /Users/gabriele/models/Project.js: Unexpected token (15:8)
13 | return {
14 | id: res.insertId,
> 15 | ...newProject
| ^
16 | };
17 | } catch ({ sqlMessage }) {
18 | console.log(sqlMessage);
at Parser.pp$5.raise (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp$3.parseIdentifier (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:4332:10)
at Parser.pp$3.parsePropertyName (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:4156:96)
at Parser.pp$3.parseObj (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:4045:12)
at Parser.pp$3.parseExprAtom (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:3719:19)
at Parser.pp$3.parseExprSubscripts (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:3494:19)
at Parser.pp$3.parseMaybeUnary (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:3474:19)
at Parser.pp$3.parseExprOps (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:3404:19)
at Parser.pp$3.parseMaybeConditional (/Users/gabriele/Websites/pages-api/node_modules/babylon/lib/index.js:3381:19)
This is my code:
...
return {
id: res.insertId,
...newProject
};
...
Here is the output if I console.log(newProject)
:
{ name: 'My test', slug: 'my-test', user_id: 1 }
Can anybody help me?
Upvotes: 6
Views: 8380
Reputation: 484
I fixed by installing transform-object-rest-spread (https://www.npmjs.com/package/babel-plugin-transform-object-rest-spread).
You can install with npm install --save-dev babel-plugin-transform-object-rest-spread
.
Then you need to add in the .babelrc:
"plugins": [["transform-object-rest-spread", { "useBuiltIns": true }]]
This will do the magic.
This happens because spread operator for objects is still in stage3 of approval on ecmascript.
Hope it can help someone!
Upvotes: 7