Debugging node js windows

Node.js v15.14.0 documentation

Debugger #

Node.js includes an out-of-process debugging utility accessible via a V8 Inspector and built-in debugging client. To use it, start Node.js with the inspect argument followed by the path to the script to debug; a prompt will be displayed indicating successful launch of the debugger:

The Node.js debugger client is not a full-featured debugger, but simple step and inspection are possible.

Inserting the statement debugger; into the source code of a script will enable a breakpoint at that position in the code:

Once the debugger is run, a breakpoint will occur at line 3:

The repl command allows code to be evaluated remotely. The next command steps to the next line. Type help to see what other commands are available.

Pressing enter without typing a command will repeat the previous debugger command.

Watchers #

It is possible to watch expression and variable values while debugging. On every breakpoint, each expression from the watchers list will be evaluated in the current context and displayed immediately before the breakpoint’s source code listing.

To begin watching an expression, type watch(‘my_expression’) . The command watchers will print the active watchers. To remove a watcher, type unwatch(‘my_expression’) .

Command reference #

Stepping #

  • cont , c : Continue execution
  • next , n : Step next
  • step , s : Step in
  • out , o : Step out
  • pause : Pause running code (like pause button in Developer Tools)

Breakpoints #

  • setBreakpoint() , sb() : Set breakpoint on current line
  • setBreakpoint(line) , sb(line) : Set breakpoint on specific line
  • setBreakpoint(‘fn()’) , sb(. ) : Set breakpoint on a first statement in function’s body
  • setBreakpoint(‘script.js’, 1) , sb(. ) : Set breakpoint on first line of script.js
  • setBreakpoint(‘script.js’, 1, ‘num , sb(. ) : Set conditional breakpoint on first line of script.js that only breaks when num evaluates to true
  • clearBreakpoint(‘script.js’, 1) , cb(. ) : Clear breakpoint in script.js on line 1

It is also possible to set a breakpoint in a file (module) that is not loaded yet:

It is also possible to set a conditional breakpoint that only breaks when a given expression evaluates to true :

Information #

  • backtrace , bt : Print backtrace of current execution frame
  • list(5) : List scripts source code with 5 line context (5 lines before and after)
  • watch(expr) : Add expression to watch list
  • unwatch(expr) : Remove expression from watch list
  • watchers : List all watchers and their values (automatically listed on each breakpoint)
  • repl : Open debugger’s repl for evaluation in debugging script’s context
  • exec expr : Execute an expression in debugging script’s context

Execution control #

  • run : Run script (automatically runs on debugger’s start)
  • restart : Restart script
  • kill : Kill script

Various #

  • scripts : List all loaded scripts
  • version : Display V8’s version

Advanced usage #

V8 inspector integration for Node.js #

V8 Inspector integration allows attaching Chrome DevTools to Node.js instances for debugging and profiling. It uses the Chrome DevTools Protocol.

V8 Inspector can be enabled by passing the —inspect flag when starting a Node.js application. It is also possible to supply a custom port with that flag, e.g. —inspect=9222 will accept DevTools connections on port 9222.

To break on the first line of the application code, pass the —inspect-brk flag instead of —inspect .

(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29 at the end of the URL is generated on the fly, it varies in different debugging sessions.)

If the Chrome browser is older than 66.0.3345.0, use inspector.html instead of js_app.html in the above URL.

Chrome DevTools doesn’t support debugging worker threads yet. ndb can be used to debug them.

Debugging Guide

This guide will help you get started debugging your Node.js apps and scripts.

Enable Inspector

When started with the —inspect switch, a Node.js process listens for a debugging client. By default, it will listen at host and port 127.0.0.1:9229. Each process is also assigned a unique UUID.

Читайте также:  Установка линукс без носителя

Inspector clients must know and specify host address, port, and UUID to connect. A full URL will look something like ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e .

Node.js will also start listening for debugging messages if it receives a SIGUSR1 signal. ( SIGUSR1 is not available on Windows.) In Node.js 7 and earlier, this activates the legacy Debugger API. In Node.js 8 and later, it will activate the Inspector API.

Security Implications

Since the debugger has full access to the Node.js execution environment, a malicious actor able to connect to this port may be able to execute arbitrary code on behalf of the Node.js process. It is important to understand the security implications of exposing the debugger port on public and private networks.

Exposing the debug port publicly is unsafe

If the debugger is bound to a public IP address, or to 0.0.0.0, any clients that can reach your IP address will be able to connect to the debugger without any restriction and will be able to run arbitrary code.

By default node —inspect binds to 127.0.0.1. You explicitly need to provide a public IP address or 0.0.0.0, etc., if you intend to allow external connections to the debugger. Doing so may expose you to a potentially significant security threat. We suggest you ensure appropriate firewalls and access controls in place to prevent a security exposure.

See the section on ‘Enabling remote debugging scenarios’ on some advice on how to safely allow remote debugger clients to connect.

Local applications have full access to the inspector

Even if you bind the inspector port to 127.0.0.1 (the default), any applications running locally on your machine will have unrestricted access. This is by design to allow local debuggers to be able to attach conveniently.

Browsers, WebSockets and same-origin policy

Websites open in a web-browser can make WebSocket and HTTP requests under the browser security model. An initial HTTP connection is necessary to obtain a unique debugger session id. The same-origin-policy prevents websites from being able to make this HTTP connection. For additional security against DNS rebinding attacks, Node.js verifies that the ‘Host’ headers for the connection either specify an IP address or localhost or localhost6 precisely.

These security policies disallow connecting to a remote debug server by specifying the hostname. You can work-around this restriction by specifying either the IP address or by using ssh tunnels as described below.

Inspector Clients

Several commercial and open source tools can connect to the Node.js Inspector. Basic info on these follows:

node-inspect

  • CLI Debugger supported by the Node.js Foundation which uses the Inspector Protocol.
  • A version is bundled with Node.js and can be used with node inspect myscript.js .
  • The latest version can also be installed independently (e.g. npm install -g node-inspect ) and used with node-inspect myscript.js .

Chrome DevTools 55+, Microsoft Edge

  • Option 1: Open chrome://inspect in a Chromium-based browser or edge://inspect in Edge. Click the Configure button and ensure your target host and port are listed.
  • Option 2: Copy the devtoolsFrontendUrl from the output of /json/list (see above) or the —inspect hint text and paste into Chrome.

Visual Studio Code 1.10+

  • In the Debug panel, click the settings icon to open .vscode/launch.json . Select «Node.js» for initial setup.

Visual Studio 2017

  • Choose «Debug > Start Debugging» from the menu or hit F5.
  • Detailed instructions.

JetBrains WebStorm 2017.1+ and other JetBrains IDEs

  • Create a new Node.js debug configuration and hit Debug. —inspect will be used by default for Node.js 7+. To disable uncheck js.debugger.node.use.inspect in the IDE Registry.

chrome-remote-interface

  • Library to ease connections to Inspector Protocol endpoints.

Gitpod

  • Start a Node.js debug configuration from the Debug view or hit F5 . Detailed instructions

Eclipse IDE with Eclipse Wild Web Developer extension

  • From a .js file, choose «Debug As. > Node program», or
  • Create a Debug Configuration to attach debugger to running Node.js application (already started with —inspect ).

Command-line options

The following table lists the impact of various runtime flags on debugging:

Flag Meaning
—inspect
  • Enable inspector agent
  • Listen on default address and port (127.0.0.1:9229)
—inspect=[host:port]
  • Enable inspector agent
  • Bind to address or hostname host (default: 127.0.0.1)
  • Listen on port port (default: 9229)
—inspect-brk
  • Enable inspector agent
  • Listen on default address and port (127.0.0.1:9229)
  • Break before user code starts
—inspect-brk=[host:port]
  • Enable inspector agent
  • Bind to address or hostname host (default: 127.0.0.1)
  • Listen on port port (default: 9229)
  • Break before user code starts
node inspect script.js
  • Spawn child process to run user’s script under —inspect flag; and use main process to run CLI debugger.
node inspect —port=xxxx script.js
  • Spawn child process to run user’s script under —inspect flag; and use main process to run CLI debugger.
  • Listen on port port (default: 9229)
Читайте также:  Частота процессора линукс терминал

Enabling remote debugging scenarios

We recommend that you never have the debugger listen on a public IP address. If you need to allow remote debugging connections we recommend the use of ssh tunnels instead. We provide the following example for illustrative purposes only. Please understand the security risk of allowing remote access to a privileged service before proceeding.

Let’s say you are running Node.js on a remote machine, remote.example.com, that you want to be able to debug. On that machine, you should start the node process with the inspector listening only to localhost (the default).

Now, on your local machine from where you want to initiate a debug client connection, you can setup an ssh tunnel:

This starts a ssh tunnel session where a connection to port 9221 on your local machine will be forwarded to port 9229 on remote.example.com. You can now attach a debugger such as Chrome DevTools or Visual Studio Code to localhost:9221, which should be able to debug as if the Node.js application was running locally.

Legacy Debugger

The legacy debugger has been deprecated as of Node.js 7.7.0. Please use —inspect and Inspector instead.

When started with the —debug or —debug-brk switches in version 7 and earlier, Node.js listens for debugging commands defined by the discontinued V8 Debugging Protocol on a TCP port, by default 5858 . Any debugger client which speaks this protocol can connect to and debug the running process; a couple popular ones are listed below.

The V8 Debugging Protocol is no longer maintained or documented.

Built-in Debugger

Start node debug script_name.js to start your script under the builtin command-line debugger. Your script starts in another Node.js process started with the —debug-brk option, and the initial Node.js process runs the _debugger.js script and connects to your target.

node-inspector

Debug your Node.js app with Chrome DevTools by using an intermediary process which translates the Inspector Protocol used in Chromium to the V8 Debugger protocol used in Node.js.

В© OpenJS Foundation. All Rights Reserved. Portions of this site originally В© Joyent.

Debugging Guide

This guide will help you get started debugging your Node.js apps and scripts.

Enable Inspector

When started with the —inspect switch, a Node.js process listens for a debugging client. By default, it will listen at host and port 127.0.0.1:9229. Each process is also assigned a unique UUID.

Inspector clients must know and specify host address, port, and UUID to connect. A full URL will look something like ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e .

Node.js will also start listening for debugging messages if it receives a SIGUSR1 signal. ( SIGUSR1 is not available on Windows.) In Node.js 7 and earlier, this activates the legacy Debugger API. In Node.js 8 and later, it will activate the Inspector API.

Security Implications

Since the debugger has full access to the Node.js execution environment, a malicious actor able to connect to this port may be able to execute arbitrary code on behalf of the Node.js process. It is important to understand the security implications of exposing the debugger port on public and private networks.

Exposing the debug port publicly is unsafe

If the debugger is bound to a public IP address, or to 0.0.0.0, any clients that can reach your IP address will be able to connect to the debugger without any restriction and will be able to run arbitrary code.

By default node —inspect binds to 127.0.0.1. You explicitly need to provide a public IP address or 0.0.0.0, etc., if you intend to allow external connections to the debugger. Doing so may expose you to a potentially significant security threat. We suggest you ensure appropriate firewalls and access controls in place to prevent a security exposure.

See the section on ‘Enabling remote debugging scenarios’ on some advice on how to safely allow remote debugger clients to connect.

Local applications have full access to the inspector

Even if you bind the inspector port to 127.0.0.1 (the default), any applications running locally on your machine will have unrestricted access. This is by design to allow local debuggers to be able to attach conveniently.

Browsers, WebSockets and same-origin policy

Websites open in a web-browser can make WebSocket and HTTP requests under the browser security model. An initial HTTP connection is necessary to obtain a unique debugger session id. The same-origin-policy prevents websites from being able to make this HTTP connection. For additional security against DNS rebinding attacks, Node.js verifies that the ‘Host’ headers for the connection either specify an IP address or localhost or localhost6 precisely.

Читайте также:  Свойства vpn соединения windows 10 где находится

These security policies disallow connecting to a remote debug server by specifying the hostname. You can work-around this restriction by specifying either the IP address or by using ssh tunnels as described below.

Inspector Clients

Several commercial and open source tools can connect to the Node.js Inspector. Basic info on these follows:

node-inspect

  • CLI Debugger supported by the Node.js Foundation which uses the Inspector Protocol.
  • A version is bundled with Node.js and can be used with node inspect myscript.js .
  • The latest version can also be installed independently (e.g. npm install -g node-inspect ) and used with node-inspect myscript.js .

Chrome DevTools 55+, Microsoft Edge

  • Option 1: Open chrome://inspect in a Chromium-based browser or edge://inspect in Edge. Click the Configure button and ensure your target host and port are listed.
  • Option 2: Copy the devtoolsFrontendUrl from the output of /json/list (see above) or the —inspect hint text and paste into Chrome.

Visual Studio Code 1.10+

  • In the Debug panel, click the settings icon to open .vscode/launch.json . Select «Node.js» for initial setup.

Visual Studio 2017

  • Choose «Debug > Start Debugging» from the menu or hit F5.
  • Detailed instructions.

JetBrains WebStorm 2017.1+ and other JetBrains IDEs

  • Create a new Node.js debug configuration and hit Debug. —inspect will be used by default for Node.js 7+. To disable uncheck js.debugger.node.use.inspect in the IDE Registry.

chrome-remote-interface

  • Library to ease connections to Inspector Protocol endpoints.

Gitpod

  • Start a Node.js debug configuration from the Debug view or hit F5 . Detailed instructions

Eclipse IDE with Eclipse Wild Web Developer extension

  • From a .js file, choose «Debug As. > Node program», or
  • Create a Debug Configuration to attach debugger to running Node.js application (already started with —inspect ).

Command-line options

The following table lists the impact of various runtime flags on debugging:

Flag Meaning
—inspect
  • Enable inspector agent
  • Listen on default address and port (127.0.0.1:9229)
—inspect=[host:port]
  • Enable inspector agent
  • Bind to address or hostname host (default: 127.0.0.1)
  • Listen on port port (default: 9229)
—inspect-brk
  • Enable inspector agent
  • Listen on default address and port (127.0.0.1:9229)
  • Break before user code starts
—inspect-brk=[host:port]
  • Enable inspector agent
  • Bind to address or hostname host (default: 127.0.0.1)
  • Listen on port port (default: 9229)
  • Break before user code starts
node inspect script.js
  • Spawn child process to run user’s script under —inspect flag; and use main process to run CLI debugger.
node inspect —port=xxxx script.js
  • Spawn child process to run user’s script under —inspect flag; and use main process to run CLI debugger.
  • Listen on port port (default: 9229)

Enabling remote debugging scenarios

We recommend that you never have the debugger listen on a public IP address. If you need to allow remote debugging connections we recommend the use of ssh tunnels instead. We provide the following example for illustrative purposes only. Please understand the security risk of allowing remote access to a privileged service before proceeding.

Let’s say you are running Node.js on a remote machine, remote.example.com, that you want to be able to debug. On that machine, you should start the node process with the inspector listening only to localhost (the default).

Now, on your local machine from where you want to initiate a debug client connection, you can setup an ssh tunnel:

This starts a ssh tunnel session where a connection to port 9221 on your local machine will be forwarded to port 9229 on remote.example.com. You can now attach a debugger such as Chrome DevTools or Visual Studio Code to localhost:9221, which should be able to debug as if the Node.js application was running locally.

Legacy Debugger

The legacy debugger has been deprecated as of Node.js 7.7.0. Please use —inspect and Inspector instead.

When started with the —debug or —debug-brk switches in version 7 and earlier, Node.js listens for debugging commands defined by the discontinued V8 Debugging Protocol on a TCP port, by default 5858 . Any debugger client which speaks this protocol can connect to and debug the running process; a couple popular ones are listed below.

The V8 Debugging Protocol is no longer maintained or documented.

Built-in Debugger

Start node debug script_name.js to start your script under the builtin command-line debugger. Your script starts in another Node.js process started with the —debug-brk option, and the initial Node.js process runs the _debugger.js script and connects to your target.

node-inspector

Debug your Node.js app with Chrome DevTools by using an intermediary process which translates the Inspector Protocol used in Chromium to the V8 Debugger protocol used in Node.js.

В© OpenJS Foundation. All Rights Reserved. Portions of this site originally В© Joyent.

Оцените статью