


Detailed explanation of how to debug Node.js programs more simply and effectively in vscode! !
This article will introduce to you a super simple method to use vscode to debug Node.js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Recommended learning: "vscode tutorial", "nodejs tutorial"
Let's Let's face it... debugging Node.js has always been a pain in the ass.
Getting to the pain points of debugging Node.js
If you have ever been lucky enough to write code for a Node.js project, Well you know what I'm talking about when I say debugging it to find what's going wrong isn't the easiest thing.
Unlike JavaScript in the browser, and unlike Java with a powerful IDE like IntelliJ, you cannot set breakpoints everywhere, refresh the page or restart the compiler, nor can you slowly review the code and inspect objects , evaluate functions, find variations or missing variables, etc. It's just too bad that you can't do that.
But Node.js can also be debugged, it just requires more effort. Let's discuss these options in detail and I'll show you the simplest debugging method I've encountered in my development experience.
Some alternative ways to debug Node.js
There are some ways to debug problematic Node.js programs. I've listed these methods (with detailed links) below. If you are interested, you can check it out.
-
Console.log()
— If you’ve ever written JavaScript code, this trusty standby really needs no further explanation. It's built into Node.js and prints in the terminal, just like it's built into JavaScript and prints in the browser console.
In Java language, it is System.out.println()
. In Python, it is print()
. You see what I mean. This is the easiest to implement, and the fastest way to "dirty" clean code with extra lines of information - but it can (sometimes) also help you find and fix bugs.
-
Node.js Documentation
—-inspect
— The Node.js documentation writers themselves understand that debugging is not easy, so they do some Handy reference to help people get started debugging.
This is useful, but honestly, it's not the easiest to decipher unless you've been writing programs for a while. They quickly got into the trap of UUIDs, WebSockets, and security vulnerabilities, and I started to feel overwhelmed. I thought to myself: There must be a less complicated way to do this.
- Chrome DevTools — Paul Irish wrote a blog post about using Chrome Developer Tools to debug Node.js in 2016 (and in 2018 renew). It looks pretty simple and is a big improvement for debugging.
After half an hour and I still haven't successfully connected the DevTools window to my simple Node program, I'm not so sure anymore. Maybe I just can't follow the instructions, but Chrome DevTools seems to make debugging more complicated than it should be.
- JetBrains — JetBrains is one of my favorite software development companies and one of the developers of IntelliJ and WebStorm. Their tools have a fantastic plugin ecosystem, and until recently, they were my go-to IDE.
With such a professional user base, there are many useful articles for debugging Node, but similar to the Node documentation and Chrome DevTools options, it's not easy. You have to create a debug configuration, attach a running process, and do a lot of configuration in the preferences before WebStorm is ready.
- Visual Studio Code — This is my new gold standard for Node debugging. I never thought I'd say this, but I'm totally invested in VS Code, and every new feature release the team makes makes me love this IDE even more.
VS Code does what all other options in Debugging Node.js fail to do, which makes it foolproof. If you want to get more advanced with your debugging, that's certainly possible, but they've broken it down simple enough that anyone can get up and running quickly, regardless of your proficiency with IDEs, Node, and programming. This is awesome.
Configuring VS Code to debug Node.js
Preferences > Settings and enter
node debug in the search box. There should be an extension called
Node debug under the
Extensions tab. Click the first box here:
Debug > Node: Auto Attach, and then set the drop-down box option to on. You are now almost done with the configuration. Yes, it's quite simple.
node --inspect in the terminal. Now look, something magical happened...
VS Code ongoing code debugging
If you need a Node .js project to test it, you can download my repo. It's meant to test different forms of transferring large amounts of data using Node, but it works great for this demo. When you hit theEnter key, the bottom of your VS Code terminal will turn orange, indicating that you are in debug mode, and your console will print something similar to
Debugger Information about Attached.
console in the debug console at the bottom of VS Code .log, the yellow highlight will also move with it.
Summary
Debugging Node.js does not need to be as cumbersome as it used to be, nor does it need to include more than 500console.log## in the code base # to find out the location of the bug. Visual Studio Code's
setting makes this a thing of the past, and I'm very grateful for that. In a few weeks I will be writing some articles about end-to-end testing using Puppeteer and headless Chrome, or resetting passwords in the MERN app using Nodemailer, so follow me so you don’t miss out.
Thanks for reading, I hope this article can help you understand how to debug Node.js programs more easily and effectively with the help of VS Code. Thank you so much for the applause and for sharing my article!
Original address: https://itnext.io/the-absolute-easiest-way-to-debug-node-js-with-vscode-2e02ef5b1badProgramming VideoOriginal author: Paige Niedringhaus
Translation permanent link: https://github.com/xitu/gold-miner/blob/master/TODO1/the-absolute-easiest-way-to-debug-node-js-with- vscode.md
Translator: iceytea
For more programming-related knowledge, please visit:
The above is the detailed content of Detailed explanation of how to debug Node.js programs more simply and effectively in vscode! !. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Configuring the Ruby development environment in VSCode requires the following steps: 1. Install Ruby: Download and install from the official website or using RubyInstaller. 2. Install the plug-in: Install CodeRunner and Ruby plug-ins in VSCode. 3. Set up the debugging environment: Install the DebuggerforRuby plug-in and create a launch.json file in the .vscode folder for configuration. This way, you can write, run, and debug Ruby code efficiently in VSCode.

