Meteor: Debug on server side
Asked Answered
U

12

78

Does anyone know a good method to debug server side code? I tried enable Node.js debug then use node-inspector but it does not show any of my code.

I end up using console.log but this is very inefficient.

Update: I found the following procedure works on my Linux machine:

  1. When you run Meteor, it will spawn two processes

    process1: /usr/lib/meteor/bin/node /usr/lib/meteor/app/meteor/meteor.js

    process2: /usr/lib/meteor/bin/node /home/paul/codes/bbtest_code/bbtest02/.meteor/local/build/main.js --keepalive

  2. You need to send kill -s USR1 on process2

  3. Run node-inspector and you can see your server code

On my first try, I modify the last line on meteor startup script in /usr/lib/meteor/bin/meteor to

exec "$DEV_BUNDLE/bin/node" $NODE_DEBUG "$METEOR" "$@"

and run NODE_DEBUG=--debug meteor on command prompt. This only put --debug flag on process1 so I only see meteor files on node-inspector and could not find my code.

Can someone check this on Windows and Mac machine?

Unsophisticated answered 14/6, 2012 at 14:11 Comment(5)
FYI, instead of console.log, use Meteor._debug (it ends up calling console.log, but there is a note saying that it will be improved some day.)Oakman
See my answer, on MAC it is working, I was able to see and debug my js files.Fanestil
I tried this on my Mac, but no go.Seduce
@Harmal000 you linked to this question - did you mean to link to another one?Kaslik
This article is really useful about debugging in meteor.js joshowens.me/easily-debugging-meteor-jsHeterodyne
S
88

In Meteor 0.5.4 this has become a lot easier:

First run the following commands from the terminal:

npm install -g node-inspector
node-inspector &
export NODE_OPTIONS='--debug-brk'
meteor

And then open http://localhost:8080 in your browser to view the node-inspector console.

Update

Since Meteor 1.0 you can just type

meteor debug

which is essentially a shortcut for the above commands, and then launch node inspector in your browser as mentioned.

Update

In Meteor 1.0.2 a console or shell has been added. It may come in handy to output variables and run commands on the server:

meteor shell
Savick answered 22/2, 2013 at 10:20 Comment(5)
Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product.Campestral
How do I disable this debugger? Every time I run meteor this debugger runs and locks up my meteor app from running anything client-side.Unfriendly
console outputs always [object Object], for example : console.log('asd') [object Object]Rejoin
is it possible to print values? how?Rejoin
Is there any official documentation on meteor debug. I can't find my code to set a break point.Brusa
H
16

Meteor apps are Node.js apps. When running a Meteor app with the meteor [run] command, you can configure the NODE_OPTIONS environment variable to start node in debug mode.

Examples of NODE_OPTIONS environment variable values:

  • --debug
  • --debug=47977 - specify a port
  • --debug-brk - break on the first statement
  • --debug-brk=5858 - specify a port and break on the first statement

If you export NODE_OPTIONS=--debug, all meteor command run from the same shell will inherit the environment variable. Alternatively, you can enable debugging just for one run, with NODE_OPTIONS="--debug=47977" meteor.

To debug, run node-inspector in a different shell, then go to http://localhost:8080/debug?port=<the port you specified in NODE_OPTIONS>, regardless of what node-inspector tells you to run.

Hygrograph answered 14/2, 2013 at 11:7 Comment(0)
C
10

To start node.js in debug mode, I did it this way:

  1. open /usr/lib/meteor/app/meteor/run.js
  2. before

    nodeOptions.push(path.join(options.bundlePath, 'main.js')); 
    

    add

    nodeOptions.push('--debug');
    

Here are additional practical steps for your to attach debugger eclipse:

  1. use '--debug-brk' instead of '--debug' here, because it's easier for me to attach node.js using eclipse as debugger.
  2. add 'debugger;' in the code where you want to debug.(I prefer this way personally)
  3. run meteor in console
  4. attach to node.js in eclipse(V8 tools, attach to localhost:5858)
  5. run, wait for debugger to be hit

when you start meteor in your meteor app folder, you'll see that "debugger listening on port 5858" in console.

Carlettacarley answered 29/7, 2012 at 13:15 Comment(4)
if you're using mrt, of course the path to run.js is different.Alundum
... as in ~/.meteorite/meteors/meteor/meteor/0a148c69d6af9832006a6f6d27cc112ed90cb3e4/app/meteor/Alundum
My files are duplicated in /usr/lib and /usr/local/ by unknown reasons. If it doesn't work to you, try editing /usr/local/meteor/app/meteor/run.js instead of /usr/lib/meteor/app/meteor/run.jsRunion
Thanks, your solution is the easiest I ever found. It doesn't require to be killing process, neither set variables over the command line.Runion
R
10

