Code Monkey home page Code Monkey logo

react-native-dotenv's Introduction

react-native-dotenv

Let you import environment variables from a .env file in React Native, don't need any native code integration.

CircleCI npm version npm downloads

Install

$ npm install react-native-dotenv --save-dev

Add the react-native-dotenv preset to your .babelrc file at the project root.

{
  "presets": ["react-native", "react-native-dotenv"]
}

If you haven't got .babelrc set up for React Native, remember to install babel-preset-react-native first.

$ npm install babel-preset-react-native --save-dev

Usage

Add your app configuration in an .env file.

API_KEY=lorem
ANOTHER_CONFIG=foobar

Now you can import it in your .js file.

import { API_KEY, ANOTHER_CONFIG } from 'react-native-dotenv'

ApiClient.init(API_KEY, ANOTHER_CONFIG)

How it works?

As you can see, it's implemented as a babel plugin. All referenced imported members are replaced as the values specified in the .env file.

The example above will get compiled as below.

ApiClient.init('lorem', 'foobar')

Can I use different .env settings for production ?

Yes, simply create a separate .env.production file and the default release process of react-native will pickup the right config.

iOS

You can use the Release configuration to launch the Simulator. (Only supported in RN v0.39+)

react-native run-ios --configuration Release

Android

  1. Command⌘ + M to launch the developer menu in Android emulator.
  2. Tap DevSettings.
  3. Toggle JS Dev Mode.

Benefits we got

  • It could find out error like importing an non-existing variable.

  • Zero native code integration required. (compared to react-native-config)
  • Given that we use the existing dotenv package to parse .env file, the same config file could be reused in nodejs environment.

Troubleshooting

Changes to .env file is not updated

Manually edit the file importing react-native-dotenv (either add a blank line or whitespace) will work.

Contact

David Chang @zetachang

LICENSE

MIT License, see LICENSE file for detail.

react-native-dotenv's People

Contributors

zetachang avatar levity avatar

Watchers

James Cloos avatar Colin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.