启动node.js repl的脚本 [英] Start up script for node.js repl

查看:114
本文介绍了启动node.js repl的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以配置node.js的repl?我希望每当repl启动时自动要求jquery和下划线.

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 it starts the repl?

Python中的等效项是使用以下命令编辑~/.ipython/ipy_user_conf.py:

The equivalent in Python is to edit ~/.ipython/ipy_user_conf.py with:

import_mod('sys os datetime re itertools functools')

推荐答案

我不知道任何这样的配置文件,但是如果要在REPL中使用模块foobar,则可以创建一个包含以下内容的文件myrepl.js:

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); 
});

,并且当您使用node myrepl.js执行它时,您将获得具有这些可用模块的REPL.

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

以此知识为基础,您可以将#!/path/to/node放在顶部并使其直接可执行,或者您可以修改repl.js模块的版本(源可在

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 :)

这篇关于启动node.js repl的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