有没有办法配置node.js的repl?我想在repl开始时自动要求jQuery和下划线。是否有一个node.js在启动repl时加载的文件(noderc?)?为node.js启动脚本repl
在Python等效与编辑~/.ipython/ipy_user_conf.py
:
import_mod('sys os datetime re itertools functools')
有没有办法配置node.js的repl?我想在repl开始时自动要求jQuery和下划线。是否有一个node.js在启动repl时加载的文件(noderc?)?为node.js启动脚本repl
在Python等效与编辑~/.ipython/ipy_user_conf.py
:
import_mod('sys os datetime re itertools functools')
我不知道任何这样的配置文件,但如果你想拥有的模块foo
和bar
在REPL是可用,您可以创建一个包含文件myrepl.js
:
var myrepl = require("repl").start();
["foo", "bar"].forEach(function(modName){
myrepl.context[modName] = require(modName);
});
并且当您与node myrepl.js
执行它你可用这些模块REPL。
有了这些知识,你可以把#!/path/to/node
顶部和可执行直接使,或者你可以(在https://github.com/joyent/node/blob/master/lib/repl.js检查可用的源代码)修改的repl.js模块的版本或任何:)
我今天尝试过,但.start
需要一个参数。另外我认为useGlobal:true
很重要。我结束了使用:
var myrepl=require('repl').start({useGlobal:true});
myrepl.context['myObj']=require('./myObject');
保存这段代码test.js
我可以做一个node test.js
然后在REPL访问myObj
。
我需要这个来获得@ nicolaskruchten的例子。感谢你们两位! – Nate
可能是Node.js的新特性(因为这个问题已经四年了),但you can load and save repl history就像ipython一样。
.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
便利足以让我的时刻。
保持简单的事情就是我的缺点。
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
这感觉对调用这个,我不关心全局,因为我只是在REPL messin'左右。
2017年2月 - 虽然我同意接受的答案,但希望在此补充一点评论。
像设置如下(从我的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'
现在,使用此配置,您只需从命令行输入noder
即可。以上,我还特别指出,我总是喜欢在strict mode
,并且不希望undefined
打印到控制台的声明等
要获得的最新信息repl
,特别repl.start
选择参阅here
非常好!我做了一个用别名加载模块的变体。 var modules = {jquery:'$',下划线:'_und'}; for(var mod in modules){ my_repl.context [modules [mod]] = require(mod); } – hekevintran