Remote Debugging

Remote debugging connects DevTools on the laptop to code running somewhere else. An Android phone: turn on USB debugging on the phone, connect the cable, open…

On your laptop, the login form works smoothly. On a friend's Android phone, the Sign in button does nothing. A phone has no Console, and no right-click and Inspect. A Node server on a VPS (a rented server on the internet) has the same problem: there's no browser tab to open.

The solution is remote debugging: DevTools on your laptop is connected to code running somewhere else. That place can be Chrome on an Android phone, a Node.js server on a VPS, or an app inside a container (a separate box an app runs in). The code keeps running where it is, but you use its Console, breakpoints and Network from your laptop. The connection can be a USB cable, or an SSH tunnel. SSH is a way to log in to another computer from the terminal, over an encrypted connection (scrambled, so nobody can read it on the way).

An Android phone: chrome://inspect over USB

A bug that only appears on a phone (a narrow screen, the on-screen keyboard, Chrome for Android) is fastest to find with the laptop's DevTools connected to the phone.

  1. On the phone: open Settings, then About phone, then tap Build number 7 times until Developer options appears. In that menu, turn on USB debugging (permission to debug the phone over a USB cable).
  2. Connect the phone to the laptop with a USB cable.
  3. In Chrome on the laptop, open chrome://inspect/#devices and make sure Discover USB devices is ticked.
  4. The phone asks Allow USB debugging? Tap Allow.
  5. Your phone appears on that page, with a list of the tabs open on the phone. Click inspect on the tab you want to debug. Full DevTools (Console, Elements, Network) opens for that tab.

The phone doesn't appear? If you have adb (Android Debug Bridge, a terminal program for Android developers), check with adb devices:

$ adb devices
List of devices attached
R58M41ABCDE	unauthorized

unauthorized means you haven't tapped Allow on the phone yet. Once it's allowed, the status changes to device.

Port forwarding: your dev server (the server you use to try the app while you build it) runs at localhost:3000 on the laptop. The number 3000 is the port, a door number where the server waits for connections. But localhost on the phone is the phone itself, not your laptop. In chrome://inspect, click Port forwarding, fill in 3000 in the Port field and localhost:3000 in the IP address and port field, then tick Enable port forwarding. Now localhost:3000 in Chrome on the phone opens the server on the laptop.

Node.js: --inspect and --inspect-brk

A Node.js server has no browser tab, so its debugger (what runs breakpoints) is turned on with a flag, an extra option in the command that starts with --. This is what Node.js 24 prints:

$ node --inspect app.js
Debugger listening on ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e
For help, see: https://nodejs.org/learn/getting-started/debugging

A distant server: through an SSH tunnel

Look at the address in that output: 127.0.0.1:9229. The address 127.0.0.1 means the machine itself: Node's debugger deliberately listens only for connections from its own machine. So if you add server-IP:9229 with the Configure button in chrome://inspect, the server never appears.

Don't solve that with node --inspect=0.0.0.0:9229. The address 0.0.0.0 means every address, so the debugger is open to everyone. Anyone who can reach that port can run any code they like on your server. Use an SSH tunnel: an SSH connection that forwards one port on the laptop to one port on the server.

# on the laptop
$ ssh -L 9229:localhost:9229 [email protected]

The opposite direction: ngrok for webhooks

Sometimes the direction is the opposite: an outside service needs to reach your laptop. An example is a webhook, a request that a service sends to your server when something happens, for example a payment succeeds. A payment gateway (an online payment service) will only send a webhook to a public URL, one that can be opened from the internet. Yet your server runs at localhost:3000. ngrok makes a tunnel from the internet to your laptop:

$ ngrok http 3000

ngrok gives you a temporary public HTTPS URL. Every request to that URL is forwarded to http://localhost:3000. Register that URL as the webhook address, and the requests arrive at your local server, with your breakpoints working. Stop ngrok when you're done.

Before ngrok http 3000 can run, ngrok needs an account: create one first, then save your token (a code that identifies your account) once with ngrok config add-authtoken <token>. Other options:

A stack trace from the phone: source maps

The code running on the phone has been bundled and minified. Bundled means every file is joined into one file, bundle.js. Minified means spaces and line breaks are removed and names are shortened to make the file small, so its lines are very long. An error reported from the phone points at bundle.js:1:0 (line 1, column 0), and you can't read the code there. A source map is a table that maps a position in the bundle (line, column) to a position in the original file. This is the table in the exercise and in the Lab:

In bundle.js (line:column) In the original file
1:0 auth.ts:45:2
1:52 utils.ts:12:0
2:0 login.ts:103:4

In the exercise, createSourceMap makes a method resolve, which looks up the original position in that table. formatStackTrace rewrites each line of a stack trace (called a frame in the code) with its original position:

