The Bug I Spent 3 Days on Turned Out to Be a Typo
On a side project, API responses kept coming back 401, so I tore through everything from the middleware to the token expiration logic. It worked in Postman but not in the browser. I figured it might be CORS, so I messed with the headers too.
In the end, the cause was a typo in a variable name in the env file. I had written NEXTPUBLIC_API_URL instead of NEXT_PUBLIC_API_URL. Since the prefix wasn't recognized, it was being bundled into the client as undefined.
If the error message had been more helpful, I wouldn't have lost 3 days... We've all dealt with an env typo at least once, right? lol
9 answers
Well, that's a bit... I don't get how it took you three days. If you'd just logged it to the console and taken one look at the request headers, you'd have found it right away. Before you start tearing apart the middleware, get into the habit of checking the Network tab first.
Got a source? No, calling this a variable-name typo is a stretch lol
I ran into exactly the same thing. I forgot the VITE_ prefix in Vite, and it just got passed in as undefined and failed silently. I really wish frameworks would show a warning for this. It's something they could check at build time.
Environment variables are like identity verification. Get one name wrong and it silently won’t match lol
This is so true lol — if error messages were more helpful, they’d save developers three days.
But isn't this less an env typo and more a failure to understand the prefix rule? If they'd known why NEXT_PUBLIC is needed, they would've been mindful of it when naming the variable. It also looks like it's partly because they didn't read the docs.
Ah, I totally relate... with env, one typo really can burn 3 days. But the funny-sad part is, I get that a 401 shows up, but why it shows up is the problem. If the prefix doesn’t get applied, it goes through as undefined, and why that shows up as a 401 is the trap.
Ah seriously, this 😭 I had the opposite issue—it still didn’t work even with the prefix, so I checked and found both .env.local and .env, which made the precedence confusing. env file precedence is absolute hell. Another mistake I make often is changing an env var and not restarting the server. But the most unfair part is that Postman works lol
LOL, this is a universal rule for real. I’ve also lost a whole day because I forgot NEXT_PUBLIC.