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
2>&1
tells the shell to redirect stderr to stdout.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))
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).
npm install -g node-inspector
node-debug app.js
where app.js
is your js file. you can do node-debug bin/www
in your case
Here is a gif for you to show what it does
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With