启动node.js repl的脚本

有没有办法configurationnode.js的repl? 我想要求每当repl开始时自动jQuery和下划线。 是否有一个文件(noderc?)node.js启动repl时加载?

Python中的等价物是编辑~/.ipython/ipy_user_conf.py

 import_mod('sys os datetime re itertools functools') 

我不知道任何这样的configuration文件,但是如果你想让模块foobar在REPL中可用,你可以创build一个myrepl.js文件, myrepl.js包含:

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

而当你用node myrepl.js执行它的时候,你会得到一个可用的模块的REPL。

有了这些知识,你可以把#!/path/to/node放在最上面,直接让它可执行,或者你可以修改你的repl.js模块的版本(可以在https://github.com/joyent/上find源码) 节点/ blob / master / lib / repl.js进行检查)或其他:)

我今天试了这个,但是开始需要一个参数。 另外我觉得useGlobal:true是很重要的。 我结束了使用:

 var myrepl=require('repl').start({useGlobal:true}); myrepl.context['myObj']=require('./myObject'); 

将这段代码保存在test.js我可以做一个node test.js然后在REPL中访问myObj

可能是Node.js的一个新特性(因为这个问题已经四年了),但是你可以像ipython一样加载和保存repl历史logging 。

 .break - While inputting a multi-line expression, sometimes you get lost or just don't care about completing it. .break will start over. .clear - Resets the context object to an empty object and clears any multi-line expression. .exit - Close the I/O stream, which will cause the REPL to exit. .help - Show this list of special commands. .save - Save the current REPL session to a file .save ./file/to/save.js .load - Load a file into the current REPL session. .load ./file/to/load.js 

我无法弄清楚如何在启动shell时自动执行这个操作,但是.load something现在对我来说已经足够方便了。

20172月 – 虽然我同意接受的答案,但希望在这里增加一点评论。

喜欢设置如下(从我的Mac上的主目录)

.node ├── node_modules │ ├── lodash │ └── ramda ├── package.json └── repl.js

那么repl.js可能如下所示:

 const repl = require('repl'); let r = repl.start({ ignoreUndefined: true, replMode: repl.REPL_MODE_STRICT }); r.context.lodash = require('lodash'); r.context.R = require('ramda'); // add your dependencies here as you wish.. 

最后,将别名放入.bashrc.zshrc文件等(取决于您的shell首选项) – 如下所示:

alias noder='node ~/.node/repl.js'

现在,要使用此configuration,只需从命令行键入noder 。 在上面,我还指定了我总是喜欢strict mode ,不要将undefined打印到控制台进行声明等。

有关repl的最新信息,特别是repl.start选项,请参阅此处

保持简单的东西就是我的缺点。

repl.js:

 // things i want in repl global.reload = require('require-nocache')(module) // so I can reload modules as I edit them global.r = require('ramda') // <3 http://ramdajs.com/ // launch, also capture ref to the repl, in case i want it later global.repl = require('repl').start() 

我可以用node repl来调用这个感觉是正确的,我不关心全局variables,因为我只是在repl中混乱。