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?
有没有办法配置node.js的repl?我想在repl启动时自动要求jquery和下划线。是否有一个文件(noderc?)node.js在启动repl时加载?
The equivalent in Python is to edit ~/.ipython/ipy_user_conf.py
with:
Python中的等价物是使用以下命令编辑〜/ .ipython / ipy_user_conf.py:
import_mod('sys os datetime re itertools functools')
5 个解决方案
#1
16
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:
我不知道任何这样的配置文件,但是如果你想在REPL中使用模块foo和bar,你可以创建一个包含以下内容的文件myrepl.js:
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.
当你使用节点myrepl.js执行它时,你得到一个带有这些模块的REPL。
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 :)
有了这些知识,您可以将#!/ path / to / node放在顶部并使其直接执行,或者您可以修改您的repl.js模块版本(可从https://github.com/joyent/获取源代码) node / blob / master / lib / repl.js用于检查)或者其他:)
#2
4
I tried this today but .start
required an argument. Also I think the useGlobal:true
was important. I wound up using:
我今天尝试了这个但是.start需要一个参数。另外我认为useGlobal:true很重要。我结束了:
var myrepl=require('repl').start({useGlobal:true});
myrepl.context['myObj']=require('./myObject');
Saving this code in test.js
I could do a node test.js
then access myObj
in REPL.
在test.js中保存此代码我可以执行节点test.js然后在REPL中访问myObj。
#3
3
Might be a newer feature of Node.js (since this question is four years old), but you can load and save repl history like ipython.
可能是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
I can't figure out how to execute this automatically when starting the shell, but .load something
is convenient enough for me at the moment.
我无法弄清楚如何在启动shell时自动执行此操作,但是。对于我来说,.load足够方便。
#4
2
Feb 2017 - whilst I agree with the accepted answer, wished to add a little more commentary here.
2017年2月 - 虽然我同意接受的答案,但希望在此添加更多评论。
Like to setup as follows (from home directory on my Mac)
喜欢设置如下(从我Mac上的主目录)
.node ├── node_modules │ ├── lodash │ └── ramda ├── package.json └── repl.js
.node├──node_modules│├──lodash│└──ramda├──package.json└──repl.js
Then repl.js may look like as follows:
然后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..
And finally, put an alias into your .bashrc
or .zshrc
file etc (depending on your shell prefs) - something like:
最后,在你的.bashrc或.zshrc文件等中添加一个别名(取决于你的shell prefs) - 类似于:
alias noder='node ~/.node/repl.js'
别名noder ='node~ / .node / repl.js'
Now, to use this configuration, you just have to type noder
from the command line. Above, I also specified that I'd always like to be in strict mode
, and do not want undefined
printed to the console for declarations etc.
现在,要使用此配置,您只需从命令行键入noder即可。上面,我还指出我总是喜欢处于严格模式,并且不希望未定义打印到控制台以进行声明等。
For up-to-date information on repl
and in particular repl.start
options see here
有关repl的最新信息,特别是repl.start选项,请参见此处
#5
1
Keeping things simple here's what I clabbered up.
保持简单,这就是我的抨击。
repl.js:
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()
I can invoke this with node repl
which feels right, and I don't care about globals, because I am just messin' around in the repl.
我可以使用感觉正确的节点repl调用它,我不关心全局变量,因为我只是在repl中乱码。
#1
16
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:
我不知道任何这样的配置文件,但是如果你想在REPL中使用模块foo和bar,你可以创建一个包含以下内容的文件myrepl.js:
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.
当你使用节点myrepl.js执行它时,你得到一个带有这些模块的REPL。
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 :)
有了这些知识,您可以将#!/ path / to / node放在顶部并使其直接执行,或者您可以修改您的repl.js模块版本(可从https://github.com/joyent/获取源代码) node / blob / master / lib / repl.js用于检查)或者其他:)
#2
4
I tried this today but .start
required an argument. Also I think the useGlobal:true
was important. I wound up using:
我今天尝试了这个但是.start需要一个参数。另外我认为useGlobal:true很重要。我结束了:
var myrepl=require('repl').start({useGlobal:true});
myrepl.context['myObj']=require('./myObject');
Saving this code in test.js
I could do a node test.js
then access myObj
in REPL.
在test.js中保存此代码我可以执行节点test.js然后在REPL中访问myObj。
#3
3
Might be a newer feature of Node.js (since this question is four years old), but you can load and save repl history like ipython.
可能是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
I can't figure out how to execute this automatically when starting the shell, but .load something
is convenient enough for me at the moment.
我无法弄清楚如何在启动shell时自动执行此操作,但是。对于我来说,.load足够方便。
#4
2
Feb 2017 - whilst I agree with the accepted answer, wished to add a little more commentary here.
2017年2月 - 虽然我同意接受的答案,但希望在此添加更多评论。
Like to setup as follows (from home directory on my Mac)
喜欢设置如下(从我Mac上的主目录)
.node ├── node_modules │ ├── lodash │ └── ramda ├── package.json └── repl.js
.node├──node_modules│├──lodash│└──ramda├──package.json└──repl.js
Then repl.js may look like as follows:
然后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..
And finally, put an alias into your .bashrc
or .zshrc
file etc (depending on your shell prefs) - something like:
最后,在你的.bashrc或.zshrc文件等中添加一个别名(取决于你的shell prefs) - 类似于:
alias noder='node ~/.node/repl.js'
别名noder ='node~ / .node / repl.js'
Now, to use this configuration, you just have to type noder
from the command line. Above, I also specified that I'd always like to be in strict mode
, and do not want undefined
printed to the console for declarations etc.
现在,要使用此配置,您只需从命令行键入noder即可。上面,我还指出我总是喜欢处于严格模式,并且不希望未定义打印到控制台以进行声明等。
For up-to-date information on repl
and in particular repl.start
options see here
有关repl的最新信息,特别是repl.start选项,请参见此处
#5
1
Keeping things simple here's what I clabbered up.
保持简单,这就是我的抨击。
repl.js:
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()
I can invoke this with node repl
which feels right, and I don't care about globals, because I am just messin' around in the repl.
我可以使用感觉正确的节点repl调用它,我不关心全局变量,因为我只是在repl中乱码。