How can I run a globally-installed node module that exposes a shell script in --harmony
mode?
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() {
}
TL;DR: Just use Node 5+, most ES6 features will be availble right away.
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
Object.assign
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With