DEV Community

Cover image for Local and Production ENV Variables using Expo React Native
Cormac
Cormac

Posted on

3 1

Local and Production ENV Variables using Expo React Native

Expo recently came out with a new feature which makes managing ENVs a lot easier using what they call a dynamic app config file.

*Here’s you to setup env variables for different environments in Expo:
*

  1. Create and app.config.js file at the root of your repo
  2. Populate the file, it should look something like this:

import 'dotenv/config';

export default {
name: 'APP_NAME',
version: '1.0.0',
extra: {
environment: 'dev'
},
};

  1. Call variable. Now when we run our application, we can access the environment variable by doing the following:

import Constants from 'expo-constants';
export default function App(props) {
console.log("props.environment: ", props.environment)
}
App.defaultProps = {
environment: Constants.manifest.extra.environment,
};

  1. Setup different variables for production and local development. To do this, we’ll need to change out deploy script to manually add a variable describing the environment.

I place mine in a Dockerfile which Google Cloud uses to build my application:

RUN ENVIRONMENT=prod expo build:web

But you you add the ENVIRONMENT=prod to wherever you are building your app.

  1. Update the app.config.js to make variables dynamic based on the environment.

import 'dotenv/config';
if (process.env.ENVIRONMENT === 'prod') {
export default {
name: 'APP_NAME',
version: '1.0.0',
extra: {
environment: 'prod'
},
};
} else {
export default {
name: 'APP_NAME',
version: '1.0.0',
extra: {
environment: 'dev'
},
};
}

Here we check the ENVIRONMENT variable and then we can update the extra variables based on where we are using our app. When we deploy our app will use variables defined in the top section.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay