如何配置VS代码在调试时运行NPX Vite Dev

发布于 2025-01-25 16:10:03 字数 1860 浏览 6 评论 0原文

我是VS代码和JavaScript的新手,并且我正在尝试使用 svelte ,但是我有一个我似乎无法解决的问题。 (我的代码当前只是创建新项目时给出的默认代码;我根本没有更改它。)

当我通过Windows终端运行应用程序时(通过导航到项目root Directory并运行npx vite dev),该应用程序运行正常,我的浏览器可以连接到localhost:3000

但是,当我按两个:

  • run>开始调试,或
  • 运行> 在Visual Studio代码中不调试的情况下运行

,它将Chrome打开到localhost:3000,但我只看到localhost拒绝连接。我认为VS代码从未真正运行命令npx vite vite dev,但我不知道该如何更改。

当我打开.vscode/lunaine.json时,我看到了这一点:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with Chrome",
            "type": "pwa-chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            
        }
    ]
}

我不确定我应该在这里添加什么以使其工作。任何帮助都将不胜感激,如果这是一个愚蠢的问题,则很抱歉,但是我无法为搜索Google或Soe的任何帮助提供资金。

编辑:

我几乎通过添加prelaunchtask来工作,但是现在Chrome在我开始调试时不再自动打开,因此我不妨运行npm :dev自己。

这是.vscode/lunaine.json现在:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with Chrome",
            "type": "pwa-chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "npm: dev"
        }
    ]
}

我认为这可能是因为npm:dev task(有效运行npx vite vite vite vite dev)正在阻止,只有在我按下停止按钮(或双击CTRL+C)时才能完成,因此未打开Chrome,因为VS代码认为预定任务仍在运行。

如果有任何办法,我可以告诉VS代码在继续运行npm:dev时打开Chrome?

I am new to VS Code and JavaScript, and I am trying to make a simple app using Vite and Svelte, but I have a problem which I can't seem to resolve. (My code is currently just the default code given when a new project is created; I haven't changed it at all.)

When I run my app through Windows Terminal (by navigating to the project root directory and running npx vite dev), the app runs fine and my browser can connect to localhost:3000.

However, when I press on either:

  • Run > Start Debugging, or
  • Run > Run Without Debugging

in Visual Studio Code, it opens up Chrome to localhost:3000 but I just see localhost refused to connect. I think VS Code is never actually running the command npx vite dev, but I don't know how to change this.

When I open up .vscode/launch.json, I see this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with Chrome",
            "type": "pwa-chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            
        }
    ]
}

And I am not sure what I should add here to get this to work. Any help would be appreciated, and sorry if this is a bit of a stupid question, but I couldn't fund any help searching Google or SO.

EDIT:

I have almost got this working by adding a preLaunchTask, but now chrome no longer automatically opens when I start debugging, so I might as well just run npm: dev on its own.

Here is .vscode/launch.json now:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with Chrome",
            "type": "pwa-chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            "preLaunchTask": "npm: dev"
        }
    ]
}

I think this might be because the npm: dev task (which effectively runs npx vite dev) is blocking, and only finishes when I press the stop button (or double-click ctrl+c), so chrome is not opened because VS Code thinks the pre-launch task is still running.

If there any way I can tell VS Code to open Chrome while continuing to run npm: dev?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2025-02-01 16:10:03

这是启动Vite Dev Server( npx vite ),然后自动打开Chrome并将调试会话附加到它。

// launch.json
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Vite DEV server",
      "request": "launch",
      "runtimeExecutable": "npx",
      "runtimeArgs": [
        "vite",
      ],
      "type": "node",
      "serverReadyAction": {
        "action": "debugWithChrome",
        "pattern": "Local: http://localhost:([0-9]+)",
        "uriFormat": "http://localhost:%s"
      }
    },
  ],
}

可以在启动中自定义许多事情。我建议您阅读上面链接的文档。

“魔术”在“ serverreadyAction ”中发生了,其中将“ Action ”设置为“ debugwithchrome ”以打开Chrome。

模式”是用于捕获服务器已启动的端口的正则表达式。

最后,通过使用%s,您可以将端口添加到“ uriformat ”中的URL中。 %s表示用“ 模式”中的正则捕获的捕获

Here's the VSCode way to start the Vite Dev server (npx vite) and then automatically open Chrome and attach a debug session to it.

