It is not possible to set a stop function inside a function when using the required internal closure

Using the node inspector, I cannot set a breakpoint in the following node.js code. (Content main.js )

 (function() { require('underscore'); var doSomething = function(callback) { callback('doSomething Finished'); } doSomething(function(x) { console.log(x); }); }).call(this); 

I can easily set a breakpoint on line 2, line 4 or line 8, however no matter how hard I try, the debugger does not allow me to set a breakpoint on line 5 or line 9. To be clear, m, using the following commands to run node - inspector

 node --debug-brk main.js node-inspector 

I also tried to debug a web storm, but the problem persists. If I remove the string require('underscore'); , the problem will disappear immediately, and I can again set a breakpoint inside the function body. The problem also disappears if I remove the outermost closure function. It appears that the interaction between require and file-level closure minimizes node debugging functionality. Has anyone experienced this problem on their own and / or knows any workarounds to be able to break functions inside the body?

EDIT: My version of node js

 Tony:~ $ node --version v0.10.12 Tony:~ $ 
+6
source share
2 answers

This may not be the answer you want to hear, as it does not explain why you cannot set any breakpoints, but I would just remove your require statement from the closure and place it at the top level. I would go even further and recommend that you not use a closure like the one above.

The reason is because node uses its own modular system and, unlike Javascript in the browser, declaring top-level variables does not pollute the global namespace. This requires a requirement (...). Thus, you do not get anything by wrapping your code in an immediately called function (unless, of course, you want your module to be able to run both the client and server sides).

I would suggest that the reason you cannot set breakpoints is because the V8 runtime recognizes an unnecessary closure and then optimizes your code for you. The rewritten code may not have the correct display of the source, and therefore breakpoints cannot be set.

So, two sentences:

  • require calls not to look like regular statements. They are more like import operations in Java and are specially processed by the compiler. They should always be at the top level in the node file.
  • No need to wrap your code with an anonymous function when in Node.
+3
source

I came across the same issue with the same setup.

I added a breakpoint after defining the objective function (this was the only place I could add a breakpoint). When the debugger reached this breakpoint and the function was actually defined, I was able to add breakpoints to the actual target function ...

+11
source

Source: https://habr.com/ru/post/949725/


All Articles