nodejs mongoose doesn't return to console after saving an entry

I use a small piece of code from console to test saving/retrieving entries in MongoDB database. It looks like this:

var mongoose = require('mongoose');
var models = require('../models');
mongoose.connect('localhost', 'users');
var john = new models.User({ name:'John' });
john.save(function (err, john) {
    if (err) {
        console.log(err)
    }
    else {
        console.log(john.name);
    }
});

After I run it, it shows the name when it saves the entry, then doesn't return me to console and doesn't show any more messages. How can I make it return me to console?

Your node.js program won't exit as long as a database connection is still open, so you need to close your connection pool after you're done with it like this:

var mongoose = require('mongoose');
var models = require('../models');
mongoose.connect('localhost', 'users');
var john = new models.User({ name:'John' });
john.save(function (err, john) {
    if (err) {
        console.log(err)
    }
    else {
        console.log(john.name);
    }
    mongoose.disconnect();
});

It returns you to the console once the call to save() is finished, but that's before your callback gets called. If the save operation took several minutes you could continue playing with the console in the meantime.

When the save operation finishes, your callback gets called and prints the john.name to the console. Printing to the console while in the REPL can be somewhat tricky because it gets written where you are typing.

So my guess is you think that everything is blocked because you don't see the prompt > but it is in fact there, it's just that console.log wrote on that line. If you try other commands, it should still work.