I have someScript.js which requires some node options for correct running. E.g. (let's suppose we have node 0.12.7):
node --harmony someScript.js
I wish to run it without --harmony option and somehow set it inside the script.
I tried to change process.execArgv:
process.execArgv.push('--harmony');
function getGenerator() {
return function *() {
yield 1;
};
}
var gen = getGenerator();
Since NodeJS is interpreter it could allow such settings change (even on the fly). But, NodeJS seems to ignore process.execArgv changes.
Also I tried v8.setFlagsFromString:
Interesting, all NodeJs versions which needed --harmony to support generators do not contain v8 module.
So, I made experiments with NodeJS 4.1.1
var v8 = require('v8');
var vm = require('vm');
v8.setFlagsFromString('--harmony');
var str1 =
'function test1(a, ...args) {' +
' console.log(args);' +
'};';
var str2 =
'function test2(a, ...args) {' +
' console.log(args);' +
'};';
eval(str1);
test1('a', 'b', 'c');
vm.runInThisContext(str2);
test2('a', 'b', 'c');
Unfortunately, v8.setFlagsFromString('--harmony')
did not help, even for eval() and vm.runInThisContext().
I wish to avoid creating some wrapping script.
So, is there some other way to set nodejs arguments from javascript source?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…