You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Node.js默认调试器端口?解决9229端口占用报错

Hey there! Let's break down how to change Node.js's default debugger port and fix that "address already in use" error you're seeing.

修改Node.js默认调试器端口号

There are a few straightforward ways to override the default 9229 port for the Node.js inspector:

  • Command line direct specification: When starting your app, append the --inspect flag with your desired port. For example:

    node --inspect=9230 app.js
    

    If you need to allow connections from external devices (not just localhost), you can bind to all addresses like this:

    node --inspect=0.0.0.0:9230 app.js
    

    Note: For older Node.js versions (pre-v7), use --debug=9230 instead, though the inspect API is now the standard.

  • Package.json script configuration: If you use npm scripts to start your app, update the script to include the port flag. For example:

    {
      "scripts": {
        "start": "node --inspect=9230 app.js"
      }
    }
    

    Now running npm start will automatically use your custom port.

  • Environment variable: Set the NODE_INSPECT_PORT environment variable before launching Node.js:

    • On Linux/macOS (bash/zsh):
      export NODE_INSPECT_PORT=9230
      node app.js
      
    • On Windows (Command Prompt):
      set NODE_INSPECT_PORT=9230
      node app.js
      
    • On Windows (PowerShell):
      $env:NODE_INSPECT_PORT=9230
      node app.js
      
解决"Starting inspector on 127.0.0.1:9229 failed: address already in use"报错

This error means another process is already using port 9229. Here's how to fix it:

Option 1: Kill the process using port 9229

First, find out which process is hogging the port:

  • Linux/macOS:
    Use lsof to list processes using the port:

    lsof -i :9229
    

    Look for the PID column, then kill the process with:

    kill -9 <PID>
    

    If you get a permission error, add sudo before the commands.

    Alternatively, use netstat:

    netstat -tulpn | grep 9229
    
  • Windows:
    In Command Prompt, run:

    netstat -ano | findstr :9229
    

    The last number in the output is the PID. Kill the process with:

    taskkill /F /PID <PID>
    

    Or open Task Manager, go to the "Details" tab, find the process with that PID, and end it.

Option 2: Use a different port instead

If you don't want to kill the existing process, just start your Node app with a custom port as we covered earlier. For example:

node --inspect=9230 app.js

Pro tip: Sometimes the stuck process is a leftover Node.js instance that didn't exit properly. Killing it should resolve the issue for future launches on the default port.

内容的提问来源于stack exchange,提问作者Sasi Rekha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:45:54