How can I add .env variables to the app.json file

I am using .env variables throughout my project, but would find it really useful to be able to inject them into the app.json file is there a way to achieve this?

thanks

1 Like

Maybe you could do something like this:

Write a script (e.g. gen_app_json) that takes an app.json template, adds in the environment variables, writes the real app.json and then runs the rest of the args.

e.g. if you run it like gen_app_json expo build:android it would generate app.json and execute expo build:android

Then for convenience you could add/update the scripts section in package.json to call gen_app_json expo start instead of expo start etc.

Thanks for your comment but it is far too general for me. When you say write a script, what do you mean, in what language - javascript? Then “add in the environment variables”, how would I do that. Furthermore, if I can add in environment variables to the app.json file anyway, why would I have to generate my own app.json file with a special script?

thanks

app.json is just JSON, so any language that can read environment variables and produce JSON output could create an app.json based on the environment variables. But based on your last comment I’m not sure what you’re trying to accomplish.

Could you be more specific about what environment variables you want to add into app.json and how you want to use them after they’ve been added there?

We do this all the time using nodejs scripts we run from the terminal. The jsonfile library is great for this- reads in the JSON as an object, then you can edit it and write it back to app.json.

So could I do this as part of the expo start process? perhaps by editing the package,json file?

Yep, you can work it into one of the scripts in the scripts section, like:

"start": "node updateAppJson.js; expo start"

then yarn start will do both of those things.

ok cool, i’ll give this a go