On Meteor 1.0.3.1 (update to Sergey.Simonchik answer)

Start your server with meteor run --debug-port=<port-number>

Point browser to http://localhost:6222/debug?port=<port-number>

Where <port-number> is a port you specify.

In your code add a debugger; where you want to set your break point.

Depending on where debugger; is invoked, it will either break on your client or server browser window with inspector opened.

Rhinehart answered 18/2, 2015 at 0:56 Comment(0)
M
7

I like to set breakpoints via a GUI. This way I don't have to remember to remove any debugging code from my app.

This is how I managed to do it server side for my local meteor app:

meteor debug

start your app this way.

Open Chrome to the address it gives you. You MAY need to install https://github.com/node-inspector/node-inspector (it might come bundled with Meteor now? not sure)

You'll see some weird internal meteor code (not the app code you wrote). Press play to run the code. This code simply starts up your server to listen for connections.

Only after you press play you'll see a new directory in your debugger folder structure called "app". In there are your meteor project files. Set a breakpoint in there one the line you want.

Open the local address of your app. This will run your server side code and you you should be able to hit your breakpoint!

Note: you have to reopen the inspector and go through this process again each time your app restarts!

Malines answered 2/11, 2015 at 16:11 Comment(0)
C
6

As of Meteor 1.0.2 probably the best way for server-side debugging is directly via the new built-in shell: with running server run meteor shell. More info here: https://www.meteor.com/blog/2014/12/19/meteor-102-meteor-shell

Cragsman answered 29/1, 2015 at 5:19 Comment(0)
F
5

I am not sure why it was not working for you.
I am able to use it by following steps on console (Mac).

$ ps  
$ kill -s USR1 *meteor_node_process_id*  
$ node-inspector &

Above steps are mentioned on https://github.com/dannycoates/node-inspector. It is for attaching node-inspector to running node process.

Fanestil answered 14/6, 2012 at 18:21 Comment(6)
I have tested on Mac only, are you using Mac?Fanestil
Yeah, I have a Mac. How do you add the breakpoint in code? What are the exact steps you use for the whole thing?Seduce
after above (this answer's) steps, I start the inspector. $ node-inspector & opened 127.0.0.1:8080/debug?port=5858 in chrome. I was able to see my files in source tab in webkit-inspectorFanestil
I've tried this, adding both debugger and breakpoints in the inspector, but neither of them worked. Any idea why?Displace
When look at ps | grep node (or similar) make sure you pick the main.js pid to signal, not it's parent node process. This is how I got it to work.Colour
from what I remember & operator does launch process in the background as in Linux.Rejoin
M
4

I wrote a small meteor package called meteor-inspector which simplifies the use of node-inspector to debug meteor apps. It internally manages the lifecycle of node-inspector and hence, the user does not need to restart the debugger manually after some files have changed.

For more details and concrete usage instructions take a look at https://github.com/broth-eu/meteor-inspector.

Muraida answered 14/12, 2013 at 10:7 Comment(0)
S
4

for meteor 1.3.5.2, run

meteor debug --debug-port 5858+n n is a non-zero number, this will cause node-inspector use 8080+n as web port.

Secern answered 5/1, 2017 at 11:14 Comment(0)
A
3

WebStorm, the powerful IDE free for open source developers, makes it much easier to debug server-side.

I've tested it on Windows, and the configuration was painless - see my answer.

Askwith answered 12/2, 2014 at 2:3 Comment(0)
R
3

A inspector that solve my issues is meteor server console. Here is the process I followed to install it:

  1. In your project folder, add the smart package server-eval:

    mrt add server-eval
    

    For Meteor 1.0:

    meteor add gandev:server-eval
    
  2. Restart meteor.

  3. Download crx Chrome extension file from here.
  4. Open extensions page in Chrome and drag crx file to extensions page.
  5. Restart Chrome.
  6. Check the web inspector out to eval server side code:

    enter image description here

In comparison with node-inspector, I have a clearer output.

Rejoin answered 1/7, 2014 at 23:11 Comment(3)
from what I remember it worked with breakpoints, but I am not sure.Rejoin
do you remember how you set them? via dev tools or incode?Malines
dev tools, but don't remember exactly howRejoin
S
2

If you prefer to use nodeJS' official debugger you can call NODE_OPTIONS='--debug' meteor and then (on a different shell) node debug localhost:5858.

Stithy answered 18/11, 2014 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.