如何配置VS代码在调试时运行NPX Vite Dev
我是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
, orRun > 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是启动Vite Dev Server( npx vite ),然后自动打开Chrome并将调试会话附加到它。
可以在启动中自定义许多事情。我建议您阅读上面链接的文档。
“魔术”在“ 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.
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"
与其让它运行
npx vite dev
(这是npm:dev
任务),让它运行npx vite vite dev -open
:)Instead of having it run
npx vite dev
(which is thenpm: dev
task), have it runnpx vite dev --open
:)接受的答案对我不起作用(也许是因为我在
vscode
中进行远程开发)。这是对我有用的方法:
编辑'lunaine.json'包含以下内容:
编辑'package.json'的“脚本”元素,以便“ dev”项目包含以下内容:
由于我在运行岩石linux的远程系统上开发,因此我必须从shell中进行以下(一次):
然后重新启动远程系统(确保端口实际上打开)
)嵌入式终端窗口启动服务器:
这启动了开发服务器,该服务器会在端口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:
Edit 'launch.json' to contain the following:
Edit the "scripts" element of 'package.json' so that the "dev" item contains the following:
Since I develop on a remote system running Rocky Linux, I have to do the following (once) from a shell:
Then restart the remote system (to make sure the port is actually open)
With the above changes, type the following command in the embedded terminal window to start the server:
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!
仅供参考,Vite的较新版本为输出增添了颜色,从而破坏了正则表达式匹配。
我不得不添加:
我的启动配置。
FYI, newer versions of vite add color to the output which breaks the regex expression matching.
I had to add:
to my launch configuration.