Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting with app code in the node REPL

One of the pleasures of frameworks like Rails is being able to interact with models on the command line. Being very new to node.js, I often find myself pasting chunks of app code into the REPL to play with objects. It's dirty.

Is there a magic bullet that more experienced node developers use to get access to their app specific stuff from within the node prompt? Would a solution be to package up the whole app, or parts of the app, into modules to be require()d? I'm still living in one-big-ol'-file land, so pulling everything out is, while inevitable, a little daunting.

Thanks in advance for any helpful hints you can offer!

like image 902
cantlin Avatar asked Dec 27 '25 16:12

cantlin


1 Answers

One-big-ol'-file land is actually a good place to be in for what you want to do. Nodejs can also require it's REPL in the code itself, which will save you copy and pasting.

Here is a simple example from one of my projects. Near the top of your file do something similar to this:

function _cb() {
  console.log(arguments)
}

var repl = require("repl");
var context = repl.start("$ ").context;
context.cb = _cb;

Now just add to the context throughout your code. The _cb is a dummy callback to play with function calls that require one (and see what they'll return).

like image 50
Dan Avatar answered Dec 30 '25 06:12

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!