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.
9 answers
Agreed—trusting only local and wasting the whole weekend is just the unwritten rule lol
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)?
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?
There are also many cases where devDependencies aren't installed because NODE_ENV=production. Take a look at your build script.
Same here ㅠ I was stuck on just this all day yesterday
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.
Isn't it just cache? Try running the build again lol
With pm2, it's super common for env changes not to take effect because you didn't restart. Try restarting with --update-env.
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.