Debugging Code You've Never SeenLesson 4.1
How to read an error message and find where the bug actually is
stack trace anatomy, reading line numbers, finding the application frame vs library frame, error types, message vs cause distinction, nested errors
The Stack Trace Is a Map to the Bug
Most developers read only the first line of a stack trace. The first line names the error — it doesn't locate the bug. The bug is in your code, not in Node.js internals, so you need to find the frame where your code first appears.
Reading a Stack Trace
TypeError: Cannot read properties of undefined (reading 'email')
at formatUserResponse (src/utils/formatters.js:34:25) ← your code
at UserController.getUser (src/controllers/user.js:18:12) ← your code
at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
at next (node_modules/express/lib/router/route.js:137:13)
// The bug is at formatters.js line 34:
function formatUserResponse(user) {
return {
email: user.email, // line 34: user is undefined here
name: user.name,
};
}
// Root cause: getUser() called formatUserResponse(undefined)
// Fix: check that getUser() returned a user before formattingError Type Tells You the Category
- TypeError — wrong type: null/undefined where object expected, wrong method called
- ReferenceError — variable doesn't exist in scope
- SyntaxError — code couldn't be parsed at all
- RangeError — value out of allowed range (e.g., infinite recursion, invalid array length)
Once you know the category, you know where to look — type errors point to the data source, reference errors point to scope issues.
