Open url given by stdout from node --inspect command - node.js

Alright here's a fun one, we get the following stdout:
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://devtools/remote/serve_file/#60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/a7a2a58d-dfd1-4cc6-875c-47da78adeb1b
when we run a command like so:
node --inspect --debug-brk bin/www.js
I was thinking of creating a bash script like this:
#!/usr/bin/env bash
NODE_DEBUG_OUTPUT=$(node --inspect --debug-brk bin/www.js)
open -a "/Applications/Google Chrome.app"/ getUrl(${NODE_DEBUG_OUTPUT})
but here's where my bash skills end - how can I implement the getUrl function so that I can get the url from the output?
I am pretty certain that if we pass a url to the open function in bash, it will open a browser, in this case though it has to be Google Chrome browser.

Here's a bash command to extract the url
url=$(grep 'chrome-devtools://' <(node --inspect bin/www.js $# 2>&1))
Explanation
The 2>&1 tells the shell to redirect stderr to stdout.
The node command is run within <( node ), this is called process substitution and behaves like a read-only file with the contents being the stdout of the command (node in this case)
Other Issues
There is a bug, filed recently Chrome DevTools not able to connect to Node inspect since 7.5.0
Version 7.4.0 is reported to work
To open a chrome-devtools url from the command line requires some AppleScript, fortunately someone's already done that:
chrome-node-devtools-refresh.sh
So the command would be something like this:
chrome-node-devtools-refresh.sh $(grep 'chrome-devtools://' \
<(node --inspect bin/www.js $# 2>&1))

Alternative
So I am probably not answering your question but I want to give this as a simpler solution to the higher problem you are trying to resolve.
There is a npm package called node-inspector. This package provides the same functionality as you need from chrome dev-tools. Here is the description from their github page :
Node Inspector is a debugger interface for Node.js applications that
uses the Blink Developer Tools (formerly WebKit Web Inspector).
How do you Install?
npm install -g node-inspector
How do you use?
node-debug app.js
where app.js is your js file. you can do node-debug bin/www in your case
What does it do?
Here is a gif for you to show what it does
Summary
The node-inspector package can help you achieve the functionality you desire. i.e. launch chrome to debug your js code. I feel this is a easier option. Also you can read more about this to explore additional use cases if you have
From the developers of this package:
The node-debug command will load Node Inspector in your default
browser.
NOTE: Node Inspector works in Chrome and Opera only. You have to
re-open the inspector page in one of those browsers if another browser
is your default web browser (e.g. Safari or Internet Explorer). Node
Inspector works almost exactly as the Chrome Developer Tools. Read the
excellent DevTools overview to get started.

This is really close! The only thing that's not working is that chrome is not actually opening a url, it just opens to google.com
export PATH=$PATH:/Applications/Google\ Chrome.app/Contents/MacOS/
(node --inspect --debug-brk bin/www.js $# &> debug-server.log) &
sleep 1; // wait for the stdout to get written to file...
LINE=$(grep -n "chrome-devtools" debug-server.log | awk '{print $2}')
echo " => Supposed chrome-devtools link => '$LINE'";
Google\ Chrome "${LINE}"
anybody know why the "Google\ Chrome" command would not open the url provided as the first argument?

Related

Command line documentation viewer for node js modules

Is there a way to display the documentation of installed NodeJS packages in a console window (as a command line tool)? Often - in case of editing on a server - it is easier to view the docs of some specific stuff right in the command window, then to start a browser.
Such as npm docs fs or npm docs express would be perfect, but these try to start a browser and display documentation there instead of displaying README.md directly in the window.
Something similar to Perl's perldoc would be a perfect solution.
Here's what I do when I'm in a hurry:
# nodedoc
# USAGE: nodedoc <name of module>
# e.g. nodedoc http
curl https://raw.githubusercontent.com/nodejs/node/master/doc/api/$1.md | pandoc --from markdown --to plain | less

Node Inspector Console - Can't Execute Command, Goes to Newline Instead

I am running node inspector and trying to run commands in the console.
However, I cannot run them. Instead of running, hitting enter goes to a new line.
Using any other console in chrome works fine, just not Inspector's.
Have tried restarting node and inspector. Any ideas how to fix this?
It seems like Chrome v54, v56 are having this issue for both mac and windows. The easiest solutions so far is downgrading your chrome to v51, and I think some one is working on a PR to fix this.
You can download V51 from here
http://google-chrome.en.uptodown.com/mac
===========================================================================
Another work around is to use command
node --inspect --debug-brk index.js
After running that, goto chrome, open
chrome://inspect you will see a page like this
Find the target and click the inspect, a debugger will be attached automatically and everything works from there. Just like it used to be in node-inspect, and the console works!
read this for more information
http://www.mattzeunert.com/2016/06/01/node-v8-inspector-inspect.html

node.js command prompt not initialised as expected

I installed the .msi file from nodejs website and installed it.
Now, when I run nodejs.exe, I do get a command prompt, but it shows a blinking > by default, instead of C:/>
It looks somewhat like this:
What to do?
This is called the REPL. You can enter statements for Node to execute and get realtime feedback. Ctrl+C twice will get you back to the command prompt.
I recommend checking out the answers to How do I get started with Node.js for learning more about Node.js and how it works. Typically you provide a file with your Javascript for Node to execute:
node app.js
or you can leave the .js off:
node app

Is it possible to break into interactive mode in Node.js?

If you run node you get into interactive mode, you can inspect, execute functions etc. But once you run a function from interactive mode or from the OS shell, can you pause execution and go to interactive mode deliberately?
In browser you can use debugger keyword, but in Node.js debugger does nothing for me.
When my software fails and I want to quickly figure it out, I add a debugger instruction where I want the debugger to break and do:
node debug [script I want to debug]
Note that it is node debug, not node --debug. The latter also starts a debugger but it is one that waits on a port for some UI to connect.
Doing the above will cause the debugger to stop before executing your software. Type c to let it continue. It will next stop where you put your debugger statement. If you want to evaluate things dynamically you can use the repl command.
If you do it this way, you don't need any extra tools. The documentation is here.
try the node-inspector, you can debug your app with chrome developer tool
1.install and start node-inspector
npm install -g node-inspector
node-inspector &
2.start your app with debug parameter (if you want to break in the first line, replace the parameter to --debug-brk
node --debug server.js
3.open debug link in Chrome
open http://127.0.0.1:8080/debug?port=5858

Does any web browser allow writing to stdout?

I plan to write a Hashify command line client, and I'd like to confirm that simply writing to stdout is not an option before getting creative.
(Ideally the command will behave like TextMate's mate, in this case opening hashify.me in a browser and waiting to be fed input.)
WGet (simple http fetching tool) supports redirecting a file to STDOUT with the -qO - optioncalled like wget -qO - http://url/.
Lynx (full CLI browser) supports dumping to STDOUT with the -dump or -crawl option.
You could have a look at Google Native Client, available in the dev channel version of Google Chrome.
When run with the native client flags, it allows you to print to the terminal as stated here. Look into the documentation here and here for details.
Getting it to work on windows could be a pain, but it (supposedly) works great on linux. I'm trying it out right now.

Resources