59

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

8
  • 1
    what is it holding/output of it console.log(res.transactions); Commented Feb 22, 2016 at 9:56
  • 1
    What environment are you running it in? Commented Feb 22, 2016 at 9:56
  • @nils, in node.js. I updated the OP tags. Commented Feb 22, 2016 at 9:57
  • @Milkncookiez Which version? Commented Feb 22, 2016 at 10:09
  • I cannot replicate this error in Node v5.3.0. Have you double-checked the version with node --version? Commented Feb 22, 2016 at 10:22

3 Answers 3

123

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

Sign up to request clarification or add additional context in comments.

4 Comments

Changed to ECMAScript 6, but I'm still getting this error on the line - var texts = entity.name.split(" ").filter(s => s); (meant to split string, avoiding empty results). Any idea how to solve it?
I get this issue and have to go and toggle that language version setting from ES6 to something else then back again. Really annoying.
This also worked in PHPStorm for my nodejs project. As you already pointed out, this setting is a JavaScript setting (found in Javascript, not nodejs).
had to restart IntelliJ after setting to ECMAScript6
15

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.

1 Comment

I've tried this, and found an additional aspect. Do NOT adjust this in File > Default Settings, as that only affects new projects. Instead, you MUST change it in Webstorm > Preferences > Languages and Frameworks -> Javascript. And don't forget to hit Apply! Annoying, pointless, purposeless errors like this (why does WebStorm use ECMAScript 5.1 as the default when 6.0 exists and its syntax is so commonplace??) are a reason that I so often go back to Visual Studio and C# - though I'm sure any developer will say that in favor of their best-known IDE and language.
-1

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.

3 Comments

Arrow functions are enabled by default
nodejs.org/en/docs/es6/… says that arrow functions do not require run-time flag. :)
Hmm yep, my bad. Node on my laptop is pretty outaded it seems (0.12). I don't see any problem with your code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.