In VSCode, you can use Git for code version fallback. 1. Use gitreset--hardHEAD~1 to fall back to the previous version. 2. Use gitreset--hard to fall back to a specific commit. 3. Use gitrevert to safely fall back without changing history.

Configuring VSCode to synchronize code with GitHub can improve development efficiency and team collaboration. First, install the "GitHubPullRequestsandIssues" and "GitLens" plugins; second, configure the GitHub account; then clone or create a repository; finally, submit and push the code to GitHub.

The steps to manually install the plug-in package in VSCode are: 1. Download the .vsix file of the plug-in; 2. Open VSCode and press Ctrl Shift P (Windows/Linux) or Cmd Shift P (Mac) to call up the command panel; 3. Enter and select Extensions:InstallfromVSIX..., then select .vsix file and install. Manually installing plug-ins provides a flexible way to install, especially when the network is restricted or the plug-in market is unavailable, but attention needs to be paid to file security and possible dependencies.

Best practices for writing JavaScript code in VSCode include: 1) Install Prettier, ESLint, and JavaScript (ES6) codesnippets extensions, 2) Configure launch.json files for debugging, and 3) Use modern JavaScript features and optimization loops to improve performance. With these settings and tricks, you can develop JavaScript code more efficiently in VSCode.

VSCode solves the problems of multilingual project coding and garbled code including: 1. Ensure that the file is saved with correct encoding and use the "redetection encoding" function; 2. Set the file encoding to UTF-8 and automatically detect the encoding; 3. Control whether to add BOM; 4. Use the "EncodingConverter" plug-in to convert encoding; 5. Use the multiple workspace functions to set encoding for different sub-projects; 6. Optimize performance and ignore unnecessary file monitoring. Through these steps, the coding problem of multilingual projects can be effectively dealt with.

I have a lot of experience in participating in VSCode offline technology exchange activities, and my main gains include sharing of plug-in development, practical demonstrations and communication with other developers. 1. Sharing of plug-in development: I learned how to use VSCode's plug-in API to improve development efficiency, such as automatic formatting and static analysis plug-ins. 2. Practical demonstration: I learned how to use VSCode for remote development and realized its flexibility and scalability. 3. Communicate with developers: I have obtained skills to optimize VSCode startup speed, such as reducing the number of plug-ins loaded at startup and managing the plug-in loading order. In short, this event has benefited me a lot and I highly recommend those who are interested in VSCode to participate.

Create and manage multiple project workspaces in VSCode through the following steps: 1. Click the "Manage" button in the lower left corner, select "New Workspace", and decide the save location. 2. Give the workspace a meaningful name, such as "WebDev" or "Backend". 3. Switch the project in Explorer. 4. Use the .code-workspace file to configure multiple projects and settings. 5. Pay attention to version control and dependency management to ensure that each project has .gitignore and package.json files. 6. Clean useless files regularly and consider using remote development skills
