Node.js: how to pass parameter value from terminal to JS script

For jsdom

jsdom

jsdom

:

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) * * * * * * * * * * * * * * * * * * * * * * * *
);

      

Considering I'm using the NodeJS terminal command to run it and create a file output.svg

:

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

      

How to pass parameter value from terminal to NodeJS?


Test dependencies:

  • svgcreator.node.js github repository :git clone 'git@github.com:hugolpz/svgcreator.node.js.git'

  • Requires jsdom, use: sudo npm install -g jsdom

    (global).

The solution used (@Matt_Harrison): we rely onprocess.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) * * * * * * * * * * * * * * * * * * * * * * * *
);

      

NodeJS terminal command:

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

      

+1 @Matt_Harrison's answer and question is appreciated!

+3


source to share


1 answer


In the terminal, you can use environment variables :

$ COLOR=#FFFFFF node jsdom.node.js

      

In your JS do:

var color = process.env.COLOR;

      


Or, you can add additional 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 suggest looking into the Minimist or Commander library for a more fully featured solution.

+11


source







All Articles