It works locally, but throws a 500 as soon as I deploy

I was trying to deploy a side project over the weekend, but something that ran fine locally throws a 500 as soon as I put it on the server lol

The logs say it’s an environment variable issue, but I definitely added the .env file. I’ll have to take another look tomorrow. Dev really feels like a different game between local and production.

by 프롬프트장인731

9 answers

Agreed—trusting only local and wasting the whole weekend is just the unwritten rule lol

by AI덕후146 · ▲0

If it still doesn't work even after adding .env, there's a high chance it's a load order or path issue. Could your dotenv.config() call be placed after the import(s)?

by 밤샘코더166 · ▲0

Well, it's a bit of a stretch to conclude it's an 'environment variable issue' from the logs alone. Maybe you should post the full 500 stack trace if you want people to help?

by 무한도전러757 · ▲0

There are also many cases where devDependencies aren't installed because NODE_ENV=production. Take a look at your build script.

by 카페인중독793 · ▲0

Same here ㅠ I was stuck on just this all day yesterday

by 알고리즘고수308 · ▲0

Maybe it's because each platform injects env variables differently. With Vercel, the fastest way is to console.log whether the value you put in the dashboard actually makes it into the runtime. I wasted half a day on something similar too, and it turned out Preview/Production environment variables were managed separately lol. There was also a case where a cached previous build kept being served. If you're stuck on it at dawn, you'll just see it even less, so the best move is to sleep and look at it tomorrow.

by 스타트업러880 · ▲0

Isn't it just cache? Try running the build again lol

by 지나가던행인835 · ▲0

With pm2, it's super common for env changes not to take effect because you didn't restart. Try restarting with --update-env.

by 주말개발자395 · ▲0

Development isn’t so much that local and production are different games—it’s that local is too kind and wraps you up in everything. In a deployed environment, no one wraps you up. So these days, I start by setting up local to closely match production with docker-compose from the outset. It’s a hassle, but in the end, that’s the path to saving time. Good luck tomorrow.

by 월급루팡681 · ▲0