简体   繁体   中英

VS Code Node.js macOS debugging not working - I don't even know how to state the problem

Newbie question here, it seems... I'm losing my mind. Why is this so hard? I've been programming other languages for years, but I'm relatively new to node and JS. I've read and watched as much as I can find official and unofficial on the web about this and it's either years old or significantly different from my situation, so not very helpful.

I'd really, really appreciate anyone who can understand my (pretty simple, I would have thought!?) specific case and help me get this set up to just work. Possible?

  • macOS 10.15.6 Catalina.
  • As I understand it, node comes with the OS. It's Node.js 12.8.1
  • VS Code 1.48.2

I create a new file, name it abcd.js. VS Code figures out it's javascript. I add some code - read a file of JSON from the disk, make some changes, console.log some stuff (appears under the Output tab). I run it.* All good and works.

Now... Run menu > "Start Debugging". Or, There's a Run/Debug icon in the sidebar that shows a "Run and Debug" button which I click on. Both of those ask me for an environment - I choose Node.js, and the Debug console outputs this:

/usr/local/bin/node path/to/my/file.js
Debugger listening on ws://127.0.0.1:56430/30b0c2aa-278b-4540-94c4-825d30b94f57
For help see https://nodejs.org/en/docs/inspector

And that's it. I have breakpoints, but it doesn't stop on them. Something happens (too quick to see what it is). I don't think it's actually running my code because nothing new shows up under the Output tab.

There's also a note in that sidebar saying "To customize Run and Debug create a launch.json file." I've experimented with that as much as I can, but nothing I do seems to help.

I've been programming for years. I'm used to other languages and IDEs (eg. C, Java, AppleScript, Swift, others), where you open the IDE, write some code, hit run (or debug), it may or may not build a target (depending on the language and environment), but then it runs the code (opens the target app, or runs in the IDE, and allows you to step through the code, breakpoints, etc. in the IDE if debugging). Is JS/node programming radically different from this somehow?

*One theory:

I'm not doing this through any terminal or any browser. Maybe that's the problem...? At some point I installed a "Code Runner" plugin that I believe is what is running my code. It was a while ago, but if I recall correctly maybe I turned to that because I was looking for the experience I've described above, and this plugin gave me that where the "normal" node setup process didn't? Perhaps my problem is that that plugin simplifies RUNNING js scripts so much that my issues with DEBUGGING exist because that plugin allows me to bypass other usual complications with running JS/Node scripts (but doesn't have any debugging functionality of its own)?

My thanks to @ippi for his/her comment with a gif video. Not sure if the following really answers my question, but I'm posting it in case it helps anyone else.

I took some time to clean up my Mac a little, and I completely uninstall VSCode, all its extensions and supporting files and started again.

I re-installed Code from scratch, didn't add any extensions, and then tried what @ippi's video showed.

It still didn't work - I got more or less the same output as in my post, and I'm not sure if it was running the code or not.

I had an urgent deadline to meet so didn't come back to it till now - two days later - and now, it's working.

A couple of things changed in that time though I really don't know what fixed this, if it was either of those or anything else. The two changes I can remember that I've done the last two days are:

  1. Despite my post saying my node was 12.8.1, sometime in the last couple of days it turned into 8.something. I updated it to the latest v12.8.3, and
  2. to meet that deadline I used node inspect myfile.js in Terminal for a bit.

The inspect (mostly) worked to meet my deadline, but still a hassle compared to what's shown in @ippi's video. I definitely wanted to get it working that way.

So… updating node fixed it? Running it in the terminal for a bit fixed it? Something else? I'm at a loss to know what did it. But it's working now.

Thanks again to @ippi for that. It pointed me in a good direction at least - to clean up my install a bit, if nothing else.

Maybe this will help someone else sometime in the future.


It seems the ultimate answer is:

It should just work out of the box (possibly subject to updating node to its latest). If it doesn't then:

  • The issue most likely isn't that you have to do any fancy configuring or whatnot to make it work.

  • Instead, most likely something is wrong with your installation and/or OS and you should explore that.


@ippi's video attached in case his/her link ever breaks:

@ippi 的 gif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM