In the debug nodejs Project time , When used vscode, Project report error , But his debug The content displayed is Show
I set him monkey , If you don't tell me what's wrong, how can I change it , Before, I used the console output method , But this error report makes my console output statements useless Console output method :
I couldn't help it , What does Baidu say for a long time , Disheartened , Look, it feels so annoying , So keep looking for , Just in launch.json Add a sentence to the document **“console”: “integratedTerminal”** It can be realized .
over, So far, there are errors copyright notice Node.js debugging in VS CodeThe Visual Studio Code editor has built-in debugging support for the Node.js runtime and can debug JavaScript, TypeScript, and many other languages that are transpiled into JavaScript. Setting up a project for Node.js debugging is straightforward with VS Code providing appropriate launch configuration defaults and snippets. There are a few ways you can debug your Node.js programs in VS Code:
Auto AttachIf the Auto Attach feature is enabled, the Node debugger automatically attaches to certain Node.js processes that have been launched from VS Code's Integrated Terminal. To enable the feature, either use the Toggle Auto Attach command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) or, if it's already activated, use the Auto Attach Status bar item. There are three modes for auto attach, which you can select in the resulting Quick Pick and via the debug.javascript.autoAttachFilter setting:
After enabling Auto Attach, you'll need to restart your terminal. This can be done by clicking the ⚠ icon in the top right of the terminal, or just creating a new one. Then, the debugger should attach to your program within a second: When auto attach is on, the Additional ConfigurationOther Launch Configuration Properties You can apply other properties normally found in launch.json to auto attach in the debug.javascript.terminalOptions setting. For example, to add node internals to your skipFiles, you could add the following to your user or workspace settings:
Auto Attach Smart Patterns In
JavaScript Debug TerminalIn a similar way to
auto attach, the JavaScript Debug Terminal will automatically debug any Node.js process you run in it. You can create a Debug Terminal by running the Debug: Create JavaScript Debug Terminal command from the Command Palette ( Additional ConfigurationOther Launch Configuration Properties You can apply other properties normally found in launch.json to the debug terminal in the debug.javascript.terminalOptions setting. For example, to add node internals to your skipFiles, you could add the following to your user or workspace settings:
Launch ConfigurationLaunch configs are the traditional way to set up debugging in VS Code, and provide you the most configuration options for running complex applications. In this section we'll go into more detail about configurations and features for more advanced debugging scenarios. You'll find instruction for debugging with source maps, stepping over external code, doing remote debugging, and much more. If you'd like to watch an introductory video, see Getting started with Node.js debugging.
Launch configuration attributesDebugging configurations are stored in a Below is a reference of common The following attributes are supported in launch configurations of type
These attributes are only available for launch configurations of request type
This attribute is only
available for launch configurations of request type
Launch configurations for common scenariosYou can trigger IntelliSense (⌃Space (Windows, Linux Ctrl+Space)) in your You can also bring up the snippets with the Add Configuration... button in the lower right of the These are the available snippets:
Node consoleBy default, Node.js debug sessions launch the target in the internal VS Code Debug Console. Since the Debug Console does not support programs that need to read input from the console, you can enable either an external terminal or use the VS Code Integrated Terminal by setting the If an external terminal is used, you can configure which terminal program to use via the Launch configuration support for 'npm' and other toolsInstead of launching the Node.js program directly with node, you can use 'npm' scripts or other task runner tools directly from a launch configuration:
Let's look at an 'npm' example. If your
the corresponding launch configuration would look like this:
Multi version supportIf you are using 'nvm' (or 'nvm-windows') to manage your Node.js versions, it is possible to specify a
If you are
using 'nvs' to manage your Node.js versions, it is possible to use
Make sure to have those Node.js versions installed that you want to use with the If you omit the minor and patch version and have, for example, Load environment variables from external fileThe VS Code Node debugger supports loading environment variables from a file and passing them to the
Node.js runtime. To use this feature, add an attribute
Any environment variable specified in the Here's an example of an
Attaching to Node.jsIf you want to attach the VS Code debugger to an external Node.js program, launch Node.js as follows:
or if the program shouldn't start running, but must wait for the debugger to attach:
Now you have a couple options for attaching the debugger to your program:
Let's go through these options in detail: Attach to Node Process actionThe Attach to Node Process command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) opens a Quick Pick menu that lists all potential processes that are available to the Node.js debugger: The individual processes listed in the picker show the debug port and process ID. Once you select your Node.js process in that list, the Node.js debugger will try to attach to it. In addition to Node.js processes, the picker also shows other programs that were launched with one of the various forms Setting up an "Attach" configurationThis option requires more work but in contrast to the previous two options it allows you to configure various debug configuration options explicitly. The simplest "attach" configuration looks like this:
The port If you want to attach to a Node.js process that hasn't been started in debug mode, you can do this by specifying the process ID of the Node.js process as a string:
Since it is a bit laborious to repeatedly find the process ID and enter it in the launch
configuration, Node debug supports a command variable Using the
Stop debuggingUsing the Debug: Stop action (available in the Debug toolbar or via the Command Palette) stops the debug session. If the debug session was started in "attach" mode (and the red terminate button in the Debug toolbar shows a superimposed "plug"), pressing Stop disconnects the Node.js debugger from the debuggee that then continues execution. If the debug session is in "launch" mode, pressing Stop does the following:
So if you see that a debug session doesn't end when you press the red Stop button, then press the button again to force a shutdown of the debuggee. Note that on the Windows operating system, pressing Stop always forcibly kills the debuggee and its child processes. Source mapsThe JavaScript debugger of VS Code supports source maps that help debugging of transpiled languages, for example, TypeScript or minified/uglified JavaScript. With source maps, it's possible to single step through or set breakpoints in the original source. If no source map exists for the original source, or if the source map is broken and cannot successfully map between the source and the generated JavaScript, then breakpoints show up as unverified (gray hollow circles). The source map feature is controlled by the Tool ConfigurationSince source maps are not always automatically created, you should make sure to configure your transpiler to create them. For example: TypeScript For TypeScript, you can enable
sourcemaps by passing
Babel For Babel, you'll want to set the sourceMaps option to
Webpack Webpack has numerous source map options. We
recommend setting the property Also, if you have additional compilation steps in webpack, such as using a TypeScript loader, you'll also want to make sure that those steps are set up to generate sourcemaps. Otherwise, the sourcemaps that webpack generates will map back to the compiled code from the loader, instead of the real sources. Source Map DiscoveryBy default, VS Code will search your entire workspace, excluding
Note that the Source Map ResolutionBy default, only source maps in your You can configure this behavior by setting the
Smart steppingWith the Smart stepping is especially useful for cases like async/await downcompilation in TypeScript, where the compiler injects helper code that is not covered by a source map. The JavaScript source map tipsA common issue when debugging with source maps is that you'll set a breakpoint, and it will turn gray. If you hover the cursor over it, you'll see the message, When you set a breakpoint in When you build your Finally, the debug adapter searches for the full path of Here are some things to try when your breakpoints turn gray:
Remote debugging
If you are unable to use any of the Remote Development extensions to debug your Node.js program, below is a guide on how to debug a remote Node.js program from your local instance of VS Code. The Node.js debugger supports remote debugging where you attach to a process running on a different machine, or in a container. Specify a remote host via the
By default, VS Code will stream the debugged source from the remote Node.js folder to the local VS Code and show it in a read-only editor. You can step through this code, but cannot modify it. If you want VS Code to open the
editable source from your workspace instead, you can set up a mapping between the remote and local locations. A
Access Loaded ScriptsIf you need to set a breakpoint in a script that is not part of your workspace and therefore cannot be easily located and opened through normal VS Code file browsing, you can access the loaded scripts via the LOADED SCRIPTS view in the Run and Debug view: The LOADED SCRIPTS view lets you quickly select the script by typing its name or filter the list when Enable Filter on Type is on. Scripts are loaded into a read-only editor where you can set breakpoints. These breakpoints are remembered across debug sessions but you only have access to the script content while a debug session is running. Restarting debug sessions automatically when source is editedThe If you have started your program
you can attach the VS Code debugger to it with the following launch configuration:
Alternatively
you can start your program
Restart frameThe Node debugger supports restarting execution at a stack frame. This can be useful in situations where you have found a problem in your source code and you want to rerun a small portion of the code with modified input values. Stopping and then restarting the full debug session can be time-consuming. The Restart Frame action allows you to reenter the current function after you have changed variables with the Set Value action:
Restart Frame won't roll back mutation to state outside of the function, so it may not always work as expected. BreakpointsConditional BreakpointsConditional breakpoints are breakpoints that only pause when an expression returns a truthy value. You can create one by right-clicking in the gutter beside a line number and selecting "Conditional Breakpoint": LogpointsSometimes you want to just log a message or value when code hits a certain location, rather than pausing. You can do this with logpoints. Logpoints don't pause, but
rather log a message to the Debug Console when hit. In the JavaScript debugger, you can use curly braces to interpolate expressions into the message, like You can create one by right-clicking in the gutter beside a line number and selecting "Logpoint". For example, this might log something like Hit count breakpointsThe 'hit count condition' controls how many times a breakpoint needs to be hit before it will 'break' execution. You can place a hit count breakpoint by right-clicking in the gutter beside a line number, selecting "Conditional Breakpoint", and then switching to "Hit Count". The hit count syntax supported by the Node.js debugger is either an integer or one of the operators Some examples:
Breakpoint validationFor performance reasons, Node.js parses the functions inside JavaScript files lazily on first access. As a consequence, breakpoints don't work in source code areas that haven't been seen (parsed) by Node.js. Since this behavior is not ideal for debugging, VS Code passes the Since the When doing so, you will find that some of your breakpoints don't "stick" to the line requested but instead "jump" for the next possible line in already-parsed code. To avoid confusion, VS Code always shows breakpoints at the location where Node.js thinks the breakpoint is. In the BREAKPOINTS section, these breakpoints are shown with an arrow between requested and actual line number: This breakpoint validation occurs when a session starts and the breakpoints are registered with Node.js, or when a session is already running and a new breakpoint is set. In this case, the breakpoint may "jump" to a different location. After Node.js has parsed all the code (for example, by running through it), breakpoints can be easily reapplied to the requested locations with the Reapply button in the BREAKPOINTS section header. This should make the breakpoints "jump back" to the requested location. Skipping uninteresting codeVS
Code Node.js debugging has a feature to avoid source code that you don't want to step through (also known as 'Just My Code'). This feature can be enabled with the For example, using:
all code in the Built-in core modules of Node.js can be referred to by the 'magic name'
The exact 'skipping' rules are as follows:
Skipped source is shown in a 'dimmed' style in the CALL STACK view: Hovering over the dimmed entries explains why the stack frame is dimmed. A context menu item on the call stack, Toggle skipping this file enables you to easily skip a file at runtime without adding it to your launch config. This option only persists for the
current debugging session. You can also use it to stop skipping a file that is skipped by the
In the following (
Supported Node-like runtimesThe current VS Code JavaScript debugger supports Node version at or above 8.x, recent Chrome versions, and recent Edge
versions (via the Next stepsIn case you didn't already read the Node.js section, take a look at:
To see tutorials on the basics of Node.js debugging, check out these videos:
To learn about VS Code's task running support, go to:
To write your own debugger extension, visit:
Common questionsCan I debug if I'm using symlinks?Yes, if you've created symlinks for folders inside your project, such as with
If your main script is inside a symlinked path, then you will also need to add the How do I debug ECMAScript modules?If you use esm or pass
9/1/2022 |