简体   繁体   中英

Using JavaScript's yield in IntelliJ / WebStorm

I'm trying out some JavaScript demo code:

function* idMaker(){
    var index = 0;
    while(index < 3)
        yield index++;
}

The yield statement has a red underline with the error message:

Generators are not supported by current JavaScript Version

I was able to use alt - enter to access a context menu to change the JavaScript version in IDEA. That made the red underline error go way. However, when I run the script, I get the following:

/usr/bin/node temp.js
/home/sal/workspace/javascript_interviews/test/temp.js:3
function* idMaker(){
        ^
SyntaxError: Unexpected token *
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Process finished with exit code 1

This is my node process info:

{ http_parser: '2.3',
  node: '0.12.7',
  v8: '3.28.71.19',
  uv: '1.6.1',
  zlib: '1.2.8',
  modules: '14',
  openssl: '1.0.1p' }

So it looks like I'm still running the wrong version of JavaScript. However, according to Node's notes, any version > 0.11.x should be able to use the generator syntax.

I read elsewhere that I need to take additional steps to activate this language feature, but I am not yet sure what these additional steps are.

Update 0

This seems to work on the command line:

sal@blodgett:~/workspace/javascript_interviews$ node --harmony
> function* foo(i) {yield i+1}
undefined
> 

So how do I get that --harmony action to be the default so that my gulp tasks (and all processes spawned from IntelliJ IDEA) have this flag?

Just add the --harmony flag to Node.js run configuration you are using for running your application ( Run/Edit Configurations/Node.js , select your configuration, add --harmony to Node parameters: field).

To get this flag added to all Node.js Run configurations on creation, add this option to 'default' Node.js run configuration: expand Defaults node, find Node.js there, add a flag.

For Gulp tasks, Gulp.js run configuration, Node options: field, has to be changed accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM