5

Given a jsdom based svgcreator.node.js script file :

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", "green");
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

Given I use NodeJS terminal command to run it and generate a output.svg file :

node svgcreator.node.js > output.svg  # nodeJS + script command

How to pass a parameter's value from the terminal to NodeJS ?


Dependencies for tests:


Solution used (@Matt_Harrison): we rely on process.env.myVar

svgcreator.node.js JS code :

var jsdom = require('jsdom');
jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ 'http://d3js.org/d3.v3.min.js',    // JS DEPENDENCIES online ...
  'js/d3.v3.min.js' ],                 // ... & offline
// D3JS CODE * * * * * * * * * * * * * * * * * * * * * * * *
  function (err, window) {

    var color = process.env.COLOR;     // <<################# IMPORTANT !!
    var svg = window.d3.select("body")
        .append("svg")
        .attr("width", 100)
        .attr("height", 100);
    svg.append("rect")
        .attr("id", "rect1")
        .attr("x", 10)
        .attr("y", 10)
        .attr("width", 80)
        .attr("height", 80)
        .style("fill", color);         // <<################# IMPORTANT !!
    // END svg design

  //PRINTING OUT SELECTION
    console.log(window.d3.select("body").html());
 }
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);

Terminal NodeJS command :

COLOR=#66AAFF node svgcreator.node.js > out.svg   # <<############# IMPORTANT !! setting the value.

+1 @Matt_Harrison answer and the question appreciated !

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • 2
    I am not pretty sure if you are looking for this only but ..you can use process.argv for passing parameter from terminal to browser – Sandeep Pal Aug 09 '14 at 15:47
  • I want to pass parameters from terminal to my script `jsdom.node.js`, which runs in **nodejs JS engine**. – Hugolpz Aug 09 '14 at 15:52
  • Sorry! i am also new to node.js but i am preety sure, you can pass parameter from terminal to js file using process.argv array. for ex. node test.js 2 on terminal and in js file you can get it like console.log(process.argv[2]). hope it helps – Sandeep Pal Aug 09 '14 at 16:14
  • Your good will is welcome, we learn by trying~ – Hugolpz Aug 09 '14 at 16:22
  • See also: http://stackoverflow.com/questions/4351521/how-to-pass-command-line-arguments-to-node-js – Hugolpz Sep 02 '14 at 20:16

1 Answers1

16

In your terminal, you can use environment variables:

$ COLOR=#FFFFFF node jsdom.node.js

In your JS, do:

var color = process.env.COLOR;

Or you could add extra arguments to the command:

$ node jsdom.node.js '#FFFFFF'

and in your JS :

var color = process.argv[2];

If you want to use a library; I would advise looking into the Minimist library, or Commander for a more fully-featured solution.

Matt Harrison
  • 13,381
  • 6
  • 48
  • 66
  • When you say `This will then be available` you means `within the js / d3js script` right? (just checking). – Hugolpz Aug 09 '14 at 17:00
  • Coming (dinner was here). I will add to my JS some `var color = process.env.COLOR;`, and run that. – Hugolpz Aug 09 '14 at 18:56
  • n⁰1 works ! I edited your answer to make it more clear. – Hugolpz Aug 09 '14 at 19:34
  • Ok, validated ! The answer is fully working, and have been improved to be plain clear. – Hugolpz Aug 09 '14 at 19:36
  • Is there something like `node svgcreator.node.js COLOR='#FFFFFF' > out.svg` together with `var color = process.argv[COLOR];` ? I just tried but doesn't work for me. – Hugolpz Aug 12 '14 at 17:44
  • Done. Minimist is nice indeed. The Nodejs file header get `var argv = require('minimist')(process.argv.slice(2));` you then define it as `var color = argv.COLOR;` and last, need to call the variable such `style("fill", color);`. So I can now use something node `svgcreator.node.js --COLOR=#66AAFF > out.svg` – Hugolpz Aug 14 '14 at 17:26