WebStorm remote debug using typescript... howto? - node.js

I am currently debugging pure js Nodejs apps remotely over SSL using port mapping.
On the Linux server side I run balance -df 8585 127.0.0.1:5858 to port map 8585 to 5858 and it works great with remote debugging and WebStorm.
so on server I run node --debug-brk --nolazy ./someFile.js
and put a breakpoint on Windows in WebStorm and all is great!
Now I would like to remote debug TypeScript files on server side instead of JS.
Is there any tutorial on that? Do I need to compiles to JS file every time on server side and where should I keep the sourceMap on Windows side where I put the breakpoints or on server side? How about the tsconfig?

Related

Node js question about debugging - Using Koa for Cadence-web

I started the debugger using :
$ node --inspect ../cadence-web/server/routes.js
Debugger listening on ws://127.0.0.1:9229/e470a53c-480b-497e-8494-5f1ea62f3b38
Now - I inspect chrome: The code is successfully opened in the debugger.
Please see link below
I open dedicated devTools for Node
Please see link below
I go to localhost:9229: The site cannot be reached.
Any help will be appreciated.
What am I doing incorrectly. Why is the code not loaded on localhost: 9229
The port mentioned in the startup is the Debugger port where the Debugger (Web Tools or VSCode etc.) listens to node. It is not the port your web server is using. That port hasn't changed from its setting in code. Check your source, it should be there, something like 3000 or 8080, depending on your framework and settings

WebSockets request was expected error when using --inspect-brk option

When I run nodemon dist/server/app.js it works on default port 3000 and I'm able to reach my API. But if I run
nodemon --inspect-brk=localhost:3000 dist/server/app.js
I got error
WebSockets request was expected
What's wrong?
You can't run your web server and the debugger on the same port. They are each separate servers (the debugger is a server built into the node.js runtime).
So, you can either remove the port and host designation from the --inspect-brk option and just let it use the defaults (which is all I ever use) or you can select a different port for the debugger that doesn't conflict with your web server or anything else running on that host.

Remotely debugging my node app that is hosted on AWS

