Arrow function "expression expected" syntax error

Javascriptnode.jsEcmascript 6Arrow Functions

Javascript Problem Overview


I want to transform this code:

var formatQuoteAmount = function (tx) {
    return Currency.toSmallestSubunit(tx.usd, 'USD');
};
var quoteAmounts = res.transactions.map(formatQuoteAmount);

into an anonymous arrow function. I've written this:

var quoteAmounts = res.transactions.map(tx => Currency.toSmallestSubunit(tx.usd, 'USD'));

I get expression expected syntax error at the arrow. I looked up the default syntax here and seems like the syntax of my code is correct. Any ideas what the problem might be?

I have it working with this syntax:

    var quoteAmounts = res.transactions.map(function (tx) {
        return Currency.toSmallestSubunit(tx.usd, 'USD')
    });

but I want to make it a one-liner, with an arrow-function.

Running on node v5.3.0

Javascript Solutions


Solution 1 - Javascript

I had the error expression expected reported by Webstorm when editing a Node.js program. In this case the solution is to set the language version to a version that supports this feature.

enter image description here

Solution 2 - Javascript

The following is what i did that work for me. (1) I change the JavaScript language option to ECMAScript 6 as show in the selected answer by @Joe23

(2) I close the Webstorm project/application.

(3) Navigate to the project folder and delete the .idea folder in it. I believe this is the folder webstorm generated to keep information about the project/application.

(4) I reopen my project in webstorm and the errors are gone.

Solution 3 - Javascript

Arrow functions are available by default on the latest versions of node and other javascript runtimes. You need to enable support for them only if you're dealing with a really old runtime (0.12 and earlier if I recall correctly) in which case you need to add the "--harmony" flag when you start the node process.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilkncookiezView Question on Stackoverflow
Solution 1 - JavascriptJoe23View Answer on Stackoverflow
Solution 2 - JavascriptOdeyinka OlubunmiView Answer on Stackoverflow
Solution 3 - JavascriptsitifensysView Answer on Stackoverflow