Debugging
Setup
- Open Lab 25 solution code.
- In a command-prompt or terminal, run the commands.npm installnpm 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]<ProjectFormproject={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 | <ProjectForm37 | 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.
- Add the
returnback. - Verify the error goes away.
Runtime Errors
Open Chrome DevTools
Open Chrome DevTools by following these steps:
- In the upper right hand corner of
Chromeclick theThree Dots > More Tools> Developer Tools.
- Shortcuts:
- (Windows, Linux): CTRL+SHIFT+I
- (MacOS): CMD+OPTION+I
Breakpoints
Open
ProjectsPage.[js|tsx]in the Chrome DevToolsSourcestab.To find a file in the Chrome DevTools
Sourcestab 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.projectOpen the
Consoletab.Log
this.props.projectStep 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-debugfunctionality.
Steps
- In a Create React App project, run
npm startcommand from the command prompt or terminal. - Open
App.jsorApp.tsxand set a breakpoint by clicking in the gutter to the left of the line number. - Click the
Run\Debuggerin Toolbar. - Click the
Run and Debugbutton. - Choose environment
Chrome (preview) - Change port
8080to port3000in thelaunch.jsonfile. - In the top left of the window click
Launch Chrome again - You will stop on the breakpoint in VS Code.