Script Valley
Reading Other People's Code
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

Stack trace anatomy diagram

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 formatting

Error 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.

Up next

How to use console.log strategically when debugging unfamiliar code

Sign in to track progress

How to read an error message and find where the bug actually is โ€” Debugging Code You've Never Seen โ€” Reading Other People's Code โ€” Script Valley โ€” Script Valley