R
Railway2y ago
eiger

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:
Object { NODE_ENV: "production", PUBLIC_URL: "", WDS_SOCKET_HOST: undefined, WDS_SOCKET_PATH: undefined, WDS_SOCKET_PORT: undefined, FAST_REFRESH: true }
Object { NODE_ENV: "production", PUBLIC_URL: "", WDS_SOCKET_HOST: undefined, WDS_SOCKET_PATH: undefined, WDS_SOCKET_PORT: undefined, FAST_REFRESH: true }
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
Percy
Percy2y ago
Project ID: 4d7cb82a-87a2-496e-b702-70b5ed733e7d
MantisInABox
MantisInABox2y ago
Is the deploy failing at your CI, and not making it to Railway? Or is it failing on railway?
eiger
eiger2y ago
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).
MantisInABox
MantisInABox2y ago
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
eiger
eiger2y ago
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 😭
MantisInABox
MantisInABox2y ago
I hate to say it… I’m at a loss… maybe someone else with more node experience can tag in and help
eiger
eiger2y ago
No problem, thank you for trying to look at it anyways! 😄 (and it can happen to anyone, don't worry!)
eiger
eiger2y ago
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.
Want results from more Discord servers?
Add your server