๐ What you'll see
process.env.API_KEY is undefined
๐ค Why it happens
A .env file isn't read automatically. Each framework has its own naming rules (for example, variables used in the browser need a specific prefix), and you have to restart the server after changing a value. The .env file isn't uploaded to your host either, so you have to add the values in your hosting service's settings separately.
๐ ๏ธ How to fix it
- Make sure the .env file is in the project's top folder.
- Check your framework's docs for its environment variable naming rules.
- If you changed a value, stop and restart the dev server.
- If it only fails on your deployed site, add the same values in your host's Environment Variables settings.
- Never put secret keys in variables that are exposed to the browser.
๐ค What to ask your AI
Replace the parts in [ ] with your own details, then paste it in.
In [framework name], values from my .env come out undefined. The file's location and contents are: [description]. Tell me how to set this up both for development and for deployment.
Other errors
CORS errorThe browser blocks your page from fetching data from another site404 Not FoundThe address or file can't be foundundefined / Cannot read properties of undefinedReading a property of a value that isn't thereis not definedUsing a name that was never declaredSyntaxErrorBrackets or quotes don't match upExposed API keyYou put a secret key in your code or on GitHubMixed ContentAn https page loads something from an http addressBlank white pageYou deployed it and all you see is a blank screenis not a functionCalling something that isn't a functionUnexpected token < in JSONYou expected JSON but got HTMLCannot use import statement outside a moduleThe browser won't accept your import statementfavicon.ico 404The console shows a 404 for favicon.icoData disappears when I refreshYour to-dos or scores aren't being savedPage looks broken or tiny on phonesFine on a computer, weird on mobilegit push rejectedA "rejected" / non-fast-forward errornpm: command not foundYour terminal says npm or node doesn't existPort 3000 is already in useYour dev server says the port is takenThe AI keeps failing to fix the same errorYou ask for a fix and nothing changes