Equivalence of Rails console for Node.js

Create your own REPL by making a js file (ie: console.js) with the following lines/components:

  1. Require node's built-in repl: var repl = require("repl");
  2. Load in all your key variables like db, any libraries you swear by, etc.
  3. Load the repl by using var replServer = repl.start({});
  4. Attach the repl to your key variables with replServer.context.<your_variable_names_here> = <your_variable_names_here>. This makes the variable available/usable in the REPL (node console).

For example: If you have the following line in your node app: var db = require('./models/db') Add the following lines to your console.js

 var db = require('./models/db');
 replServer.context.db = db;
  1. Run your console with the command node console.js

Your console.js file should look something like this:

var repl = require("repl");

var epa = require("epa");
var db = require("db");

// connect to database
db.connect(epa.mongo, function(err){
  if (err){ throw err; }

  // open the repl session
  var replServer = repl.start({});

  // attach modules to the repl context
  replServer.context.epa = epa;
  replServer.context.db = db;  
});

You can even customize your prompt like this:

var replServer = repl.start({
  prompt: "Node Console > ",
});

For the full setup and more details, check out: http://derickbailey.com/2014/07/02/build-your-own-app-specific-repl-for-your-nodejs-app/

For the full list of options you can pass the repl like prompt, color, etc: https://nodejs.org/api/repl.html#repl_repl_start_options

Thank you to Derick Bailey for this info.


UPDATE:

GavinBelson has a great recommendation for running with sequelize ORM (or anything that requires promise handling in the repl).

I am now running sequelize as well, and for my node console I'm adding the --experimental-repl-await flag.

It's a lot to type in every time, so I highly suggest adding:

"console": "node --experimental-repl-await ./console.js"

to the scripts section in your package.json so you can just run:

npm run console

and not have to type the whole thing out.

Then you can handle promises without getting errors, like this:

const product = await Product.findOne({ where: { id: 1 });


Here is the way to do it, with SQL databases:

Install and use Sequelize, it is Node's ORM answer to Active Record in Rails. It even has a CLI for scaffolding models and migrations.

node --experimental-repl-await

> models = require('./models'); 
> User = models.User; //however you load the model in your actual app this may vary
> await User.findAll(); //use await, then any sequelize calls here

TLDR

This gives you access to all of the models just as you would in Rails active record. Sequelize takes a bit of getting used to, but in many ways it is actually more flexible than Active Record while still having the same features.

Sequelize uses promises, so to run these properly in REPL you will want to use the --experimental-repl-await flag when running node. Otherwise, you can get bluebird promise errors

If you don't want to type out the require('./models') step, you can use console.js - a setup file for REPL at the root of your directory - to preload this. However, I find it easier to just type this one line out in REPL


I am not very experienced in using node, but you can enter node in the command line to get to the node console. I then used to require the models manually


It's simple: add a REPL to your program