|
| 1 | +--- |
| 2 | +title: Debugging in VS Code and Chrome |
| 3 | +type: cookbook |
| 4 | +order: 8 |
| 5 | +--- |
| 6 | + |
| 7 | +Every application reaches a point where it's necessary to understand failures, small to large. In this recipe, we explore a few workflows for VS Code users, who are using Chrome to test. |
| 8 | + |
| 9 | +This recipe shows how to use the [Debugger for Chrome](https://github.com/Microsoft/VSCode-chrome-debug) extension with VS Code to debug Vue.js applications generated by the [Vue CLI](https://github.com/vuejs/vue-cli). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +You must have Chrome and VS Code installed. Make sure to get the latest version of [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome) extension installed in VS Code. |
| 14 | + |
| 15 | +Install and create a project with the [vue-cli](https://github.com/vuejs/vue-cli), with the instructions for installation documented in the readme of the project. Change into the newly created application directory and open VS Code. |
| 16 | + |
| 17 | +### Showing Source Code in the Chrome Devtools |
| 18 | + |
| 19 | +Before you can debug your Vue components from VS Code you need to update the generated Webpack config to build sourcemaps. We do this so that our debugger has a way to map the code within a compressed file back to its position in the original file. This ensures that you can debug an application even after your assets have been optimized by Webpack. |
| 20 | + |
| 21 | +Go to `config/index.js` and find the `devtool` property. Update it to: |
| 22 | + |
| 23 | +```json |
| 24 | +devtool: 'source-map', |
| 25 | +``` |
| 26 | + |
| 27 | +### Launching the Application from VS Code |
| 28 | + |
| 29 | +Click on the Debugging icon in the Activity Bar to bring up the Debug view, then click on the gear icon to configure a launch.json file, selecting **Chrome** for the environment. Replace content of the generated launch.json with the following two configurations: |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "version": "0.2.0", |
| 36 | + "configurations": [ |
| 37 | + { |
| 38 | + "type": "chrome", |
| 39 | + "request": "launch", |
| 40 | + "name": "vuejs: chrome", |
| 41 | + "url": "http://localhost:8080", |
| 42 | + "webRoot": "${workspaceFolder}/src", |
| 43 | + "breakOnLoad": true, |
| 44 | + "sourceMapPathOverrides": { |
| 45 | + "webpack:///src/*": "${webRoot}/*" |
| 46 | + } |
| 47 | + } |
| 48 | + ] |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Setting a Breakpoint |
| 53 | + |
| 54 | +1. Set a breakpoint in **src/components/HelloWorld.vue** on `line 90` where the `data` function returns a string. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +2. Open your favorite terminal at the root folder and serve the app using Vue CLI: |
| 59 | + |
| 60 | +``` |
| 61 | +npm start |
| 62 | +``` |
| 63 | + |
| 64 | +3. Go to the Debug view, select the **'vuejs: chrome'** configuration, then press F5 or click the green play button. |
| 65 | + |
| 66 | +4. Your breakpoint should now be hit as the new instance of Chrome opens `http://localhost:8080`. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## Alternative Patterns |
| 71 | + |
| 72 | +### Vue Devtools |
| 73 | + |
| 74 | +There are other methods of debugging, varying in complexity. The most popular and simple of which is to use the excellent [vue-devtools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd). Some of the benefits of working with the devtools are that they enable you to live-edit data properties and see the changes reflected immediately. The other major benefit is the ability to do time travel debugging for Vuex. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +<p class="tip">Please note that if the page uses a production/minified build of Vue.js (such as the standard link from a CDN), devtools inspection is disabled by default so the Vue pane won't show up. If you switch to an unminified version, you may have to give the page a hard refresh to see them.</p> |
| 79 | + |
| 80 | +### Vuetron |
| 81 | + |
| 82 | +[Vuetron](http://vuetron.io/) is a really nice project that extends some of the work that vue-devtools has done. In addition to the normal devtools workflow, you are able to: |
| 83 | + |
| 84 | +* Quickly view API Request/Response: if you're using the fetch API for requests, this event is displayed for any request sent. The expanded card displays the request data as well as the response data. |
| 85 | +* Subscribe to specific parts of your application’s state for faster debugging |
| 86 | +* Visualize component hierarchy, and an animation allows you to collapse or expand the tree for specific hierarchy views. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +### Simple Debugger Statement |
| 91 | + |
| 92 | +The example above has a great workflow. However, there is an alternative option where you can use the [native debugger statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) directly in your code. If you choose to work this way, it's important that you remember to remove the statements when you're done. |
| 93 | + |
| 94 | +```js |
| 95 | +<script> |
| 96 | +export default { |
| 97 | + data() { |
| 98 | + return { |
| 99 | + message: '' |
| 100 | + } |
| 101 | + }, |
| 102 | + mounted() { |
| 103 | + const hello = 'Hello World!' |
| 104 | + debugger |
| 105 | + this.message = hello |
| 106 | + } |
| 107 | +}; |
| 108 | +</script> |
| 109 | +``` |
| 110 | + |
| 111 | +## Acknowledgements |
| 112 | + |
| 113 | +This recipe was based on a contribution from [Kenneth Auchenberg](https://twitter.com/auchenberg), [available here](https://github.com/Microsoft/VSCode-recipes/tree/master/vuejs-cli). |
0 commit comments