A broken JavaScript file is almost always a syntax error, a bad reference to a missing dependency, an encoding problem, or a file that was truncated or modified mid-download. Start by capturing the exact error and its location, reproduce it in isolation, then fix the cause rather than patching symptoms. The one condition that changes everything is whether the file fails to parse at all: a parse error stops the whole module before any of its logic runs, while a runtime error means the code loaded and failed later. Knowing which one you have determines whether you read the parser's line number or trace a call stack.

This guide is written for the ordinary case: a .js file that used to work, or one you received from someone else, that now throws errors, behaves unpredictably, or refuses to load. It covers browser and Node.js contexts, since the same file can break in one and work in the other. It does not cover TypeScript compilation internals or bundler-specific plugin bugs beyond where they intersect with a plain JavaScript file.

Identify What "Broken" Actually Means Before Touching Code

The word "broken" hides at least five different failures, and each has a different repair path. A file that throws SyntaxError: Unexpected token never executed. A file that throws ReferenceError: foo is not defined executed until it hit the missing name. A file that loads but produces wrong output has a logic or data problem, not a structural one. A file that returns a 404 or MIME-type refusal never reached the JavaScript engine at all. And a file that works locally but fails after deployment is usually a path, caching, or environment problem.

Sort your symptom into one of these buckets first. The error message and where it appears—browser console, terminal, build log, or server log—narrows it fast. If you see nothing in any console, the file probably isn't being requested; check the network panel or your server access log before reading a single line of code.

One useful distinction: parse-time versus run-time. V8, SpiderMonkey, and JavaScriptCore all report parse errors with a line (and often column) number, and they refuse to execute any of the file. Run-time errors give you a stack trace pointing at the calling chain. Treat the first as "the text is malformed" and the second as "the text is valid but the world doesn't match its assumptions."

Capture The Exact Error And Its Location

Before editing, record three things: the full error message including the error type, the file and line reported, and the input that triggered it. Copy the message verbatim into a scratch note. Error text is your ground truth, and paraphrasing it ("it says something about undefined") throws away the token name that usually points straight at the bug.

In a browser, open developer tools, go to the Console, and reload with the cache disabled. In Chrome or Edge that's the checkbox in the Network panel or Ctrl+Shift+R; in Firefox it's Ctrl+Shift+R as well. In Node.js, run the file directly with node path/to/file.js and read stderr. If the error only appears inside a larger app, isolate with the smallest command that still reproduces it.

Note the difference between the reported location and the real cause. Minified or bundled files report positions in the bundle, which map back to source only if source maps exist. Files served with a stale cache can report errors from code you already fixed—hard-reload and, if needed, disable caching in the dev tools before you trust any line number.

A quick success check here: you can state the error type, the exact token or identifier named, and the file it came from. If you can't, keep capturing; you're not ready to edit.

Check The File Itself For Truncation, Encoding, And Line Endings

A surprising share of "broken" JavaScript files are damaged files rather than bad code. Download interruptions, FTP transfers in text mode, copy-paste through rich-text editors, and git merge artifacts all corrupt the bytes while leaving something that looks like JavaScript.

Prerequisites: a terminal and the original or a known-good copy for comparison. Actions, in order:

1: Check the file size against the source. A file that should be tens of kilobytes but is a few hundred bytes was truncated. 2: Look at the last lines. A complete file often ends with a closing brace, an export, or a newline; a truncated one stops mid-token, often mid-string. 3: Search for conflict markers: <<<<<<<, =======, >>>>>>>. Any hit means an unresolved merge and the file cannot parse. 4: Check encoding. JavaScript source should be UTF-8. A file saved as UTF-16 shows up as spaced-out or garbled characters, and a byte-order mark at the start can cause a parse error in some toolchains. On the command line, file path/to/file.js reports the detected encoding on most Unix-like systems. 5: Check line endings. Windows CRLF is legal in JavaScript, but stray carriage returns inside string literals or pasted code can produce odd token errors. Normalizing to LF removes a whole class of confusion. 6: Look for invisible characters: non-breaking spaces (U+00A0), zero-width spaces, and smart quotes pasted from a word processor. These are the classic cause of "Unexpected token" on a line that looks perfectly fine.

If any of these checks fail, re-obtain the file from a trustworthy source rather than repairing it by hand. Re-downloading or restoring from version control is faster and safer than reconstructing a corrupted file.

Reproduce In Isolation To Separate The File From Its Environment

A file can be perfectly valid and still fail because of how it's loaded. Isolating means running the file with the fewest possible dependencies and the simplest possible host.

For a browser file, create a minimal HTML page that loads only that script with