I would like to connect to my node server running in debug mode on AWS (node --debug app.js) from my development machine, and be able to debug my app remotely.
Two questions:
Can I do this with node-inspector? I wish I could, but node-inspector fails to install on my AWS instance.
Any alternatives that will allow me to do this?
And with the help of tepez's answer, the following worked for me (Node Inspector v0.12.2):
On my machine:
ssh -L 8080:127.0.0.1:8080 <username>#<host> -N
On the remote server:
node-debug --cli <appname>
And enter the following address in the browser:
127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858
Forward remote debugger port with ssh from your dev machine
ssh -L 5858:127.0.0.1:5858 ubuntu#some.ec2.host.com
And now you can start node-inspector as if the debugger is running locally.
Allow me to present an alternative using node --inspect. I had the same need, although in a Windows environment, I believe this should work for you.
Remote machine (tested with Node 6.10.2, Windows Server 2012)
node --inspect=0.0.0.0:9229 <appname>.js
Local Machine (tested with Win 10, Chrome 60.0.3112.90)
In Chrome DevTools - Click the vertical ellipsis menu in top right:
Go to: More Tools -> Remote Devices
Under Network targets Click Add address
Enter <remote-ip>:9229
Once you enter address and remote target is connected you'll see Node.JS Icon on Top Left of DevTools
Click NodeJS Logo to launch DevTools Node Debugger
Screenshots of Steps 1,3,4 below.
One more option to use 0.0.0.0 to listen to request from outside:
node-debug --web-host=0.0.0.0 --cli app.js
and visit this address to debug:
http://<the-domain>:8080/?port=5858
it would be better if HTTP/2 is available since there are lots of small files.
You can use node --inspect too in your remote machine.
Start your node with node --inspect myapp.js
Then locally ssh -L 9229:127.0.0.1:9229 myuser#myserver -N
Search for an string like this at the log head and copy it
chrome-devtools://devtools/remote/serve_file/#60cd6e859b9ff284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/bef2ae68
Paste it in your chrome browser
Enjoy :)
If you are using pm2 just add this on your ecosystem.js
"apps": [{
"name": "myapp",
"script": "index.js",
"node_args": ["--inspect"],
...
Also you can specify --web-host argument.
node-inspector --web-host host.amazonaws.com
This is what worked for me:
Start node-inspector on server.
Start debugee on remote server with --debug flag.
Note the port that the debugger listens on, i.e. Debugger listening on port DEBUG_PORT message.
Create an ssh tunnel for port 8080, not the DEBUG_PORT as Andrey Sidorov's answer suggests.
Open SERVER_API:8080/debug?ws=127.0.0.1:8080&port=DEBUG_PORT in browser an voilĂ .
Node Inspector v0.10.1

Debugging node.js apps in WebStorm 7

I am having a strange issue debugging node.js apps in WebStorm 7.
The first time I start the debugger I get the application running:
/usr/bin/node --debug-brk=34041 app.js
debugger listening on port 34041
Current Environment: development
Express server listening on port 3000
But, after the first time (or if I restart the debugger), I get the debugger running but not the app, like below:
/usr/bin/node --debug-brk=42140 app.js
debugger listening on port 42140
or
/usr/bin/node --debug-brk=51341 app.js
debugger listening on port 51341
I need to restart the WebStorm itself in order to debug the app again.
Is it a bug in WebStorm?
Is it a bug in Node.js?
Is there a way to make it work properly?
After contacting the support, I found that it was a bug in the WebStorm, already fixed in version 7.0.1.

How do you debug a Node.js server running with Chrome/WebKit as the remote debugger?

If you have your Node running
node --debug server.js
This gives me a port number xxxx, should I use this port number when starting Chrome?
Do you remote debug into it from Google\ Chrome --remote-debugging-port=xxxx?
Or is the 9222 a magic port, as it is mentioned all over.
Am I on the right track, trying to start Chrome with --remote-debugger into the Node.js server.js
The node-inspector / --debug are now replaced by inspector
See update below
#now deprecated / see below for update
#install node-inspector
npm install -g node-inspector
#start node-inspector, listen on port 8080 (default)
node-inspector --web-port=8080
#in another terminal session/window:
#while node-inspector is running, start your project in debug mode
node --debug myproject.js
Now you can browse to http://your_server:8080 for a full debug session of myproject.js
If your remote server is not accessible on the remote port because of firewalls or other reasons, you could create an ssh-tunnel to it from port 8080 on your local machine to 'localhost:8080' on the remote server:
ssh -L 8080:localhost:8080 username#remoteserver -N
and keep this running while you use http://localhost:8080 on your local machine to debug your remote nodejs session
Update august 2017
Start node in inspect mode:
node --inspect=0.0.0.0:9229 myproject.js
or if you want the debugger to break at the first line of myproject.js:
node --inspect-brk=0.0.0.0:9229 myproject.js
Then open the following URL in your chrome browser:
chrome://inspect
Click the 'Configure...' button and add the following target:
ip-or-name-of-server-running-node:9229
After you click the 'Done' button, you should see myproject.js under your remote targets. Click the inspect link to start debugging. Unfortunately, the inspect link does not work on Chrome 58 for Ubuntu. It works fine on Chrome 60 for Windows.
Use node-inspector to remotely debug your node application from Chrome that you've started with the --debug option as you've shown.
Recent versions of Node (> v6.3.0) and Chrome now allow you to use the Chrome Developer Tools to debug a Node.JS process without having to install anything else. Just pass --inspect to node:
$ node --inspect script.js
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://SOME-URL-HERE
Just open that URL in Chrome, and you're good to go.
If you need to pause your script immediately after Node starts, you can also pass --debug-brk in the same command.
using
$ vagrant ssh -- -L 5858:127.0.0.1:5858
to ssh connect to VM. also this comment would start a proxy server on port 5858;
you could test using telnet 127.0.0.1 5858 to see if local proxy server started or not.
In VM, you can start node with command
$ node --debug-brk app.js
set up debug configuration in web storm.
when you start debug in web storm, node.js server in VM will start in a few seconds.
PS: there is no need to touch the vagrant file.
Reference: Connecting WebStorm to a remote node.js debugging session.

Resources