Fix the 'Parse failure: Unterminated string constant' error

By

How to fix the Parse failure Unterminated string constant error in Astro on Windows by renaming a parent folder with odd characters to plain ASCII.

~~~

The “Parse failure: Unterminated string constant” error means the JavaScript parser found a string that was opened but never closed. Usually the cause is a quote problem in your code. Sometimes, as I found out, the cause is much weirder: a strange character in a folder name.

The usual causes

Most of the time this error points at a real syntax problem. An unclosed quote:

const message = 'hello

The parser reaches the end of the line and never finds the closing quote.

A regular string can’t span multiple lines either. If you need a line break inside a string, use a template literal:

const message = `hello
world`

Another sneaky one: smart quotes. If you paste code from a blog post or a Word document, the closing quote might be a curly instead of a straight '. The parser doesn’t recognize it as a string delimiter, so the string never terminates. Retype the quotes in your editor and the error goes away.

So the first thing to do is open the file mentioned in the error message and look at the reported line.

When your code is fine

I ran into this error with a student of mine, running Astro on Windows (could not replicate on macOS), after running npm run dev.

We stared at the code for a long time. Nothing was wrong with it. The same project worked on my machine.

After much 🤔 we solved by renaming the parent folder, which apparently had a strange character, perhaps non-ASCII.

My best explanation is that dev tools embed absolute file paths into the code they generate. An unusual character in the path can end up inside one of those generated strings and confuse the parser. I can’t prove that’s the exact mechanism, but the fix was consistent: new folder name, error gone.

The fix

If you run into this problem and your code looks correct, look at the full path of your project. Check every folder in it, not just the project folder itself.

Try renaming the folder you are running (or perhaps a parent folder in the path) to just ASCII text, like “test”. No accents, no emoji, no special symbols. Then run npm run dev again.

~~~

Related posts about js: