node.js - Cannot set breakpoint inside function when using require inside closure -
using node-inspector, i'm unable set breakpoint in following node.js code. (content of main.js
)
(function() { require('underscore'); var dosomething = function(callback) { callback('dosomething finished'); } dosomething(function(x) { console.log(x); }); }).call(this);
i can set breakpoint on line 2, line 4 or line 8, no matter how hard try debugger won't let me set break point on line 5 or line 9. clear, i'm using following commands run node-inspector
node --debug-brk main.js node-inspector
i tried debug in web storm, issue persists. if remove line require('underscore');
, problem goes away , i'm able set break point inside function body again. problem goes away if remove outermost closure function. seems interaction between require
, file level closure screwing node debugging functionality. has experienced problem , / or knows workarounds able break inside function body?
edit: node js version
tony:~ $ node --version v0.10.12 tony:~ $
this may not answer want hear doesn't explain why can't set breakpoints, remove require statement closure , place top-level. go further , recommend don't use closure 1 above @ all.
the reason node uses own module system, , unlike javascript in browser, declaring variables top-level not pollute global namespace. require(...) comes in. so, gain nothing wrapping code in invoked function (unless of course want module able run both client side , server side).
i guess reason not able set breakpoints v8 runtime recognizing unnecessary closure , optimizing code you. rewritten code may not have correct source mapping , breakpoints cannot set.
so, 2 suggestions:
- require calls not regular statements. more similar import statements in java , handled specially compiler. should top-level in node file.
- no need wrap code in anonymous function when in node.
Comments
Post a Comment