LoginSignup
0
0

More than 1 year has passed since last update.

nodeで.envを作る

Posted at

node.jsで設定ファイルを一か所に集めたくなったので、
envファイルで設定値を保存したくなったので調べたら
dotenvっていうパッケージを見つけたので、
使ってみた。

dotenvのページ

 インストール

npm install dotenv

 .envファイルを作る

プロジェクトルートに.envファイルを作る

中身はこんな感じ

/.env
hoge = hogehogehogehoesetting

# 環境変数を定義コメントはこんな感じ
fuga = 設定値

使う

index.jsに以下を配置

require('dotenv').config();

他のファイルでは、読み込む必要は無い

設定値の読みだし

console.log(process.env.hoge) // hogehogehogehoesetting

って感じで、設定している文字列を取得できる

ちょう簡単

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