17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ExpoとReactNativeで環境変数を設定する

Last updated at Posted at 2020-09-09

はじめに

ReactNative with Expo という構成で環境変数を設定しようと思ったら意外にハマったので備忘録

プラグインをインストール

色々な記事があって混乱しますが以下のプラグインが一番シンプルにできました。
babel-plugin-dotenv-import

$ npm install babel-plugin-dotenv-import

babel.config.js を修正

babel.config.js
module.exports = function(api) {
  api.cache(true);
  return {
    // pluginsに必要な情報を追記
    plugins: [
      [
        'dotenv-import', {
          'moduleName': '@env',
          'path': '.env',
          'blacklist': null,
          'whitelist': null,
          'safe': false,
          'allowUndefined': false,
        }],
    ],
    presets: ['babel-preset-expo'],
  };
};

.envファイルの作成

これはWEBの記法と何も変わりません。

.env
SAMPLE_EMAIL=sample-mail@sample.com
SAMPLE_URL=https://sample/sample

環境変数を読み込む

sample.js
import {SAMPLE_EMAIL, SAMPLE_URL} from '@env';

console.log(SAMPLE_EMAIL); // sample-mail@sample.com
console.log(SAMPLE_URL); // https://sample/sample
17
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?