문제

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

도움이 되었습니까?

해결책 2

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.

다른 팁

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() {
}

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

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.

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top