dotenv
https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png
使い方
.envファイルに纏めて定義
code:.env
NODE_ENV=development
code:app.js
require('dotenv').config();
// process.envで読み込める
console.log(process.env.API_URL)
import from 'dotenv/config';
オプションは、環境変数で渡す
DOTENV_CONFIG_DEBUG
DOTENV_CONFIG_PATH
インストール
npm i dotenv
オプション
path
環境変数ファイルの場所指定
debug
デバッグできる
code:debug.js
require('dotenv').config({
path: 'src/env'
debug:true
})
注意
require('dotenv').config(); を早めに呼ぶ
一番上に配置しておくのが良い。
gitに入れないこと
変数の中身消した.env.exampleでも入れておく