Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a node shell script in --harmony mode

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

like image 923
bitpshr Avatar asked Jan 07 '14 19:01

bitpshr


3 Answers

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() {
}
like image 75
Polor Beer Avatar answered Sep 26 '22 22:09

Polor Beer


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+:

  • new.target
  • Spread operator
  • Arrow functions
  • Object.assign

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.

like image 45
gustavohenke Avatar answered Sep 22 '22 22:09

gustavohenke


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

like image 45
raine Avatar answered Sep 24 '22 22:09

raine