All Projects โ†’ goatandsheep โ†’ React Native Dotenv

goatandsheep / React Native Dotenv

Licence: mit
Load react native environment variables using import statements for multiple env files.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Dotenv

Dotenv Kotlin
๐Ÿ—๏ธ Dotenv is a module that loads environment variables from a .env file
Stars: โœญ 326 (+71.58%)
Mutual labels:  hacktoberfest, environment-variables, dotenv
Dotenv Java
๐Ÿ—๏ธ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: โœญ 72 (-62.11%)
Mutual labels:  hacktoberfest, environment-variables, dotenv
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: โœญ 18 (-90.53%)
Mutual labels:  dotenv, environment-variables
dotenv.net
A library to read .env files in a .NET Core environment
Stars: โœญ 126 (-33.68%)
Mutual labels:  dotenv, environment-variables
envfile
Parse and write environment files with Node.js
Stars: โœญ 42 (-77.89%)
Mutual labels:  dotenv, environment-variables
php-env
A small and fast .env loader for PHP
Stars: โœญ 19 (-90%)
Mutual labels:  dotenv, environment-variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: โœญ 73 (-61.58%)
Mutual labels:  dotenv, environment-variables
gconfigs
gConfigs - Config and Secret parser
Stars: โœญ 42 (-77.89%)
Mutual labels:  dotenv, environment-variables
Dotenv Flow
Loads environment variables from .env.[development|test|production][.local] files for Node.jsยฎ projects.
Stars: โœญ 537 (+182.63%)
Mutual labels:  environment-variables, dotenv
Environs
simplified environment variable parsing
Stars: โœญ 631 (+232.11%)
Mutual labels:  hacktoberfest, environment-variables
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: โœญ 1,022 (+437.89%)
Mutual labels:  environment-variables, dotenv
angular-cli-envvars
Example project for my article "Angular CLI and OS Environment Variables"
Stars: โœญ 56 (-70.53%)
Mutual labels:  dotenv, environment-variables
dotenv
Load .env files in crystal
Stars: โœญ 16 (-91.58%)
Mutual labels:  dotenv, environment-variables
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: โœญ 47 (-75.26%)
Mutual labels:  dotenv, environment-variables
exenv
Exenv makes loading environment variables from external sources easy.
Stars: โœญ 35 (-81.58%)
Mutual labels:  dotenv, environment-variables
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: โœญ 87 (-54.21%)
Mutual labels:  dotenv, environment-variables
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: โœญ 33 (-82.63%)
Mutual labels:  dotenv, environment-variables
checkdotenv
Verify environment variables presence for Node JS.
Stars: โœญ 12 (-93.68%)
Mutual labels:  dotenv, environment-variables
Sync Dotenv
Keep your .env in sync with .env.example
Stars: โœญ 393 (+106.84%)
Mutual labels:  environment-variables, dotenv
Dynaconf
Configuration Management for Python โš™
Stars: โœญ 2,082 (+995.79%)
Mutual labels:  hacktoberfest, environment-variables

react-native-dotenv CircleCI

Load environment variables using import statements.

npm version dependencies Status codecov XO code style Join the chat at https://gitter.im/pass-it-on/react-native-dotenv npm downloads

Installation

$ npm install react-native-dotenv

Breaking changes: moving from v0.x to v2.x changes both the setup and usage of this package. Please see the migration guide.

Many have been asking about the reasons behind recent changes in this repo. Please see the story wiki page.

Introduction

This babel plugin lets you inject your environment variables into your react-native environment using dotenv for multiple environments.

Usage

.babelrc

Basic setup:

{
  "plugins": [
    ["module:react-native-dotenv"]
  ]
}

If the defaults do not cut it for your project, this outlines the available options for your Babel configuration and their respective default values, but you do not need to add them if you are using the default settings.

{
  "plugins": [
    ["module:react-native-dotenv", {
      "moduleName": "@env",
      "path": ".env",
      "blacklist": null,
      "whitelist": null,
      "safe": false,
      "allowUndefined": true
    }]
  ]
}

Note: for safe mode, it's highly recommended to set allowUndefined to false.

.env

API_URL=https://api.example.org
API_TOKEN=abc123

In users.js

import {API_URL, API_TOKEN} from "@env"

