Reputation: 1
Node v0.10.20 provides many options pertaining to harmony,
--harmony_typeof (enable harmony semantics for typeof)
--harmony_scoping (enable harmony block scoping)
--harmony_modules (enable harmony modules (implies block scoping)
--harmony_proxies (enable harmony proxies)
--harmony_collections (enable harmony collections (sets, maps, and weak maps))
--harmony (enable all harmony features (except typeof))
I understand that these are not production-ready features and that they're under development, but many of them are good enough.
Is there a way to enable them at runtime?
"use strict";
"use harmony collections";
Something like the above. Even if it's not just module-level enabling of those features, it'd be nice to ensure they were enabled inside the module rather than assume they were enabled.
Upvotes: 7
Views: 3927
Reputation: 158
The previous answer isn't such a bad idea, for a module, (isolating the ES6 files in an exec/child process), if you can handle the idea of it being run in a child process.
The best seeming answer is to, if you're a module, document that you require these features and test for them at run time and bail with a helpful error. I haven't myself exactly figured out how to test for this nicely (give me a break, I've been using node for 3 days)
If you're writing an app, the answer is slightly different. In my case, the app I am writing will likely make use of these features - and due to the limitation of only being able to use a single parameter in the shebang line, not being able to change the JS version during runtime (which, of course, as explained above, makes complete sense), and not wanting to exec a child process (my server is already multithreaded) - I have been forced to write a script to run my node server so that my users don't have to figure out the proper node commmand line to run my app (ugly) and if I ever want to use more than --harmony
and "use strict";
I can do it with the script as it is just a shell script that calls node and my app..
It is recommended to use #!/usr/bin/env node
as the shebang (which will find node for you, wherever it is installed to) however, you can only use one parameter in the shebang so this won't work with --harmony
(or any other parameter)
Of course - you can always run node --harmony --use_strict --blah_blah yourScript.js
, but if you need certain options you'll have to type it every time, hence using a shell script is advised (by me!). I suppose you could include this (or such a) script with your module and recommend it's use in executing the app that uses your module.
This is a similar implementation to the shell script that I am using for my server, it will find node and run your script with whichever parameters you require:
#!/bin/bash
if [ "$myScript" == "" ]; then
myScript="./src/myNodeServer.js"
fi
if [ "$myNodeParameters" == "" ]; then
myNodeParameters="--harmony --use_strict"
fi
if [ "$myNode" = "" ]; then
myNode=`which node`
fi
if [ "$myNode" = "" ]; then
echo node was not found! this app requires nodeJS to be installed in order to run.
echo if you have nodeJS installed but is not found, please make sure the 'which'
echo command is available. alternatively, you can forcibly specify the location of
echo node with the $myNode environment variable, or editing this file.
else
echo Yay! node binary was found at $myNode
fi
if [ "$1" = "start" ]; then
echo you asked to start..
echo calling $myNode $myParameters $myScript $2
$myNode $myParameters $myScript $2
exit
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo you asked for help..
echo usage:
echo $0 start [script.js] [parameters for script]
echo parameters for node and node location can be
echo set with the \$myParameters and \$myNode env
echo variables (or edit the top of this file).
exit
else
echo no valid command specified - use $0 --help to see help.
fi
It's worth noting, if you ONLY want to use harmony and strict, while you can't specify both in a shebang, you can hardcode the location of node and use the "use strict"; alias:
#!/usr/bin/node --harmony
"use strict";
Upvotes: 1
Reputation: 36088
No, you can't. In fact, some things might potentially go horribly wrong within V8 internals if you tried to sneak in multiple different settings of these flags within the same V8 instance (disclosure: I implemented most of these flags).
Upvotes: 10
Reputation: 11245
There is no way to do this, the interpreter reads the content of the modules then validate them and then evaluate them. If you will use some ES6 specific syntax then the validation will fail and the code will not be evaluated.
You only could isolate the ES6 syntax files and run them as child processes (with the necessary options), but I guess this is not the way you want to do this.
Upvotes: 1