How to debug and log own code on the server side of Meteor?


Question

Never mind. The reason this did not work: I forgot to meteor reset so debugger did not get a chance to stop. Duh!

More info: I am using the method in the answer by Mason Chang to the related question, not the kill -s USR1 [proc_id] (where I could see the scripts, but not able to stop in the startup() function.). Also, I am using meteorite.

I am trying to debug the Meteor.startup(function ()) code on Meteor server side (i.e., under /server) with node-inspector, I have read this question, and following the answer to change run.js, but somehow, my own script for the startup function does not show up in the scripts section of Chrome.

How do I see my code here and set break points and stop at those points? BTW, the Meteor_debug() does not output anything to stdout, stderr, or node-inspector browser console. I also tried console.log() with no avail. How to enable logging on Meteor server side?

If it matters, I am on auth branch.

The code here is very simple (/server/bootstrap.js):

Meteor.startup(function () {
 if (Logs.find().count() === 0) {
  var data = [/*...some data...*/];
  var timestamp = (new Date()).getTime();
  Meteor._debug("timestamp: "+timestamp+", data.len: " + data.length);
  debugger;
  for (var i = 0; i < data.length; i++) {
    data[i].timestamp = timestamp++;
    var entry_id = Logs.insert(data[i]);
    Meteor._debug("entry_id: "+ entry_id);
  }
 }
});
1
9
5/23/2017 12:06:49 PM

Accepted Answer

Now that I know how to do this, I will answer my own question so that we can keep this information (in details) here: (This is based on Mason Chang's answer to this question.)

  1. Stop meteor execution.
  2. Edit /usr/lib/meteor/app/meteor/run.js (or the corresponding run.js installed by meteorite in HOME//.meteorite/meteors/meteor/meteor/[LONG_HEX_CODE]/app/meteor):
    change the line
    [path.join(bundle_path, 'main.js'), '--keepalive']
    to
    ['--debug-brk', path.join(bundle_path, 'main.js'), '--keepalive']
    //--debug-brk makes the new thread break at the first line;
  3. Add debugger statements as breakpoints in your server code;
  4. Run node-inspector & in a server terminal. (google "node-inspector" to install it.)
  5. Run meteor; (this will not have the debugger attached as there's no server thread yet, if you have no client window open.)
  6. Refresh client browser window; (to initiate a server thread that will break at the first line, and be attached to node-inspector.)
  7. Open a browser window at [SERVER:8080], your server code stops at first line (main.js in your [PROJECT_DIR]/.meteor/local/build);
  8. Hit the RUN button on the debugger browser window; depending on where your debugger statements are, you may have to do some triggering actions in client browser window to run to the debugger breakpoints. (Note that if you wait too long to hit the RUN button, your client window may time out, and you have to refresh again.)
  9. Now you can do the usual debugging stuff in server debugger window: step through, watch variables, execute in console, look at the stack, etc.

Edit: For logging on server side, you can use either Meteor._debug() and console.log(), they will show up in the terminal where you run meteor. On client side, these logging statements will output to the console of your browser's dev. tools.

10
5/23/2017 12:01:26 PM

On MacOSX, you can use with Chrome :

NODE_OPTIONS="--debug-brk" meteor

and in another terminal

node-inspector --debug-port=5858 --web-port=12345

Then connect Chrome to 127.0.0.1:12345/debug?port=5858

Otherwise with Webstorm, just create a Node.js Remote Debug configuration and run it :

Name : Meteor
Host : 127.0.0.1
Port 5858

Note that once the server has started, you need to press run in order for Meteor to load, and then pause in order to debug from the server console.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon