Quick answer: Identify which expression is undefined at the failing line, fix the source (ordering, missed lookup, wrong key), and guard genuinely optional chains with optional chaining plus defaults.
The most common JavaScript error in existence, and in games it clusters around load order and data lookups. The stack trace names the line; the property name in the message names which access failed. Here is the routine.
How to fix it
1. Read the property in the message
'reading 'health'' means the thing before .health is undefined — split dense chains (a.b.c.d) into steps or log each link to find the broken one.
2. Fix load order, not just symptoms
Game code running before configs, saves, or assets resolve is the systemic cause — gate the game loop behind an async init that completes all loading first.
3. Use optional chaining deliberately
entity?.stats?.speed ?? 1 is right for genuinely optional data; sprinkling ?. everywhere converts crashes into silent wrong behavior — decide optional-vs-bug per site.
4. Capture it from players
Hook window.onerror/error tracking in shipped builds — this error's wild occurrences (weird save data, slow networks reordering loads) rarely reproduce at your desk.
Catching the ones you can't reproduce
The hardest version of this to fix is the one you can't reproduce — it only happens on a player's hardware, OS, driver, or save state, under conditions that simply aren't present on your machine. A report that says “it crashed” or “it froze” gives you nothing to act on, so the bug survives release after release while quietly costing you players.
Automatic error capture closes that gap. Each failure arrives with its full stack trace, the device and OS, the build number, and a breadcrumb trail of what the player did right before it broke, so even a failure you have never seen becomes a specific, reproducible issue. Fold identical failures into one signature ranked by how many players each hits, and your worklist sorts itself worst-first instead of arriving as a stream of vague complaints.
This is where a tool like Bugnet earns its place. Its SDK captures every HTML5 error automatically with the full stack trace plus device, OS, memory, build, and game-state context, folds duplicates into one grouped issue with an occurrence count, and ties each to the build it first appeared on — so you fix the problem that hurts the most players first and confirm it is gone when its signature disappears from the next release.
Most of the time the fix is small. Seeing the failure clearly is the part that actually costs you.