function createSourceMap(mappings) {
  return {
    resolve(minLine, minCol) {
      const match = mappings.find((m) => m.minLine === minLine && m.minCol === minCol);
      if (!match) return null; // a position that isn't listed
      return { file: match.origFile, line: match.origLine, col: match.origCol };
    },
  };
}

function formatStackTrace(frames, sourceMap) {
  return frames.map((frame) => {
    const original = sourceMap.resolve(frame.line, frame.col);
    const file = original ? original.file : frame.file; // no mapping: use the bundle position
    const line = original ? original.line : frame.line;
    const col = original ? original.col : frame.col;
    return frame.fn + " (" + file + ":" + line + ":" + col + ")";
  });
}
Frame from the phone Without a source map With a source map
validateUser, bundle.js 1:0 validateUser (bundle.js:1:0) validateUser (auth.ts:45:2)
handleLogin, bundle.js 2:0 handleLogin (bundle.js:2:0) handleLogin (login.ts:103:4)

A real source map is more complex (its positions are encoded in a mappings string), and DevTools uses it automatically as long as the .map file can be downloaded. This exercise is the simple version.

💡 iPhone: Safari Web Inspector

Chrome on an iPhone uses Safari's engine, so Chrome's DevTools can't connect to it. Use Web Inspector, Safari's DevTools:

  1. On the iPhone, turn on Web Inspector in Safari's settings (the Advanced section).
  2. In Safari on the Mac, check Show features for web developers (Settings, Advanced tab). The Develop menu only appears after that.
  3. Connect the iPhone to the Mac with a cable.
  4. In Safari on the Mac, open the Develop menu and choose your iPhone.

After one connection with the cable, choose Connect via Network in your iPhone's submenu, so that next time the same Wi-Fi network is enough. Without a Mac, use a cloud device service (real phones you borrow over the internet) such as BrowserStack.

⚠️ Why it won't connect, or connects for everyone

TL;DR: Remote debugging connects DevTools on the laptop to code running somewhere else. An Android phone: turn on USB debugging on the phone, connect the cable, open chrome://inspect/#devices in Chrome on the laptop, tap Allow on the phone, then click inspect. Port forwarding opens the laptop's localhost:3000 from the phone. Node.js: node --inspect opens the debugger at 127.0.0.1:9229, which only the machine itself can reach, and --inspect-brk stops at the first line. A distant server: make an SSH tunnel (an encrypted connection) with ssh -L 9229:localhost:9229 user@server, and never use 0.0.0.0 (it opens the debugger to everyone). A webhook (a request from an outside service) to localhost: ngrok http 3000 gives a public URL that forwards requests to your laptop. A stack trace from a bundle (every file joined into one) is translated with a source map, a table that maps a position in the bundle to the original file. In the exercise, resolve looks for an exactly matching position or returns null, and each frame (a line of the stack trace) is written fn (file:line:column).

⚡ An Android phone over USB

Meaning: Turn on USB debugging on the phone, connect the cable, open chrome://inspect/#devices in Chrome on the laptop, tap Allow on the phone, then click inspect on the tab you want to debug. Full DevTools (Console, Elements, Network) opens for that tab.

⚡ Node.js with --inspect

Write:

$ node --inspect app.js
Guess first: --inspect turns on the debugger, and the program runs right away. The bug happens as the program starts. Is --inspect enough?

No. The faulty code has already run before you can attach (connect DevTools). Use --inspect-brk: this flag stops at the first line of your code and waits for you to connect.

Meaning: The debugger turns on at port 9229 (the door number for connections). On the laptop, open chrome://inspect and click Open dedicated DevTools for Node.

⚡ A distant server through an SSH tunnel

Write:

$ ssh -L 9229:localhost:9229 [email protected]
Guess first: Node's debugger only listens on its own machine (127.0.0.1), so your laptop can't connect directly. Why not just use node --inspect=0.0.0.0:9229 (every address)?

That opens the debugger to everyone. Anyone who can reach that port can run any code they like on your server.

Meaning: This command makes an SSH tunnel (an encrypted connection) to the server 203.0.113.10. Port 9229 on the laptop is forwarded over SSH to localhost:9229 on the server, so DevTools on the laptop can connect. Port 9229 on the server stays closed to the public.

⚡ A webhook to localhost: ngrok

Meaning: A webhook is a request that an outside service sends to your server, and that service only sends to a public URL. ngrok http 3000 (after you create an ngrok account and save its token) gives you a temporary public HTTPS URL that forwards every request to localhost:3000. Register that URL as the webhook address, and the requests arrive at your local server, with your breakpoints working.

What you will learn