Can't get node-debug to work - javascript

This feels really silly, but I can't get node inspector / node-debug to work.
The instructions say to do npm install then to run node-debug web.js. So I did that. Now I have a lovely browser window open showing me my code with breakpoints... and no idea which url to use to actually access the code.
The inspector is at http://localhost:8080/debug?port=5858 and the terminal says:
> node-debug web.js
debugger listening on port 5858
Node Inspector is now available from http://localhost:8080/debug?port=5858
Debugging `web.js`
...
I've tried hitting up localhost:5000 (which is my express.js port) but that either fails if I don't have a separate node web.js instance running, or it succeeds if I have the other one running but doesn't trip any of the breakpoints in the inspector.
When I go to http://localhost:5858/, I get:
Remote debugging session already active
When I go to http://localhost:8080/, I get:
Cannot GET /
(the / path totally works on my server in general.)

By default node-debug starts app in --debug-brk mode.
This stops your app at first line (express not started).
You can use node-debug --no-debug-brk see the node-debug --h for more info.

Agh. Okay, looked at some more questions before I got this posted. Looks like the problem was just that I wasn't running the original instance in debug mode. Nobody had told me I had to, so I just didn't know otherwise!
What's working for me now:
> node debug web
then in a different terminal
> node-debug web.js

Related

Node JS ctrl + C doesn't stop server (after starting server with "npm start")

