Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open url given by stdout from node --inspect command

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.

like image 377
Alexander Mills Avatar asked Mar 02 '17 23:03

Alexander Mills


2 Answers

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))
like image 70
amdn Avatar answered Oct 29 '22 05:10

amdn


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

enter image description here

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.

like image 37
manishg Avatar answered Oct 29 '22 06:10

manishg