// launch.json
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Vite DEV server",
      "request": "launch",
      "runtimeExecutable": "npx",
      "runtimeArgs": [
        "vite",
      ],
      "type": "node",
      "serverReadyAction": {
        "action": "debugWithChrome",
        "pattern": "Local: http://localhost:([0-9]+)",
        "uriFormat": "http://localhost:%s"
      }
    },
  ],
}

Many things can be customized in the launch.json. I recommend you to read the documentation linked above.

The "magic" happends in "serverReadyAction" where you set the "action" to "debugWithChrome" to open chrome.

The "pattern" is a regex used to capture the port on which the server have been launched.

Finally, you add the port to the URL in the "uriFormat" by using %s. %s represent the capture done with the regex in "pattern"

甜宝宝 2025-02-01 16:10:03

与其让它运行npx vite dev(这是npm:dev任务),让它运行npx vite vite dev -open :)

Instead of having it run npx vite dev (which is the npm: dev task), have it run npx vite dev --open :)

梦太阳 2025-02-01 16:10:03

接受的答案对我不起作用(也许是因为我在vscode中进行远程开发)。

这是对我有用的方法:

  1. 编辑'lunaine.json'包含以下内容:

      {
      //使用Intellisense了解可能的属性。
      //悬停以查看现有属性的描述。
      //有关更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
      “版本”:“ 0.2.0”,
      “配置”:[
        {
          “ type”:“ chrome”,
          “请求”:“启动”,
          “名称”:“启动Chrome”,
          “ url”:“ http:// localhost:5173”,,
          “ webroot”:“ $ {workspacefolder}”,
          “ sourcemaps”:是的,
          “ esolvesourcemaploclocations”:[
              “ $ {workspacefolder}/**”,
              “!**/node_modules/**”
          ],,
        },,
      这是给出的
    }
     
  2. 编辑'package.json'的“脚本”元素,以便“ dev”项目包含以下内容:

     “ dev”:“ vite -horst -open”,
     
  3. 由于我在运行岩石linux的远程系统上开发,因此我必须从shell中进行以下(一次):

      sudo firewall-cmd -permanent -add-port = 5173/tcp
     

    然后重新启动远程系统(确保端口实际上打开)

      sudo关闭-r现在
     

)嵌入式终端窗口启动服务器:

yarn dev

这启动了开发服务器,该服务器会在端口5173上听,并打开Chrome Development浏览器。

您可以关闭开发浏览器,然后通过单击“运行”视图顶部的“启动Chrome”按钮重新打开它。

这为我解决了问题。我将所有内容都使用远程开发,因此我需要一个本地的Chrome开发浏览器(通过其特殊VSCODE端口)连接到远程服务器。

祝你好运!

The accepted answer didn't work for me (perhaps because I do remote development in vscode).

Here is what works for me:

  1. Edit 'launch.json' to contain the following:

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "type": "chrome",
          "request": "launch",
          "name": "Launch Chrome",
          "url": "http://localhost:5173",
          "webRoot": "${workspaceFolder}",
          "sourceMaps": true,
          "resolveSourceMapLocations": [
              "${workspaceFolder}/**",
              "!**/node_modules/**"
          ],
        },
      ]
    }
    
  2. Edit the "scripts" element of 'package.json' so that the "dev" item contains the following:

    "dev": "vite --host --open",
    
  3. Since I develop on a remote system running Rocky Linux, I have to do the following (once) from a shell:

    sudo firewall-cmd --permanent --add-port=5173/tcp
    

    Then restart the remote system (to make sure the port is actually open)

    sudo shutdown -r now
    

With the above changes, type the following command in the embedded terminal window to start the server:

yarn dev

This starts the development server, which listens on port 5173, and opens a Chrome development browser.

You can close the development browser and then reopen it with a click on the "Launch Chrome" button at the top of the "Run" view.

This solved the problem for me. I use remote development for everything, so I need a local Chrome development browser to connect (through its special VSCode port) to the remote server.

Good luck!

桃酥萝莉 2025-02-01 16:10:03

仅供参考,Vite的较新版本为输出增添了颜色,从而破坏了正则表达式匹配。

我不得不添加:

"env": {
   "NO_COLOR": "1"
},

我的启动配置。

FYI, newer versions of vite add color to the output which breaks the regex expression matching.

I had to add:

"env": {
   "NO_COLOR": "1"
},

to my launch configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文