|
| 1 | +--- |
| 2 | +title: Breakpoint Debugging |
| 3 | +--- |
| 4 | + |
| 5 | +In addition to the [`@debug`](https://svelte.dev/docs/special-tags#debug) tag, you can also debug Svelte and SvelteKit projects using breakpoints within various tools and development environments. This includes both frontend and backend code. |
| 6 | + |
| 7 | +The following guides assume your JavaScript runtime environment is Node.js. |
| 8 | + |
| 9 | +## Visual Studio Code |
| 10 | + |
| 11 | +With the built-in debug terminal, you can set up breakpoints in source files within VSCode. |
| 12 | + |
| 13 | +1. Open the command palette: `CMD/Ctrl` + `Shift` + `P`. |
| 14 | +2. Find and launch "Debug: JavaScript Debug Terminal". |
| 15 | +3. Start your project using the debug terminal. For example: `npm run dev`. |
| 16 | +4. Set some breakpoints in your client or server-side source code. |
| 17 | +5. Trigger the breakpoint. |
| 18 | + |
| 19 | +### Launch via debug pane |
| 20 | + |
| 21 | +You may alternatively set up a `.vscode/launch.json` in your project. To set one up automatically: |
| 22 | + |
| 23 | +1. Go to the "Run and Debug" pane. |
| 24 | +2. In the "Run" select menu, choose "Node.js...". |
| 25 | +3. Select the "run script" that corresponds to your project, such as "Run script: dev". |
| 26 | +4. Press the "Start debugging" play button, or hit `F5` to begin breakpoint debugging. |
| 27 | + |
| 28 | +Here's an example `launch.json`: |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "version": "0.2.0", |
| 33 | + "configurations": [ |
| 34 | + { |
| 35 | + "command": "npm run dev", |
| 36 | + "name": "Run development server", |
| 37 | + "request": "launch", |
| 38 | + "type": "node-terminal" |
| 39 | + } |
| 40 | + ] |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Further reading: <https://code.visualstudio.com/docs/editor/debugging>. |
| 45 | + |
| 46 | +## Google Chrome and Microsoft Edge Developer Tools |
| 47 | + |
| 48 | +It's possible to debug Node.js applications using a browser-based debugger. |
| 49 | + |
| 50 | +> Note this only works with debugging client-side SvelteKit source maps. |
| 51 | +
|
| 52 | +1. Run the `--inspect` flag when starting the Vite server with Node.js. For instance: `NODE_OPTIONS="--inspect" npm run dev` |
| 53 | +2. Open your site in a new tab. Typically at `localhost:5173`. |
| 54 | +3. Open your browser's dev tools, and click on the "Open dedicated DevTools for Node.js" icon near the top-left. It should display the Node.js logo. |
| 55 | +4. Set up breakpoints and debug your application. |
| 56 | + |
| 57 | +You may alternatively open the debugger devtools by navigating to `chrome://inspect` in Google Chrome, or `edge://inspect` in Microsoft Edge. |
| 58 | + |
| 59 | +## References |
| 60 | + |
| 61 | +- [Debugging Node.js](https://nodejs.org/en/learn/getting-started/debugging) |
0 commit comments