1

Hi i have js file like below

var myfun = function(){ return "string from function." }

i Need command line like below

node myfile.js myfun()

output : string from function.

how to achieve this?

Abinaya
  • 364
  • 2
  • 6
  • 16
  • Yes i have tried that but m running js file from node_module as like below `node -e 'require(./node_modules/md-2-json/index.js).parse("#content")'` – Abinaya Oct 11 '17 at 16:42
  • Is your issue resolved.. ? If not, then you can try my answer posted below. – Mayur Agarwal Oct 11 '17 at 18:09
  • The problem is i used single quote instead of double quote.. `node -e "require(./node_modules/md-2-json/index.js).parse('#content')"` – Abinaya Oct 12 '17 at 04:20

1 Answers1

5

One option is to use make-runnable you will need just to add require('make-runnable'); at the end of the file and you can call it like node myfile.js myfun.

Or you can change myfile.js contents to:

module.exports.myFun = function () { return "string from function." };

and call it from the command line using this command:

node -e "console.log(require('./myfile.js').myFun())"

or even simpler, with this one:

node -p "require('./myfile.js').myFun()"
x-magix
  • 2,623
  • 15
  • 19
Andrzej Smyk
  • 1,684
  • 11
  • 21