How to set nodejs interpreter arguments from javascript source

I have someScript.js which requires several node parameters to work properly. For example. (let us have node 0.12.7):

node --harmony someScript.js

I want to run it without the -harmony option and somehow install it inside the script.

I tried changing process.execArgv:

process.execArgv.push('--harmony');

function getGenerator() {
    return function *() {
        yield 1;
    };
}

var gen = getGenerator();

Since NodeJS is an interpreter, it can allow such changes (even on the fly). But, NodeJS seems to ignore the changes to process.execArgv.

Also I tried v8.setFlagsFromString:

Interestingly, all versions of NodeJs that needed settings to support generators do not contain the v8 module. So, I experimented 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, it v8.setFlagsFromString('--harmony')didn’t help, even for eval () and vm.runInThisContext ().

script.

, nodejs- javascript?

-1
1

someScript.js:

#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"

function test(a, ...args) {
    console.log(args);
}

test('a', 'b', 'c');

"sh someScript.js" , someScript.js.

someScript.js(chmod u + x someScript.js). ./someScript.js .

: http://sambal.org/2014/02/passing-options-node-shebang-line/

, Windows EOL, '\ r' script. , ('\ r').

UPDATE:

#! /usr/bin/env node --harmony

#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"
0

Source: https://habr.com/ru/post/1618538/


All Articles