API key not recognized on deployment, even after declaring the variable (Project type: NodeJS/React)
Hello!
It's my first time posting here. I'm trying to fetch pubmed articles for my personal website and I'm running a NodeJS/React app for that purpose. To fetch the articles, i'm using the Pubmed API and used a variable like this =>
const apiKey = process.env.YOUR_API_KEY;
.
I saved my API key in Railway as an environment variable, but I realized that it wasn't read by the CI somehow and is shown as undefined
. Using the API key locally (in the non-secure way) worked well, and my main issue is linked with the environment variable declaration...
When checking the content of process.env
, I realized there isn't any environment variable as shown here:
I might be fooled by the inspector/console log though.
Note that before using Railway, I had the exact same issue with Netlify 😓
Project ID: 4d7cb82a-87a2-496e-b702-70b5ed733e7d
If you have questions in order to clarify the issue, feel free to ask! I'll do my best to answer them! 😁
Thank you by advance 🙏
PS: I think it's possible that's it's a duplicate of that thread. Apologies by advance .https://discord.com/channels/713503345364697088/1110912233980051508/1111059762554085497 ...8 Replies
Project ID:
4d7cb82a-87a2-496e-b702-70b5ed733e7d
Is the deploy failing at your CI, and not making it to Railway? Or is it failing on railway?
The deployment and build succeeds on Railway (same on Netlify), but the website doesn't have the expected behavior (meaning that the articles are not fetched nor displayed in the website).
Hmm… okay. I don’t do too much with node/react, so I’m kinda trying to figure it all out with ya.
Not to be an ass, but you are sure you are calling the right env variable name in your code?
I know I’ve don’t that numerous times where I named it different in production
There's no problem! I think that's the most natural question lmao!
The env variable name is
YOUR_API_KEY
and I literally copypasted it and trimmed the potential space characters so i would bang my head against the wall if it was that 😭I hate to say it… I’m at a loss… maybe someone else with more node experience can tag in and help
No problem, thank you for trying to look at it anyways! 😄 (and it can happen to anyone, don't worry!)
So, I found the solution to my Issue: The
YOUR_API_KEY
should have been like that => REACT_APP_YOUR_API_KEY
(and MUST be declared like that in both the code + the dashboard/env file otherwise it isn't recognized) That should solve that issue.
For more documentation : https://create-react-app.dev/docs/adding-custom-environment-variables/Adding Custom Environment Variables | Create React App
Note: this feature is available with [email protected] and higher.