Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to debug code in VsCode initiated with Vite?

My dev team configured a Node.js project with TypeScript to use Vite as dev server, using the npm script panel of VsCode. Is there a way to attach the debugger into this Vite server so we can debug the TSX code within the editor?

like image 360
JLuis Estrada Avatar asked Feb 11 '21 00:02

JLuis Estrada


People also ask

Does VS Code have a built in debugger?

VS Code's built-in debugger helps accelerate your edit, compile and debug loop. VS Code has built-in debugging support for the Node.js runtime and can debug JavaScript, TypeScript, or any other language that gets transpiled to JavaScript.

Does Visual Studio Code support remote debugging?

Remote debugging. VS Code does not itself support remote debugging: this is a feature of the debug extension you are using, and you should consult the extension's page in the Marketplace for support and details. There is, however, one exception: the Node.js debugger included in VS Code supports remote debugging.

How do I debug configuration information in Visual Studio Code?

VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings. To create a launch.json file, open your project folder in VS Code ( File > Open Folder) and then select the Configure gear icon on the Run view top bar.

How to debug other languages in Visual Studio Code?

For debugging other languages and runtimes (including PHP, Ruby, Go, C#, Python, C++, Powershell and many others), look for Debuggers extensions in our VS Code Marketplace or select Install Additional Debuggers in the top-level Debug menu. Below are several popular extensions which include debugging support:


Video Answer


2 Answers

The previous answer is for debugging in Chrome, if you prefer Firefox, here's the debug.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": [
    {
      "type": "firefox",
      "request": "launch",
      "name": "vuejs: firefox",
      "url": "http://localhost:8080",
      "webRoot": "${workspaceFolder}/src",
    }
  ]
}

Very important: the "webRoot": "${workspaceFolder}/src", needs to point the the directory where your 'Vue.app' file is located. If that doesn't work, put the directory where your 'index.html' file is located. One of these two should work.

enter image description here

Further info can be found in this discussion on the official Vite GitHub repo: https://github.com/vitejs/vite/discussions/4065

like image 116
Z0B Avatar answered Sep 24 '22 03:09

Z0B


Go to the debug tab and click "create debug.json".

Then adapt the url port to use 4000 if you want to use the Vite plugin for VSCode. Or 3000 if you run it with yarn dev, npm run dev or vite from your console.

I also had to adapt the webRoot as I've created an app folder which is my root folder.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:4000",
            "webRoot": "${workspaceFolder}/app"
        }
    ]
}
like image 21
Harrys Kavan Avatar answered Sep 24 '22 03:09

Harrys Kavan