Run function in script from command line (Node JS)

前端 未结 11 481
情书的邮戳
情书的邮戳 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 07:59

    If you turn db.js into a module you can require it from db_init.js and just: node db_init.js.

    db.js:

    module.exports = {
      method1: function () { ... },
      method2: function () { ... }
    }
    

    db_init.js:

    var db = require('./db');
    
    db.method1();
    db.method2();
    

提交回复
热议问题