react-native-dotenv

A Babel preset let you import application configs from .env file (zero runtime dependency)

Github星跟踪图

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": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"]
}

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

$ npm install metro-react-native-babel-preset --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 does it work?

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')

FAQ

Changes to .env file is not updated

Manually edit the file importing react-native-dotenv by either adding an empty line or whitespace will work.

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.

Can I have more than production & development environment configs?

Sadly, it's not available so far. One of the workaround is generating .env file before triggering RN's bundle script automatically using either shell script or your own custom build pipeline.

Contact

David Chang
@zetachang

LICENSE

MIT License, see LICENSE file for detail.

主要指标

概览
名称与所有者zetachang/react-native-dotenv
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2016-09-05 17:20:11
推送于2020-05-27 18:01:02
最后一次提交2019-02-01 19:52:58
发布数5
最新版本名称v0.2.0 (发布于 )
第一版名称v0.0.3 (发布于 )
用户参与
星数0.9k
关注者数11
派生数68
提交数52
已启用问题?
问题数60
打开的问题数37
拉请求数8
打开的拉请求数9
关闭的拉请求数8
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?