0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

direnvを使ってディレクトリごとに環境変数を柔軟に設定しよう!

Posted at

概要

direnvを使うことで特定のフォルダに指定した環境変数を柔軟に設定できます
今回はインストール方法、使用方法等について解説します

前提

  • Macおよびzshrcを使用する前提でインストール方法について解説しています

インストール

homebrewでインストールできます

brew install direnv

zshにhookを設定します

eval "$(direnv hook zsh)"

Oh my zshのプラグインが用意されているので以下のように記載できます

.zshrc
plugins=(... direnv)

環境変数の設定

対象のディレクトリに.envrcを作成し、以下のようにexportも含めた上で記述します

.envrc
export GCP_PROJECT=project_name
export FIRESTORE_COLLECTION_NAME=collection

direnv allowコマンドで環境変数を対象のディレクトリで使用できるようロードします

direnv allow
direnv: loading ~/project/app/.envrc
direnv: export +FIRESTORE_COLLECTION_NAME +GCP_PROJECT

以下のようにechoコマンドで環境変数が設定されていることが確認できれば成功です

echo ${GCP_PROJECT}
my-project

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?