9

How can I run a globally-installed node module that exposes a shell script in --harmony mode?

bitpshr
  • 1,033
  • 2
  • 9
  • 21
  • Do you control the shell script? – bevacqua Jan 07 '14 at 19:39
  • Yes, I am writing a module that others will consume, and am exposing a shell script and want to use generators. – bitpshr Jan 07 '14 at 19:41
  • If you are writing a library to be included by node users then first you should check if harmony flag/environment is set in your library. If not ask user to run with flag. It is easier this way. Try not to force the options which are given by user. – user568109 Jan 08 '14 at 06:05

6 Answers6

8

You can create a "node-harnomy" executable file:

/usr/local/bin/node-harmony

#!/bin/bash
node --harmony "$@"

harmony-cmd.js

#!/usr/bin/env node-harmony

function* foo() {
}
Polor Beer
  • 1,814
  • 1
  • 19
  • 18
4

TL;DR: Just use Node 5+, most ES6 features will be availble right away.


2016 answer

This is more like an amendment to the 2015 answer.
The reason is because Node.js and io.js have converged, and the project is now a lot stronger, having lots of updates while keeping Long-Term Support (LTS) and supporting lots of ES6 features, in addition to those that io.js did support as well.

Notable features available in Node.js 5.0.0+:


2015 answer

We now have io.js available. It's reliable, fast, and up-to-date with the stable ES6 specs.

Depending on what ES6 features you want, you can use it with no flag at all. From their website:

  • Block scoping
    • let
    • const
    • function-in-blocks

      As of v8 3.31.74.1, block-scoped declarations are intentionally implemented with a non-compliant limitation to strict mode code. Developers should be aware that this will change as v8 continues towards ES6 specification compliance.

  • Collections
    • Map
    • WeakMap
    • Set
    • WeakSet
    • Generators
  • Binary and Octal literals
  • Promises
  • New String methods
  • Symbols
  • Template strings

2014 answer

What about spawning a second Node process with your stuff?

#!/usr/bin/env node

var spawn = require("child_process").spawn;
var child = spawn(process.execPath, [ "--harmony", "yourscript.js" ], {
  cwd: __dirname
});

child.stdout.on("data", function( data ) {
  console.log(data);
});

child.stderr.on("data", function( data ) {
  console.error(data);
});

EDIT: I believe process.execPath returns the node path, not the global script path in this case.
However, you can always change it to node directly, but that could break installations without node in the PATH.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
  • this pointed me in the right direction, but then your child does not show output. You need to add some extra lines. see ls example http://nodejs.org/api/child_process.html#child_process_class_childprocess => http://pages.citebite.com/p2g7e0m9u5vre – Alfred Feb 04 '14 at 15:34
  • 1
    @Alfred I've added it to the answer. Thanks for remembering – gustavohenke Feb 04 '14 at 17:24
  • 2
    No problem. It is still not exactly what we need. But I found it by googling => https://github.com/Swatinem/perf-cpuprofile/blob/master/bin/perf-cpuprofile.js – Alfred Feb 04 '14 at 17:30
  • So, why are people downvoting me? Any proper reason for this? – gustavohenke Aug 07 '15 at 12:06
  • @gustavohenke great answer. Accepting, sorry for the delay. – bitpshr Oct 07 '15 at 14:44
  • I wonder why I'm getting downvotes even though I answered this question. – gustavohenke Apr 13 '16 at 12:02
3

After discovering a hack for starting node with arguments, I wrote this script to start my app with generator support, and fail with a clear error if unavailable. --harmony does nothing if unsupported. You could also fall back to using gnode if you wanted to support earlier node versions.

#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"

var generators = require('generator-supported');
if (generators) {
  require('../lib');
} else {
  console.log('ERROR: node >= v0.11.3 is required for generators');
  process.exit(1);
}

https://gist.github.com/raine/ab56a90442ea1f61a97d

raine
  • 1,694
  • 17
  • 14
-1

With Node.js v5.0 you can use latest ES2015 using strict mode.

/bin/server

#!/usr/bin/env node --use_strict

//node.js code.

Then put below lines package.json scripts

scripts: {
   "start": "./bin/server"
}

Note: It works in Mac OS but not sure about Ubuntu etc.

kushdilip
  • 7,606
  • 3
  • 24
  • 30
-4

Just put #!/usr/bin/env node --harmony in the top of your script. But you should remember that harmony flag exposes different sets of features in different v8 versions, so you may not have, for example, generators available for node 0.10 and so on.

UPDATE this will probably not going to work on some systems. tested on OS X

vkurchatkin
  • 13,364
  • 2
  • 47
  • 55
  • 2
    You cannot add flags to the node shebang. – bitpshr Jan 07 '14 at 21:11
  • generators are available as of 0.10.22, I believe – gustavohenke Jan 07 '14 at 22:49
  • Again, you cannot add flags to the node shebang. This answer is incorrect. – bitpshr Feb 17 '14 at 17:32
  • Yes you can, it works at least on OS X. Is there a downside in using it and falling back to other methods if it silently fails? – raine Feb 26 '15 at 09:42
  • @rane There are two downsides that I can think of. If you have fallback methods, you lose the convenience of the shebang, and you suddenly have two places that specify the interpreter rather than one (which could cause maintenance problems). – Ian Hunter Jun 23 '15 at 19:11
-4

As of the time of this answer, the only viable solution is to use gnode's programmatic API and to separate the bash script into two separate files.

shellScript.js

#!/usr/bin/env node

require('gnode');
require('./main');

main.js

// main shell script logic using generators
bitpshr
  • 1,033
  • 2
  • 9
  • 21
  • This works but in my experience with the programmatic API `gnode` will not detect harmony generators in your node and will always compile your code making startup a bit slow. – raine Feb 26 '15 at 09:29
  • For the record, this is what I ended up doing: https://gist.github.com/raine/ab56a90442ea1f61a97d – raine Feb 26 '15 at 10:48
  • or you can use `io.js` :) – vkurchatkin Feb 26 '15 at 11:05