Nodejs waiting for 127.0.0.1:9229 to be free - node.js

When I try to use debugger in node to open debugger, I get an error 'Timeout (2000) waiting for 127.0.0.1:9229 to be free'. How can I resolve this and run the debugger correctly ?
function foo() {
var a = 5;
debugger
console.log(a)
}
foo()
I have already tried changing the port using node inspect --port=9230 app.js and it doesn't work.

Try this:
node --inspect-brk app.js
replace app.js with your file name that you want to run, and you can insert your additional command alongside with this line.

I had the same problem, it took me hours to figure it out...
Run the following command:
node inspect --port=9228 file.js

I had the same issue by using VS Code. VS code document helps. Replace program.js with your js file name.
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
if the program should not start running but must wait for the debugger to attach:
node --inspect-brk program.js

Use node --inspect-brk {js file name} instead. It will work.
--inspect-brk=[host:port]
Enable inspector agent
2.Bind to address or hostname host (default:127.0.0.1)
3.Listen on port port (default: 9229)
4.Break before user code starts

I think the issue here is the command you give to node, it should be node --inspect..
You are missing the -- in front of inspect :)

Install
npm install --global node-inspect
Then
node-inspect script.js
Ref: https://www.npmjs.com/package/node-inspect

Related

Passing JSON File to PM2 as a node argument

I have a NodeJS app which needs to start the server with the following parameter: start server.js --config=config.json. Then in the server.js I use NodeUtils.getArgs() and
JSON.parse() to get all the parameters of the config.json file. This works well.
Now, I want to start the server with PM2, but I am not being able.
If I try with pm2 start server.js --node-args"--config=config.json" I get a node: bad option: --config=config.json. I tried with a lot of options but none of them works.
How can I do it? Thanks
EDIT: After starting pm2, if it gets an error, you must pm2 delete all.
You can pass your own arguments after --, so you can do this:
pm2 start server.js -- -config=config.json

docker node app always crashes on file change using nodemon

I am using the latest version of docker and the latest node image. I have a gulpfile that starts a nodemon process. I am using the --inspect flag to indicate I want to use the experimental chrome dev tool debugger. But when I make a file change it nodemon picks it up and restarts the process but crashes.
Here is my gulp task:
gulp.task('start:dev', done => {
let started = false;
nodemon({
script: path.join(__dirname, 'index.js'),
ext: 'js json',
nodeArgs: ['--inspect=0.0.0.0:9229'],
watch: path.join(__dirname, 'express'),
legacyWatch: true
})
.on('start', () => {
// to avoid nodemon being started multiple times
if (!started) {
setTimeout(() => done(), 100);
started = true;
}
});
});
And here is the error:
Starting inspector on 0.0.0.0:9229 failed: address already in use
If I change the --inspect flag to be --debug it works like a charm.
I am guessing is that the restart process is too fast for the --inspect to release its port. If I make another file change it does work and restarts normally. Probably since it had time to release the port.
I have tried using a delay on nodemon but I'd rather not. I would like quick restarts. And I have tried using to events, like, restart and exit, to wait for a few seconds and then restart the whole gulp task. But that was temperamental and again I want quick restarts without having to hack together something.
Right now I just switched back to --debug but that is deprecated in the latest V8. They are recommending to use --inspect.
Maybe the only way is to lock down my version of node?
Any suggestions?
There is an open issue addressing this problem.
The easiest workaround I found so far was using "signal": "SIGINT" in my nodemon.json thanks to this comment.
Just kill inspector and start inspector again
here is our team's solution in our package.json.
You had better kill inspector process and then restart inspector
"inspect": "kill-port --port 9229 && node --inspect=0.0.0.0:9229 build/startup.js",
"start_watch_inspect": `nodemon --delay 80ms --watch build/ build/startup.js --exec 'npm run inspect'`
Seems like this is related to:
https://github.com/remy/nodemon/issues/1492
My workaround is to run this before each restart: (in a makefile, gulp file etc...)
lsof -i -n | grep 9229 | awk '{print $2}' | xargs kill
** If put inside a Makefile remember to replace $ with $$ **

How can I use readline-sync npm repository with WebStorm?

I wrote the following code using 'readline-sync' dependency.
var readlineSync = require('readline-sync');
function main() {
printMenu();
var userName = readlineSync.question('Please enter your choice:');
console.log(userName);
}
main();
I ran this code from WebStorm trying to use the WebStorm console window.
I got the error:
Error: The current environment doesn't support interactive reading
from TTY. stty: when specifying an output style, modes may not be set
When I run it from linux terminal the code works with no error. I understand from the error message that 'readline-sync' cannot work from WebStorm console. Do you have any idea how to solve it?
I found out the answer.
type in WebStorm terminal:
$ node --debug-brk
Web storm will give you the port number the debugger is listening to. On my machine it was 5858. Then press 'Ctrl+C'.
Create a new debugging configuration as the following one:
In WebStorm terminal type again: "$ node --debug-brk main.js "
put a breakpoint somewhere.
Click the debugging icon
Happy Debugging!

How to debug custom node server with brunch.io?

I am using brunch.io with a custom server app.coffee.
This is what my brunch-config.coffee entry looks like:
server:
path: 'app.coffee'
port: 3333
base: '/'
I want to use node's default debugger, but when I type debugger anywhere in my app.coffee, script execution doesn't stop. My debugger statement is simply ignored.
How can I make brunch run my server so that debugger statements are not ignored but pause script execution?
Brunch version: 1.7.18
Coffee version: 1.7.1
Node version: 0.10.30
Thanks for your time!
To use the debugger, it would be best to run brunch watch and your node app in separate terminal instances.
Just run brunch watch without the -s/--server option, and then separately run something like:
coffee --nodejs --debug app.coffee

Node inspector - debug-brk not breaking on first line

I've installed node-inspector, and have started it with node.
When I try to start debugging mode with --debug-brk, it still errors out (it's this error I'm trying to debug)
It does not break on the first line...
$ node --debug-brk app.js
debugger listening on port 5858
/base_controller.js:59
files.forEach(function(file) {
^
TypeError: Cannot call method 'forEach' of undefined
at Object.oncomplete (/base_controller.js:59:9)
installed node-inspector : https://github.com/node-inspector/node-inspector
try below steps:
first: node --debug-brk app.js
second:node-inspector
then:
open a new winow in chrome,open url:
http://localhost:8080/debug?port=5858
you can debug your code now !
Maybe you are not running the latest version (it happened to me). Please do
$ node
> process.version
That should give you the last version. You can update here: http://nodejs.org/

Resources