22

I am trying to use ES6 String Templates in a Node.js (v 5.7.0) app, but they are not working. Webstorm is correctly warning me that

String templates are not supported by current Javascript version

I am sure I have used string templates in a node app in the past. How can I get string templates to work? I am running the server with this command

npm start

When that is fixed, how can I help Webstorm know it is fixed and stop showing the warning?

Edit: I changed the app source to use template strings in this manner:

`````

var app = express();
var server = app.listen(process.env.PORT || 8080, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log(`App listening at http://${host}:${port}`);

`````

the app prints:

App listening at http://:::8080

cyrf
  • 5,127
  • 6
  • 25
  • 42

2 Answers2

20

String templates are supported in JavaScript version ECMAScript 6 and above.

To fix this in Web Storm:

Webstorm IDE

-> File

-> Settings

-> Languages & Frameworks

-> JavaScript

-> Javascript Language Version:

Choose ECMAScript 6 instead of ECMAScript 5.1 or any older version.

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
17

Have you try in the REPL? It works for me.

enter image description here

If the code doesn't throw an error it means that the template strings works. But if the result is not what you expected, just console.log your variables to see what's inside.

Edit: Concerning the WebStorm warning, it seems pretty easy to fix.

Cohars
  • 3,822
  • 1
  • 29
  • 50
  • Yes. I get the same results as you. – cyrf Mar 13 '16 at 18:07
  • 2
    Does you code works or is ut just the WebStorm warning bothering you? If so, just tell WS you're using ES6 (see my edit). – Cohars Mar 13 '16 at 18:09
  • Ok, how does it "not work"? Does it throw an error? Can you add your code to the question? – Cohars Mar 13 '16 at 18:10
  • I updated my question to show the code and the result. – cyrf Mar 13 '16 at 18:18
  • So the `${port}` works (right?) and the `${host}` is replaced by `::`? Can you `console.log` it? – Cohars Mar 13 '16 at 18:22
  • I just realized only ${host} is not printing as expected. This is probably more a problem understanding Node than es6 template strings. – cyrf Mar 13 '16 at 18:22
  • console.log(host) results in :: – cyrf Mar 13 '16 at 18:23
  • I was fooled by the result + the warning from Webstorm. I was expecting "localhost" to be printed. – cyrf Mar 13 '16 at 18:27
  • I see. Did you fix the warning issue with my link? And try to log `server.address()`, to find out all the informations you can get. Finally, don't hesitate to validate the answer if you find it (or the comments) helpful! – Cohars Mar 13 '16 at 18:31
  • I'd like to accept, but I think the correct answer would explain that "::" is expected. – cyrf Mar 13 '16 at 18:32