Dan Dascalescu
Dan Dascalescu

Reputation: 151926

How do I log anything to the console from meteor shell?

This GitHub issue documents that the console doesn't output anything to the meteor shell. Are there any workarounds? By default all console.log() statements will be output in the app's STDOUT (not in the shell).

Let's say we want to print certain items from a collection:

Meteor.users.find().forEach(function (user) { 
    if (...) console.log(user.emails[0].address;
});

That won't print anything. Here's what I've tried:

  1. process.stdout.write() - doesn't print anything
  2. Create a string buffer, append what we want to log to it, and evaluate it.

    var output = '';
    Meteor.users.find().forEach(function (user) {
        if (...)
            output += user.emails[0].address + "\n"
    });
    output;
    

    This works but the \n is echoed literally, not as a line feed.

  3. Evaluate the expression in the function. Predictably, this doesn't print anything.

Upvotes: 3

Views: 1135

Answers (1)

Watseka
Watseka

Reputation: 65

One workaround I've used is to run the app in the background, then run the shell in the same window. i.e.

meteor run &
meteor shell

That way, everything that gets output in the app's console gets printed to your window. Admittedly, this won't help if you want to log only specific messages to your shell, but it helps if all you want is to avoid switching back and forth between multiple windows all the time.

Upvotes: 2

Related Questions