Start up script for node.js repl

前端 未结 5 647
醉酒成梦
醉酒成梦 2020-12-30 02:01

Is there a way configure node.js\'s repl? I want to require jquery and underscore automatically whenever the repl starts. Is there a file (noderc?) that node.js loads when i

5条回答
  •  旧巷少年郎
    2020-12-30 02:34

    I don't know of any such configuration file, but if you want to have modules foo and bar be available in a REPL, you can create a file myrepl.js containing:

    var myrepl = require("repl").start();
    ["foo", "bar"].forEach(function(modName){
        myrepl.context[modName] = require(modName); 
    });
    

    and you when you execute it with node myrepl.js you get a REPL with those modules available.

    Armed with this knowledge you can put #!/path/to/node at the top and make it executable directly, or you could modify your version of the repl.js module (source available at https://github.com/joyent/node/blob/master/lib/repl.js for inspection) or whatever :)

提交回复
热议问题