How do I load my script into the node.js REPL?

前端 未结 11 1627
执念已碎
执念已碎 2020-11-30 17:59

I have a script foo.js that contains some functions I want to play with in the REPL.

Is there a way to have node execute my script and then jump into a

11条回答
  •  孤城傲影
    2020-11-30 18:11

    Old answer

    type test.js|node -i
    

    Will open the node REPL and type in all lines from test.js into REPL, but for some reason node will quit after file ends

    Another problem is, that functions will not be hoisted.

    Better answer

    node -e require('repl').start({useGlobal:true}); -r ./test2.js
    

    Then all globals declared without var within test2.js will be available in the REPL

    not sure why var a in global scope will not be available

提交回复
热议问题