Start up script for node.js repl
Is there a way configure node.js's repl? I want to require jquery and undersc开发者_运维百科ore automatically whenever the repl starts. Is there a file (noderc?) that node.js loads when it starts the repl?
The equivalent in Python is to edit ~/.ipython/ipy_user_conf.py
with:
import_mod('sys os datetime re itertools functools')
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);
});
and you when you execute it with node myrepl.js
you get a REPL with those modules available.
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 :)
Feb 2017 - whilst I agree with the accepted answer, wished to add a little more commentary here.
Like to setup as follows (from home directory on my Mac)
.node
├── node_modules
│ ├── lodash
│ └── ramda
├── package.json
└── repl.js
Then repl.js may look like as follows:
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:
alias 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.
For up-to-date information on repl
and in particular repl.start
options see here
I tried this today but .start
required an argument. Also I think the useGlobal:true
was important. I wound up using:
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.
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.
.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.
Keeping things simple here's what I clabbered up.
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.
精彩评论