When I start my server with node app.js in the command line (using Git Bash), I can stop it using ctrl + C.
In my package.json file i got this start-script that allows me to use the command npm start to start the server:
"scripts": {
"start": "node app"
},
When I do this, the server starts as normal:
$ npm start
> nodekb#1.0.0 start C:\Projects\nodekb
> node app.js
Server started on port 3000...
But when i ctrl + C now, the server does not get stopped (the node process still remains in task manager). This means that I get an error when I try to do npm start again, because port 3000 is still being used.
I'm following a tutorial on youtube (video with timestamp), and when this guy ctrl + C and then runs npm start again, it works as normal.
Any ideas why my server process is not stopped when I use ctrl + C?
My app.js file if needed:
var express = require("express");
var path = require("path");
//Init app
var app = express();
//Load View Engine
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
//Home Route
app.get("/", function(req, res) {
res.render("index", {
title: "Hello"
});
});
//Add route
app.get("/articles/add", function (req, res) {
res.render("add_article", {
title: "Add Article"
});
});
//Start server
app.listen(3000, function() {
console.log("Server started on port 3000...");
});
Thanks!
Ctrl + C does not kill the server. The resolution to the issue was using following code snippet in server.js:
process.on('SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(0);
});
This worked for me.
You can also check for other solutions mentioned at Graceful shutdown in NodeJS
I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash.
I encountered this problem in MSYS2 proper, even in latest build (x64 2018-05-31).
Luckily, Git for Windows maintain a customized MSYS2 runtime. They have patches that have not been sent upstream, including a patch that fixes emulation of SIGINT, SIGTERM and SIGKILL.
Discussion: https://github.com/nodejs/node/issues/16103
I was able to make my "MSYS2 proper" platform use Git for Windows' MSYS2 runtime, by following these instructions.
Repeated here for posterity:
Install inside MSYS2 proper
This guide assumes that you want the 64-bit version of Git for Windows.
Git for Windows being based on MSYS2, it's possible to install the git package into an existing MSYS2 installation. That means that if you are already using MSYS2 on your computer, you can use Git for Windows without running the full installer or using the portable version.
Note however that there are some caveats for going this way. Git for Windows created some patches for msys2-runtime that have not been sent upstream. (This had been planned, but it was determined in issue #284 that it would probably not be happening.) This means that you have to install Git for Windows customized msys2-runtime to have a fully working git inside MSYS2.
Here the steps to take:
Open an MSYS2 terminal.
Edit /etc/pacman.conf and just before [mingw32] (line #71 on my machine), add the git-for-windows packages repository:
[git-for-windows]
Server = https://wingit.blob.core.windows.net/x86-64
and optionally also the MINGW-only repository for the opposite architecture (i.e. MINGW32 for 64-bit SDK):
[git-for-windows-mingw32]
Server = https://wingit.blob.core.windows.net/i686
Authorize signing key (this step may have to be repeated occasionally until https://github.com/msys2/msys2/issues/62 is fixed)
curl -L https://raw.githubusercontent.com/git-for-windows/build-extra/master/git-for-windows-keyring/git-for-windows.gpg |
pacman-key --add - &&
pacman-key --lsign-key 1A9F3986
Then synchronize new repository
pacboy update
This updates msys2-runtime and therefore will ask you to close the window (not just exit the pacman process). Don't panic, simply close all currently open MSYS2 shells and MSYS2 programs. Once all are closed, start a new terminal again.
Then synchronize again (updating the non-core part of the packages):
pacboy update
And finally install the Git/cURL packages:
pacboy sync git:x git-doc-html:x git-doc-man:x git-extra: curl:x
Finally, check that everything went well by doing git --version in a MINGW64 shell and it should output something like git version 2.14.1.windows.1 (or newer).
Note: I found that the git-extra package installed by step 7 was quite intrusive (it adds a message "Welcome to the Git for Windows SDK!" to every terminal you open), so I removed it with pacman -R git-extra.
Note 2: I also found that Git for Windows' MSYS2 runtime opens in a different home directory than did MSYS2 proper's. This also means it reads in the wrong bash profile. I fixed this by adding an environment variable to Windows in the Control Panel: HOME=/C/msys64/home/myusername
I use git bash on my Windows machine and have run into this issue in the last month or so.
I still do not know what's causing it but I've found another way to stop it.
Open Task Manager
Go into the Processes tab
Look for node.exe and then press End Process
This has allowed me to stop the server quickly.
I had the same problem working with npm. But finally, I knew it was a problem with git itself.
There was a comment by dscho on GitHub 15 days ago. He said that they're working to fix this problem in the next release. He also shared the exact msys-2.0.dll file that can fix the problem for the people who can't wait.
Personally, I couldn't wait :p. So, I gave it a try, downloaded the file, and throw it in the git folder as he said. And the problem gone! It was awesome!
But please be sure to take a backup before you replace the file.
I also tried to kill it after running express as I used to; using taskkill /im node.exe on the cmd but there was no process to be found.
Check out this issue on GitHub,and search for the name of the file msys-2.0.dll to get to the comment faster.
Sometimes the node process hangs.
Check for the process ID using ps You may want to grep for node and then kill the process using kill -9 [PID]
Use Ctrl+\ to send the SIGQUIT signal. It will close the server.
Reference - https://en.wikipedia.org/wiki/Signal_(IPC)
I was able to fix this by switching to nodemon to run the server.
npm install --save-dev nodemon
package.json:
"scripts": {
"start": "nodemon app"
},
I was trying to get json-server to quit a custom server script, but it always left a child process running on Windows. It seems to be a specific problem running express via npm on Windows. If you run the server directly via the c:>node server.js then it seems to quit correctly.
I was able to debug this issue by checking the ports using TCP View, and realizing that my Node server was running even though I had pressed ctrl-C to stop it. I suggest killing the terminal you are running node from entirely.
Use Ctrl + C, then input: >pm2 stop all
This will stop all server or when you get stack with nodejs.
Inside package.json under scripts I had this line react-scripts start&. Notice it ends with an & which would send the process to the background and ctrl+c will not work. Somehow trying to bring this to the foreground with fg also did not work. Solved the problem by removing the &.
if you use Node.js Exec Extention to run your project from f8,
you can use also f9 to cancel running..
This is more than likely just a problem with your console not accurately sending the command to the process. This is pretty common, especially when using third party consoles like cmdr / conemu.
The solution?
Just hit ctrl+c several times until it closes :P

Node.js, node-inspector, JavaScript, Google Chrome: debug workflow

I start a debug session like this:
$ node-debug -p 8082 try-debug.js 8081
Then I go to the Node Inspector tab in Google Chrome. Unfortunately, I can't figure out how to abort the debug session and/or reload modified source short of restarting manually the node-debug server and reloading the Node Inspector tab.
What is the right way?

Express site will not launch

I have used node/Express framework before and I am trying to launch an old webApp that I have used before. Whenever I type ./bin/www nothing happens.. Usually it would say "listening on port 3000" but absolutely nothing happens. I go to the site and there is no connection. No error messages or anything. Does anyone know why this might be happening? I have done it before on another Ubuntu operating system and I am now just trying to launch it on Xubuntu
I am also setting the debug to ./bin/www by inputting:
DEBUG = ./bin/www
EDIT: Found the problem... Apparently I had to install node-legacy instead

CLI debugger for Mocha tests

I want to be able to drop a debugger statement (or something similar) right into my code and get a REPL when I run my tests so I can interact with my code to debug stuff.
I understand that Node (via V8) supports the debugger statement out of the box, but you have to run your code with certain flags (via node debug or node --debug or node --debug-brk -- I don't really understand the differences).
Mocha has these same flags as well, and when I run mocha debug I indeed get a debugger prompt in my terminal:
< Debugger listening on port 5858
connecting to port 5858... ok
break in node_modules/mocha/bin/_mocha:5
3 */
4
> 5 var program = require('commander')
6 , path = require('path')
7 , fs = require('fs')
debug>
But the breakpoint is in Mocha's code, not my breakpoint. This blog post says you're supposed to type run then continue to get to your breakpoint, but when I enter run it tells me "App is already running..." and when I enter continue it tells me "SyntaxError: Illegal continue statement".
It says "listening on port 5858" -- do I need to open a separate connection to that port from somewhere, like a browser or another terminal session?
I've seen some things about node-inspector, but I like to stay in the terminal as much as possible so I prefer a way to do this without opening a browser.
I'm new to JS, so please ELI5 :)
Also, if it's relevant, I'm actually using io.js, not Node proper
When the node/iojs debugger first starts up and connects it always breaks on the first line of whatever the script was that was passed into the interpreter.
Since you're already running your app, you do not need to type run here, but you can just type c to continue execution until your breakpoint has been it.
run is useful if the program terminates while you're in the debugger -- you can start it up again! (You can also do restart to restart the program within the debugger).

Webstorm debugger for node.js

I'm trying to configure Webstorm for using the node.js' debugger.
I've set the enviroment and everything, the app is running fine with the run button, but with the debugger button it just hangs up writing only:
/usr/bin/node --debug-brk=52006 --debug-brk node.js
debugger listening on port 52006
and it doesn't work or writes anything on the output.
Any idea of what is missing? I've already installed node-inspector and everything.
EDIT:
After sometime that I run the code, I get:
Failed to open socket on port 52708, waiting 1000 ms before retrying
Problem solved with the help of JetBrains. In a few words, I used cluster creating child nodes that weren't connected to the debugger.
Full explanation here: https://intellij-support.jetbrains.com/tickets/13630

Categories

Resources