I’m working on adding file upload capabilities to my app using Filestack, as described in the example-blog project. I configured the API Key with apikey={process.env.FILESTACK_API_KEY} and I got to a point where file upload is working fine locally, but when I push to Netlify I get an error saying that Error: An apikey is required to initialize the Filestack client. I did set the env var in Netlify correctly (I’ve checked a million times), but no luck.
I’ve googled this issue and everything seems to point to a missing Webpack config. I wanted first to check here if there’s a cleaner way to do this, or maybe there’s something I’ve missed.
FWIW, I started out with Redwood 3.x and have since upgraded to the latest (4.0).
Hmm, to confirm, you have create the Env Vars in Netflify deploy settings, correct? Here’s what ours looks like for the Example Blog (note: DB_HOST is the same thing as DATABASE_URL):
I don’t know that we actually used the file uploaded in production…the blog posts that are there come from the database seeds. It’s possible that the ENV var doesn’t work in production either.
Looking at the dotenv-webpack documentation, it seems like if we were to set the systemvars config to true (it defaults to false) it should fix the problem. The downside of this approach is that all system env vars would be available during the build process, which IMO is an OK tradeoff. What do you guys think ?
Oh, this just got super interesting! (yay/boo @danvalencia – this means you just found something not working that’s going to help us improve Redwood, but it’s a bit painful for you until we do. Thanks in advance for hanging in there along the way! )
As Daniel mentioned, Netlify has access to Env Vars at build time but does not do anything further to include in App unless explicitly configured to do so. From this topic on Netlify Forum:
So now you have the variables set and available to your build process, great! But, unless your build pipeline DOES something with the environment variable - it’s not going to be much use in the code that gets published and served to the browser - which doesn’t understand $API_ENDPOINT - that’s just a string to the browser and to our CDN.
That’s 100% the problem. We don’t want the web side of things to have access to your ENV VARS unless you specifically define them - This was an oversight on our part.
This is great, we can use EnvironmentPlugin | webpack, which adds a bit of syntactic sugar on top of the define plugin.
We just need a place to define them, maybe redwood.toml?
I need to hit the Could you create an Issue for this and link back here for reference? Quick read of docs looks like we can knock this out fast and/or get some community help to turn it around.
I can test locally, but the real test will be deploying a sample app to Netlify. Is there anything in place to have RW npm packages automatically published on merge to master to some “snapshot” registry ? Otherwise I can probably setup a private GH Packages registry to do this. Any ideas are welcome.
We don’t have a process/setup in place for a ‘snapshot’ registry (although this is a very interesting idea).
In this case, the changes are all Webpack specific. See the PR changed files here. So you might be able to just copy over the config to your App’s Webpack (documentation here).
I tested adding the env var to web.includeEnvironmentVariables in the redwood.toml file, as well as using an env var with the REDWOOD_ENV_ prefix, both worked well locally and in Netlify.