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?

3
  • 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")' Commented Oct 11, 2017 at 16:42
  • Is your issue resolved.. ? If not, then you can try my answer posted below. Commented Oct 11, 2017 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')" Commented Oct 12, 2017 at 4:20

1 Answer 1

6

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()"
Sign up to request clarification or add additional context in comments.

2 Comments

var s = node -e "console.log(require('./myfile.js').myFun())" how to achieve this
I think it needs to be module.exports.myFun

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.