fetch(`${API_URL}/users`, {
  headers: {
    'Authorization': `Bearer ${API_TOKEN}`
  }
})

White and black lists

It is possible to limit the scope of env variables that will be imported by specifying a whitelist and/or a blacklist as an array of strings.

{
  "plugins": [
    ["module:react-native-dotenv", {
      "blacklist": [
        "GITHUB_TOKEN"
      ]
    }]
  ]
}
{
  "plugins": [
    ["module:react-native-dotenv", {
      "whitelist": [
        "API_URL",
        "API_TOKEN"
      ]
    }]
  ]
}

Safe mode

Enable safe mode to only allow environment variables defined in the .env file. This will completely ignore everything that is already defined in the environment.

The .env file has to exist.

{
  "plugins": [
    ["module:react-native-dotenv", {
      "safe": true
    }]
  ]
}

Allow undefined

Allow importing undefined variables, their value will be undefined.

{
  "plugins": [
    ["module:react-native-dotenv", {
      "allowUndefined": true
    }]
  ]
}
import {UNDEFINED_VAR} from '@env'

console.log(UNDEFINED_VAR === undefined) // true

When set to false, an error will be thrown. This is no longer default behavior.

Multi-env

This package now supports environment specific variables. This means you may now import environment variables from multiple files, i.e. .env, .env.development, .env.production, and .env.test.

Note: it is not recommended that you commit any sensitive information in .env file to code in case your git repo is exposed. The best practice is to put a .env.template or .env.development.template that contains dummy values so other developers know what to configure. Then add your .env and .env.development to .gitignore. You can also keep sensitive keys in a separate .env.local (and respective .env.local.template) in .gitignore and you can use your other .env files for non-sensitive config.

The base set of variables will be .env and the environment-specific variables will overwrite them.

The variables will automatically be pulled from the appropriate environment and development is the default. The choice of environment is based on your Babel environment first and if that value is not set, your NPM environment, which should actually be the same, but this makes it more robust.

In general, Release is production and Debug is development.

Experimental feature

One thing that we've noticed is that metro overwrites the test environment variable even if you specify a config so we've added a way to fix this. Make sure to specify the config value as indicated in the wiki and make custom configs for alternative builds. However, if you still need this, such as for a staging / test environment, you can add the APP_ENV environment variable in the CLI. For example:

// package.json
{
  "scripts": {
    "start:staging": "APP_ENV=staging npx react-native start",
  }
}

The above example would use the .env.staging file. The standard word is test, but go nuts.

TypeScript

Option 1: easy mode

Install the @types package npm version

npm install @types/react-native-dotenv

Set the moduleName in your Babel config as react-native-dotenv.

{
  "plugins": [
    ["module:react-native-dotenv", {
      "moduleName": "react-native-dotenv"
    }]
  ]
}

Import your variables from react-native-dotenv:

import {API_URL} from 'react-native-dotenv'

console.log(API_URL)

Option 2: specify types manually

  • Create a types folder in your project
  • Inside that folder, create a *.d.tsfile, say, env.d.ts
  • in that file, declare a module as the following format:
declare module '@env' {
  export const API_BASE: string;
}

Add all of your .env variables inside this module.

  • Finally, add this folder into the typeRoots field in your tsconfig.json file:
{
...
    "typeRoots": ["./src/types"],
...
}

Reference Material

Caveats

When using with babel-loader with caching enabled you will run into issues where environment changes wonโ€™t be picked up. This is due to the fact that babel-loader computes a cacheIdentifier that does not take your environment into account.

You can easily clear the cache:

rm -rf node_modules/.cache/babel-loader/*

or

yarn start --reset-cache

or

expo r -c

Maybe a solution for updating package.json scripts:

"cc": "rimraf node_modules/.cache/babel-loader/*,",
"android": "npm run cc && react-native run-android",
"ios": "npm run cc && react-native run-ios",

Or you can override the default cacheIdentifier to include some of your environment variables.

The tests that use require('@env') are also not passing.

Credits

If you'd like to become an active contributor, please send us a message.

Miscellaneous

    โ•šโŠ™ โŠ™โ•
  โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
 โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
 โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
  โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
   โ•šโ•(โ–ˆโ–ˆโ–ˆ)โ•โ•
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].