I have written some test cases and I am trying to debug the test cases using using mocha and node debug.
I have mocha installed on my machine
I installed node-debug npm install -g node-debug
Then I am running test by this command: node-debug _mocha test.js
This opens a browser window, but I am getting following error
Detached from the target
Remote debugging has been terminated with reason: Error: connect ECONNREFUSED. Is node running with --debug port 5858?
Please re-attach to the new target.
See the attached image for more information:
How can I get rid of this error. Please help.
That is expected behavior once your tests have finished running. Put a long timeout in your test so it does not die and try it again.
You will also want to run mocha with a very high timeout so your tests don't die timing out.
The node-debug project is deprecated.
If using node > 6.3, The debugger is a part of the node core!
see https://medium.com/#paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27#.3qx9qfmwl
If using node < 6.3, you can use node-inspector
Check out https://github.com/node-inspector/node-inspector
The rest should be smooth sailing if your test is not ending the process before you're done debugging. You can also set --debug-brk so it stops on the first line of your code to give us slow humans a chance to get to the debuggeer.
Best of luck!
Related
I've been trying to use Nuclide/Atom to launch and debug a unit test that uses Babel and ES6+ code. The launch config looks like this:
Node runs the unit test as if I had run it from the command-line, and does not stop at my breakpoints. If I use the same invocation at the command-line with --inspect-brk I can debug correctly (with sourcemaps) from the chrome-devtools url in Chrome. Is there a way to make this work? I can't really "attach" since the unit tests are, and should be, a straight-shot script execution.
Nuclide currently does not support the new v8 Inspector protocol for debugging. You will need to use --debug flag to debug with Nuclide. Note, however, that the old debugger protocol has been removed from Node.js starting with Node.js 8.0.
PS. You can attach to a running Node.js process with Nuclide debugger just fine - just start your tests with node --debug --debug-brk ... and then attach the debugger from Nuclide. The test process will be stopped at first line, allowing you to resume execution at your own convenience.
I am trying to use node-inspector with a simple script. I have some console logs in place so I can tell if it's running or not. I have tried 2 ways:
node-debug test.js
With this approach, the debugger opens in chrome, but does not break and runs without giving me a chance to enter break points.
In terminal window #1:
~ $ node-inspector
Node Inspector v0.9.2
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Open debugger in chrome. In terminal window #2:
node --debug-brk test.js
The script seems to be waiting, but the debugger window I opened has nothing going on, So I refresh the page. As soon as I do, I see my console logs (not breaking).
I'm running OSX Yosemite (10.10.1), Node.js v0.12.0, NPM 2.5.1, and Node-inspector v0.9.2.
Any ideas why this is happening?
This is a known bug when running on Node v0.12 and io.js, see https://github.com/node-inspector/node-inspector/issues/534
In using node.js's debugger, I've been debugging by running the node process with node --debug-brk XXXX.js. The annoying thing is, if I ever accidentally disconnect, I have to start the process all over again. Is there a way to reconnect to the debugger? When I try (via intelliJ), it simply never reconnects.
Try node-inspector it will reconnect to node server when you open it in browser but debugging will start from starting.
You can also use nodewebkit which makes it easy for debugging code.
The V8 debugger released as part of the Google Chrome Developer Tools can be used to debug Node.js scripts. A detailed explanation of how this works can be found in the Node.js GitHub wiki.
Alternatives would be
Node.js version 0.3.4+ has built-in debugging support.
node debug script.js
Manual: http://nodejs.org/api/debugger.html
Profiling with Profiler
Note: the profiler module is deprecated, and may not work with version 0.12 of node
Install globally npm install -g profiler
Start your process with node --prof this will create a v8.log file
Build nprof by running ~/.nvm/v0.8.22/lib/node_modules/profiler/tools/build-nprof
Run ~/.nvm/v0.8.22/lib/node_modules/profiler/nprof this will read the v8.log profile and give you nice output.
CPU and Memory Profiling with NodeTime
Install to your application, npm install nodetime
Include in your application, require('nodetime').profile()
Follow the instructions it will output to console
Alternatively, you may want to use look, which is based on nodetime, but it doesn't send data to nodetime.com.
Developer Tools Debugging with Node Inspector
Install it globally: npm install -g node-inspector
Run your application in debug mode: node-debug your/node/program.js (or attach to a running process: kill -s USR1 <your node process id>)
In another terminal window run node-inspector: node-inspector
Open http://127.0.0.1:8080/debug?port=5858 (or debug remotely by replacing 127.0.0.1 with your host; make sure port 8080 is open).
Webkit Developer Tools Profiling with Node Webkit Agent
Install to your application, npm install webkit-devtools-agent
Include in your application, agent = require('webkit-devtools-agent')
Activate the agent: kill -SIGUSR2 <your node process id>
Access the agent via the appropriate link
I'm using grunt to launch my server with livereload and other tasks.
I've followed this and this post to run my grunt tasks.
It's working but I can't debug properly (when I set some breakpoints, there aren't hit)
When I launch the script, here is what I got:
As you can see the debugger appears in a 2nd tab, but it's not doing anything. (even if it says it's connected successfully).
To debug my app I've to stop this 2nd tab, and run a remote debugger... Anyway to fix this?
Additional info: The server is run in another process. If I run it in the same process as grunt, there are no issues.
What Node.js version do you use? In 0.10.x the child process occupies the same port as a master process, so debugging won't work by default. Related ticket: https://github.com/joyent/node/issues/5318. Can you check if it works for you using Node.js ersion >= 0.11.4? Also, what Grunt tasks are being used?
As mentioned by lena, the issue seems to be fixed in WebStorm 8.
I am using mocha to test my code. I am using node inspector to debug my code.
bash
mocha test/test.* --debug-brk
This works but not so well. It stops at the first line of code in mocha. I want it to stop it at my code. I tried using the 'debugger' key word to make a manual breakpoint but some how it does not stop there.
Try placing a breakpoint at the bottom of the mocha library per this issue. For some reason that allows debugger statements in your modules to pause the node debugger.
However it doesn't seem to stop at debugger statements in the spec itself. I have a SO question highlighting that problem.
I was on the latest node version, using the node-debug command (to launch node-inspector and having the same issues you were. Here's what I'm rolling with currently:
Using the following versions:
node: 0.11.13 (I downgraded from latest) <-- I specifically had to use this one
mocha: 2.2.1 <-- might work with any
node-inspector: 0.9.2 <-- might work with any
Start your tests using the following command:
node-debug _mocha test/unit-tests.js
Navigate to your test file and start putting in breakpoints, then hit run. I usually put one up by the 'requires' of my test file, and several within my 'it' functions.
Hope that helps, and that one day this kind of thing will just work :P
Got the idea to downgrade node from here:
https://www.bountysource.com/issues/7978672-script-is-resumed-as-soon-as-node-inspector-is-loaded
And the command from here:
https://github.com/node-inspector/node-inspector#how-do-i-debug-mocha-unit-tests