Run function in script from command line (Node JS)

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

    simple way:

    let's say you have db.js file in a helpers directory in project structure.

    now go inside helpers directory and go to node console

     helpers $ node
    

    2) require db.js file

    > var db = require("./db")
    

    3) call your function (in your case its init())

    > db.init()
    

    hope this helps

提交回复
热议问题