Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I debug in nodejs, any tool similar to pdb or pry in Ruby or Python

I ran my script by

node debug sample.js

It stopped at the target position.

Somehow, I don't know how to inspect the variables.

For example, I want to print out the variable db , but it couldn't.

Any recommend debugger tool on node.js

Thanks

sample.js

// Generated by CoffeeScript 1.9.2
(function() {
  var MongoClient, print;
  print = require('awesome-print');
  MongoClient = require('mongodb').MongoClient;
  MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    debugger;
    db.command({
      listCollections: 1
    }, function(err, o) {
      debugger;
      return print(o["cursor"]);
    });
  });
}).call(this);

exception

 20
 21 });
debug> print(db)
repl:1
print(db)
^
ReferenceError: print is not defined
    at repl:1:1
    at Object.exports.runInContext (vm.js:64:17)
    at Interface.controlEval (_debugger.js:975:21)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
debug> db
repl:1
db
^
ReferenceError: db is not defined
    at repl:1:1
    at Object.exports.runInContext (vm.js:64:17)
    at Interface.controlEval (_debugger.js:975:21)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
like image 861
newBike Avatar asked Nov 09 '22 09:11

newBike


1 Answers

You're probably looking for the "repl" command in the command-line debugger. Also node-inspector is a great way to hook up chrome's awesome devtools to a node process. You can learn how to use node-inspector in my blog post: Lighting up your javascript with the debugger

like image 89
Peter Lyons Avatar answered Nov 14 '22 22:11

Peter Lyons