Why We Can’t Use dotenv for Babel

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Environment Variables for Web Developers course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Environment Variables for Web Developers, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Environment Variables for Web Developers

The fourth module is about using .env with React Native. React Native does not use JavaScript bundlers, like Webpack, to compile, as it has its own way to build native mobile components. So you cannot use .env as you normally do for React Native. However, you can inject environment variables using Babel. In this lesson, I'm just going to introduce some concepts that will come up before we do the demo for the next lesson. Unlike Webpack, which facilitates building bundles in using a script, Babel only takes configuration and plugins. Libraries like .env must be wrapped in a Babel plugin to inject the environment variables in a way that is configurable. The easiest way to do this is using React Native.env. There also is a library called React Native Config, which is useful if you're trying to inject environment variables directly into your custom native components as in beyond JavaScript. However, my recommendation, even still, is to try avoid this as you then have to make sure the linking works for each React Native environment. You can always load values into JavaScript and then pass them down to your custom native components. I'm actually maintainer of React Native.env, which is a Babel plugin. So I'll go over its use, and it's very, very troubleshooting problems. If you are using Expo, you can also use Expo environment variables. As long as you don't need .env flow strategies on it, like development versus production configurations. If you want to play around with Expo without setting up your whole environment, there is an online IDE called Snack. However, it does not allow you to install Babel plugins, like React Native.env, as Dev Dependencies. You'll have to play around with this locally, unfortunately. If you're using the default bundler, you can use Babel plugin transform in-line environment variables, which is great. And this is what React Native.env is modeled off of. But again, it doesn't let you use .env flow. So is this library for more than just React Native? Theoretically, it should work with anything that pre-compiles with Babel. I was asked about if other native bundlers, like Native Script, work the same way. And actually, Native Script works with Webpack. So no, it does not need a special Babel plugin. And since we're using Babel as a pre-compiler, we will have to be weary of a few things. For one, we have to be weary of a second-end-name variable, Babel.env. It should be the same as Node.env, but it is frequently overwritten, both Expo and React Native toolchains overwrite this value. After we build our React Native app, we have to open the native publisher. So Android Studio or Xcode. At that time, you can run the build app from that IDE. However, when you run from the IDE, the app will not change, and environment variables will not read up late. In other words, you have to rebuild your React Native app every time you change your end settings. All right, anything you should look out for? The inconsistency of build tools. There are a number of automated build tools out there that support React Native environment variables, such as BitRise. BitRise allows you to set your environment variables in a dashboard, like in previous modules. Stay away from tools like CodeMagic, which dangerously force you to commit environment variables to Git. Even though they're encrypted, it still poses a lot of risks. And with that, let's get into the demo.