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

如何为VSCode的Node.js网站启动配置创建Restart Config?

How to Set Up Restart Functionality for Your Node.js Project in VS Code

Great question! You have a couple of solid options to add restart capabilities—either auto-restart when files change (super useful during development) or a manual restart command. Let’s break them down:

The easiest way to get auto-restart is using nodemon, a tool that watches for file changes and restarts your Node.js app automatically. Here’s how to set it up with your VS Code launch config:

  1. First, install nodemon as a development dependency:

    npm install --save-dev nodemon
    
  2. Update your .vscode/launch.json with a new configuration that uses nodemon:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch Program",
          "program": "${workspaceFolder}\\main.js"
        },
        {
          "type": "node",
          "request": "launch",
          "name": "Launch with Auto-Restart",
          "runtimeExecutable": "nodemon",
          "program": "${workspaceFolder}\\main.js",
          "restart": true,
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen"
        }
      ]
    }
    
    • runtimeExecutable: Tells VS Code to use nodemon instead of the regular node command.
    • restart: Enables auto-restart whenever files are modified.
    • console: Uses the integrated terminal so you can see the app’s output clearly.

Now, when you select "Launch with Auto-Restart" from the debug dropdown, your app will automatically restart every time you save changes to your code.

Option 2: Manual Restart Script

If you prefer a manual restart command (e.g., to run via the terminal or a VS Code task), you can add an npm script to your package.json:

  1. Open your package.json and add these scripts:

    {
      "scripts": {
        "start": "node main.js",
        "restart": "npm run stop && npm start",
        "stop": "pkill -f 'node main.js' || true"
      }
    }
    
    • Note: The stop command uses pkill which works on macOS/Linux. For Windows, replace the stop script with:
      "stop": "taskkill /F /IM node.exe /T || true"
      
  2. Now you can run npm run restart in your terminal to stop the current app instance and start it fresh.

Bonus: VS Code Debug Toolbar Restart

Don’t forget that when you’re already running your app in debug mode (using your original "Launch Program" config), you can click the Restart button (circular arrow) in the VS Code debug toolbar to instantly restart the session without needing any extra configs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:48:25