hek*_*ran 19 startupscript startup rc node.js read-eval-print-loop
有没有办法配置node.js的repl?我想在repl启动时自动要求jquery和下划线.是否有一个文件(noderc?)node.js在启动repl时加载?
Python中的等价物是用以下内容编辑~/.ipython/ipy_user_conf.py:
import_mod('sys os datetime re itertools functools')
Run Code Online (Sandbox Code Playgroud)
nic*_*ten 17
我不知道任何这样的配置文件,但如果你想拥有模块foo并bar在REPL中可用,你可以创建一个myrepl.js包含以下内容的文件:
var myrepl = require("repl").start();
["foo", "bar"].forEach(function(modName){
myrepl.context[modName] = require(modName);
});
Run Code Online (Sandbox Code Playgroud)
当你执行它时,node myrepl.js你得到一个可用的那些模块的REPL.
有了这些知识,您可以将其置于#!/path/to/node顶部并使其直接执行,或者您可以修改您的repl.js模块版本(可从https://github.com/joyent/node/blob/master/lib/获取源代码repl.js进行检查)或者其他:)
2017 年 2 月- 虽然我同意已接受的答案,但希望在这里添加更多评论。
\n\n喜欢按如下方式设置(从我的 Mac 上的主目录)
\n\n.node\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lodash\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ramda\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 repl.js
那么 repl.js 可能如下所示:
\n\nconst repl = require(\'repl\');\n\nlet r = repl.start({\n ignoreUndefined: true,\n replMode: repl.REPL_MODE_STRICT\n});\n\nr.context.lodash = require(\'lodash\');\nr.context.R = require(\'ramda\');\n// add your dependencies here as you wish..\nRun Code Online (Sandbox Code Playgroud)\n\n最后,将别名放入您的.bashrc或.zshrc文件等中(取决于您的 shell 首选项) - 类似于:
alias noder=\'node ~/.node/repl.js\'
现在,要使用此配置,您只需noder从命令行键入即可。上面,我还指出我总是想加入strict mode,并且不希望undefined打印到控制台进行声明等。
repl有关特定repl.start选项的最新信息,请参阅此处
小智 5
我今天尝试了这个,但.start需要争论。我也认为这useGlobal:true很重要。我最终使用:
var myrepl=require('repl').start({useGlobal:true});
myrepl.context['myObj']=require('./myObject');
Run Code Online (Sandbox Code Playgroud)
将此代码保存在中,test.js我可以在 REPL 中进行node test.js访问。myObj
可能是 Node.js 的一个较新功能(因为这个问题已经有四年了),但是您可以像 ipython 一样加载和保存 repl 历史记录。
.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
Run Code Online (Sandbox Code Playgroud)
我不知道如何在启动 shell 时自动执行此操作,但.load something目前对我来说足够方便。
| 归档时间: |
|
| 查看次数: |
2927 次 |
| 最近记录: |