To set up debugging for a SvelteKit project in VS Code, especially for your src/routes/api/conversation/[id]/+server.ts
file, you'll need to create a debugging configuration in VS Code that hooks into your development server started by Vite. Here are the steps you need to follow:
1. Install Necessary Extensions
Make sure you have the following VS Code extensions installed:
- Debugger for Chrome or Debugger for Firefox for front-end debugging.
- Svelte for VS Code for enhanced Svelte language support.
- JavaScript Debugger (Nightly) for debugging Node.js applications.
2. Update Your package.json
Scripts
Ensure your dev
script opens the application with source maps enabled for better debugging. Fortunately, Vite generates source maps by default in development mode, so you usually do not need any special configuration for that.
"scripts": {
"dev": "vite dev --open",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write .",
"test": "MONGODB_URL=mongodb://127.0.0.1:27017/ vitest",
"updateLocalEnv": "node --loader ts-node/esm scripts/updateLocalEnv.ts",
"updateProdEnv": "node --loader ts-node/esm scripts/updateProdEnv.ts",
"populate": "vite-node --options.transformMode.ssr='/.*/' scripts/populate.ts"
}
3. Create a launch.json
File
In VS Code, set up your debugging configuration in the .vscode/launch.json
file.
- Press
Ctrl+Shift+P
(or Cmd+Shift+P
on Mac).
- Select
Debug: Open launch.json
.
- Choose Node.js environment if prompted.
Add the following configuration to the launch.json
file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Vite",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/vite/bin/vite.js",
"args": ["dev"],
"cwd": "${workspaceFolder}",
"sourceMaps": true,
"protocol": "inspector",
"console": "integratedTerminal",
"env": {
"NODE_ENV": "development"
}
}
]
}
4. Running the Debugger
- Open the
src/routes/api/conversation/[id]/+server.ts
file and set breakpoints by clicking in the gutter to the left of the line numbers.
- Go to the Debug view by clicking on the Debug icon in the Activity Bar on the side of the window.
- Select
Launch Vite
from the dropdown menu at the top of the Debug pane.
- Click the green play button (or press F5) to start the debugging session.
VS Code will open the integrated terminal, start your Vite development server, and attach the debugger.
5. Debugging in the Browser
If you need to debug the client-side code as well:
- Install the "Debugger for Chrome" or "Debugger for Firefox" extension based on your browser.
- Add another configuration in
launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Vite",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/vite/bin/vite.js",
"args": ["dev"],
"cwd": "${workspaceFolder}",
"sourceMaps": true,
"protocol": "inspector",
"console": "integratedTerminal",
"env": {
"NODE_ENV": "development"
}
},
{
"type": "pwa-chrome",
"request": "launch",
"name": "Debug in Chrome",
"url": "http://localhost:3000", // Ensure this matches your Vite dev server URL
"webRoot": "${workspaceFolder}",
"breakOnLoad": true,
"sourceMaps": true
}
]
}
To use this configuration, ensure your Vite server is running (either through the previous configuration or manually using npm run dev -- --open
) then select Debug in Chrome
and start the debugger.
These steps will set up a VS Code environment for debugging both server-side and client-side code in a SvelteKit project, allowing you to effectively debug your application.