DEV Community

0x2e Tech
0x2e Tech

Posted on • Originally published at 0x2e.tech

1

Launch Node.js DevTools from CLI: A Quick Guide

Let's cut the fluff and get this done. You want to launch Node.js DevTools directly from your command line? I've got you covered. Forget wading through endless Stack Overflow threads; this is the no-nonsense guide you need.

The Problem: Debugging Node.js applications can be a pain. The standard node inspect command is helpful, but it's not always the most convenient. You want a dedicated DevTools window, easily accessible from your terminal.

The Solution: We're going to leverage the power of the --inspect-brk flag combined with a little shell scripting (or a simple batch script for Windows) to create a streamlined process. This will open your DevTools window automatically, paused at the beginning of your script, ready for debugging.

Step-by-Step Guide (macOS/Linux):

  1. Create a Shell Script: Create a new file (e.g., debug.sh) and add the following contents:
#!/bin/bash

# Replace 'your_script.js' with the actual path to your Node.js script
node --inspect-brk your_script.js
Enter fullscreen mode Exit fullscreen mode
  1. Make it Executable: Open your terminal and navigate to the directory where you saved debug.sh. Then, run:
chmod +x debug.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run Your Script: Now, simply execute your script using the shell script:
./debug.sh
Enter fullscreen mode Exit fullscreen mode

This will launch your Node.js script with the debugger attached and automatically open the DevTools window in your browser. You'll be paused at the beginning, giving you ample opportunity to set breakpoints and step through your code.

Step-by-Step Guide (Windows):

  1. Create a Batch Script: Create a new file (e.g., debug.bat) and add this:
@echo off
node --inspect-brk your_script.js
Enter fullscreen mode Exit fullscreen mode
  1. Run Your Batch Script: Double-click debug.bat to run your script with the debugger attached. Your browser will open the DevTools window automatically.

Troubleshooting and Enhancements:

  • Port Conflicts: If you encounter port conflicts (usually port 9229), you can specify a different port using the --inspect=port or --inspect-brk=port flags. For example: node --inspect-brk=9230 your_script.js

  • Chrome DevTools Not Opening: Ensure that you have Google Chrome (or another Chromium-based browser) installed. Sometimes, your browser's settings might prevent automatic opening. Check your browser's settings to see if there are any security restrictions preventing the script from opening a new window.

  • Complex Projects: For larger projects, consider integrating this into your build process or using a task runner like npm scripts. For example, in your package.json:

{
  "scripts": {
    "debug": "node --inspect-brk your_script.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

Then you can run npm run debug to start debugging.

  • Remote Debugging: For remote debugging, you'll need to adjust the --inspect flags to specify the host and port. Consult the Node.js documentation for the detailed options.

Example: Handling a Specific Error

Let's say you're dealing with a TypeError in your_script.js. By launching DevTools as described above, you can step through the code, inspect variables, and identify the precise line causing the error. This level of granular control dramatically accelerates your debugging workflow.

Remember: Always replace your_script.js with the actual path to your Node.js file.

This approach offers a direct, efficient method for launching Node.js DevTools from the command line. No more cumbersome manual processes – just straightforward debugging, ready when you are. Now get out there and debug like a pro!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more