Debugging
Setup
- Open Lab 25 solution code.
- In a command-prompt or terminal, run the commands.
npm install
npm start - In another command-prompt or terminal, run the command.
npm run api
Compiler Errors
In your editor, remove the closing curly brace shown below.
src\Projects\ProjectList.[js|tsx]
<ProjectForm
project={project
- }
onCancel={this.cancelEditing}
></ProjectForm>You should see the following error in the terminal where you are running
npm start
.Failed to compile.
src/projects/ProjectList.[js|tsx]
Line 38:15: Parsing error: Unexpected token, expected "}"
36 | <ProjectForm
37 | project={project
> 38 | onCancel={this.cancelEditing}
| ^
39 | ></ProjectForm>
40 | </div>
41 | );Add the
}
back.Verify the comile error goes away.
Remove the return statement as shown below.
src\Projects\ProjectList.[js|tsx]
class ProjectList extends React.Component {
...
render() {
...
- return
<div className="row">{items}</div>;
}
}You should see the following error in the terminal where you are running
npm start
.
Failed to compile.
./src/projects/ProjectList.js
Line 17:3: Your render method should have return statement react/require-render-return
Line 46:6: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
- Add the
return
back. - Verify the error goes away.
Runtime Errors
Open Chrome DevTools
Open Chrome DevTools
by following these steps:
- In the upper right hand corner of
Chrome
click theThree Dots > More Tools> Developer Tools
.
- Shortcuts:
- (Windows, Linux): CTRL+SHIFT+I
- (MacOS): CMD+OPTION+I
Breakpoints
Open
ProjectsPage.[js|tsx]
in the Chrome DevToolsSources
tab.To find a file in the Chrome DevTools
Sources
tab and open the navigation to follow this or a similar path: top/localhost:3000/Users/[your username]/Documents/git/r16/keeptrack-js/src/... The path may differ depending on your operating system.Set breakpoint on line 38.
Hover
this.props.project
Open the
Console
tab.Log
this.props.project
Step through the code using the buttons outlined in the JavaScript Debugging Reference.
Debugging in Visual Studio Code
This section describes how you can set breakpoints and debug JavaScript or TypeScript directly in Visual Studio Code instead of directly using a browser's DevTools like Chrome.
Note that in the latest versions of Visual Studio Code it is no longer necessary to install the Debugger for Chrome extension.
- If you already have it installed that is OK it will just forward to the built-in
js-debug
functionality.
Steps
- In a Create React App project, run
npm start
command from the command prompt or terminal. - Open
App.js
orApp.tsx
and set a breakpoint by clicking in the gutter to the left of the line number. - Click the
Run\Debugger
in Toolbar. - Click the
Run and Debug
button. - Choose environment
Chrome (preview)
- Change port
8080
to port3000
in thelaunch.json
file. - In the top left of the window click
Launch Chrome again
- You will stop on the breakpoint in VS Code.