Run function in script from command line (Node JS)

前端 未结 11 480
情书的邮戳
情书的邮戳 2020-12-07 07:25

I\'m writing a web app in Node. If I\'ve got some JS file db.js with a function init in it how could I call that function from the command line?

11条回答
  •  暖寄归人
    2020-12-07 08:15

    Simple, in the javascript file testfile.js:

    module.exports.test = function () {
       console.log('hi');
    };
    this.test();
    

    Running at the prompt:

    node testfile.js
    

提交回复
热议问题