ndb
ndb copied to clipboard
Attach from outside (standalone) ndb
Is there a way to leave ndb
running and have scripts executed with node --inspect
be picked up by it?
My current workflow is I open a standalone Node inspector from within Chrome, and leave it running. As I run my scripts that debugger attaches to them.
I can't figure out how to get a standalone ndb
to attach to my script launched with node --inspect
from an external terminal. Yes I know I can click specific npm scripts but my workflow is more complicated than clicking a script to run. I use commands to automatically launch build scripts when the filesystem changes, like git ls **/*.ts | entr npm run debug
(which runs node --inspect
).
We may have three options here:
- allow user to set ports that ndb should listen and auto connects as soon as node runs,
- provide a special script that you can run inside your terminal and then ndb auto connects to any node process that runs from that terminal session,
- you can run your debug command from builtin terminal (press escape to open drawer, terminal is next tab to console) and ndb should auto connect to any node process started from terminal.
In 2 and 3 you do not need to pass --inspect as node argument, everything should happen automatically. Option 3 is already available, could your try it?
- Open ndb from your project folder.
- Go to terminal tab.
- Run your magic command
git ls **/*.ts | entr npm run debug
(you may try to remove --inspect if it does not work as is).
It attaches on first run, but changes do not run the app again. Basically entr
(and also nodemon
) fails to do its job, despite working great in my normal terminal. I used a much more simple command to test in case the globbing I used in my example above is zsh-specific.
To avoid getting into the specifics of ndb
's terminal or the specific programs I'm using, what are your thoughts on your options 1 and 2? I'm sure we could get 3 working with the most minimal amount of work, but I don't like 3 very much ;).
Thanks for the quick response!
I will experiment with nodemon to better understand why option 3 fails and I think I will add something like ndb-observer in addition to ndb binary that will force every node in current terminal to be detected by ndb, it is option 2. Thanks for testing!
I have a little experiment that might help with this issue: ndb-inspect.
You install this pkg as dev dependency and use -r ndb-inspect
instead of --inspect
and -r ndb-inspect/brk
instead of --inspect-brk
. ndb (starting 1.0.25) will automatically detect these processes and connect to them. It will work best if your run ndb from the folder with your app source code.
Looking forward for your feedback.
@ak239 this is really cool. Any chance this would work with electron then? Ndb is just a couple steps from replacing our custom puppeteer setup that hooks into a node + electron + electron-remote processes I think, and this is one of them (electron and having it stay stable across restarts are others).
- allow user to set ports that ndb should listen and auto connects as soon as node runs,
Chrome has been unstable for me lately and I've been itching to switch over to something more focused. This quoted step is the biggest blocker for using ndb as a drop-in replacement in my own workflow right now.
Maybe I'm wildly off the mark here, but having this functionality out of the box could help with retention of users that otherwise would dip in to check it out, realize they can't attach to their running process, and move back to full blown chrome where it 'just works'.
We have --inspect replacement for local debugging. Please try ndb-inspect:
- install it as local dev dependency,
- replace any --inspect with -r ndb-inspect and --inspect-brk with -r ndb-inspect/brk
- ideally run ndb from the folder which contains project that you would like to debug.
Would this solution work for you?
That's definitely a huge improvement in terms of convenience, for sure. From a more general perspective, though, I think the fact that it necessitates changing package.json/launch.json/etc introduces that little bit of friction that may hold people back that would otherwise experiment with it more. Again I could be clueless here but thought it might be helpful to add my view.
Some updates:
- ndb as dependency does not contain Chromium binary any more, so it is not a huge dev dependency,
- it is possible to use global ndb installation, you need to locate your global node_modules folder first and then add it to NODE_PATH environment variable by running something like
export NODE_PATH=$NODE_PATH:/path/to/global/node_modules
, after you can use-r ndb/inspect
as--inspect
replacement and-r ndb/inspect-brk
as--inspect-brk
replacement.
[email protected]
can connect to a node app running in any terminal. Press escape to get console drawer, select terminal
tab there and copy paster couple exports to your terminal.
Some updates:
* ndb as dependency does not contain Chromium binary any more, so it is not a huge dev dependency, * it is possible to use global ndb installation, you need to locate your global node_modules folder first and then add it to NODE_PATH environment variable by running something like `export NODE_PATH=$NODE_PATH:/path/to/global/node_modules`, after you can use `-r ndb/inspect` as `--inspect` replacement and `-r ndb/inspect-brk` as `--inspect-brk` replacement.
This is how I work with a single ndb window, reusing it:
- in the directory where my code is, I run
ndb .
- I open another terminal, go to the same directory and I run
node -r ndb/inspect-brk app.js
- when the program ends, the ndb window stays up, and the second terminal returns to the prompt.
- if I want to run the program again thru ndb, I repeat step 2, and it loads on the existing ndb window.
This works fine with ndb 1.0.44.
But I just updated to 1.1.3 and I'm getting the error: Error: Cannot find module 'ndb/inspect-brk'
I checked the environment vars, and NODE_PATH is pointing to node_modules where ndb is located.
Is this borken or is there perhaps a better way to do this nowadays? Maybe I'm using an outdated method?
I realise that I can just run ndb .
and do a "Run this script" when I need to run the code, is there a keyboard shortcut for that? :)
It sounds to me like you would like to run app.js
from time to time using ndb. You can run ndb app.js
- it will start ndb and run app.js. Or you can run ndb .
and use run this script to run app.js
. In both cases to restart app again later you can use Cmd
/Ctrl
+ R
.
Current workaround:
- open console drawer in DevTools by pressing Escape
- select terminal tab, it dumps couple exports, run them in separate terminal tab
- ndb window will auto detect any node process running inside separate terminal tab
I am going to bring back inspect-brk
support but it requires some additional work.
Thank you for working on bringing back inspect-brk
, it's very much appreciated.
In the meantime, once a script is running with "Run this script" or Ctrl+R, is there a way to interrupt execution and just break it widow closing the ndb window? Similar to what a Ctrl+C would do on the terminal.
There is no shortcut yet, we can add it later, I need to find good one since Ctrl+C will not work. You can do it from UI:
Thank you! Kill works.
I'm sorry, I didn't mean to ask for a shortcut to be added, I think it's sufficient with kill as it is.