3

Hi I have a standalone script which should be run on commandline (not trigger by API request)

I can run this script this way node db.js

However, could I be able to put any breakpoint on this kind of script.

And stop at the breakpoint when debugging?

I can not imagine we can only use console.log to debug.

var mongoose = require('./db');
fs= require('fs');
var AppCtrl = require('../handlers/handlerModule.js');

AppCtrl.joinJSONFiles("DeltaAirlines",function (err, data) {
   data.forEach()
       ....
})
newBike
  • 12,989
  • 25
  • 88
  • 158
  • You could call some function that blocks, say, until the user tells it to continue. This would pause the entire program because JS is single-threaded. What specific problem, if any, are you trying to debug? Why is `console.log` insufficient? – qxz Aug 28 '16 at 00:31
  • [This question](http://stackoverflow.com/questions/1911015/how-do-i-debug-node-js-applications) (from the "Related" section on the right) may be helpful. See the [accepted answer](http://stackoverflow.com/a/2536734/1848578). – qxz Aug 28 '16 at 00:41
  • 1
    you know, sometime you need to stand a breakpoint, and try to print and execute some functions at the breakpoint to see what's going on. it's not efficient to console.log again and again :) Just like developing with angular or react I can not living without breakpoint – newBike Aug 28 '16 at 01:24

1 Answers1

3

Reference: Debugger | Node.js v6.4.0 Documentation

Inserting the statement debugger; into the source code of a script will enable a breakpoint at that position in the code:

// myscript.js
x = 5;
setTimeout(() => {
  debugger;
  console.log('world');
}, 1000);
console.log('hello');

To use the debugger, run your program with node debug db.js. When a breakpoint is reached, the debugger prompt (debug>) appears. Some sample commands:

  • cont, c – continue execution
  • next, n – step next
  • backtrace, bt – print backtrace of current execution frame
  • exec expression – execute an expression in the debugging script's context
  • repl – open debugger's repl for evaluation in the debugging script's context

For a full list, see the Command reference section of the above link.

qxz
  • 3,697
  • 1
  • 11
  • 29