3
0

More than 3 years have passed since last update.

【Express】環境変数を取得する方法

Posted at

プログラミング勉強日記

2021年2月9日
ネットで調べるとprocess.envを付けると環境編集を取得できるとあったが、それだけでは取得できなかった。その方法をまとめる。

失敗した環境変数を取得する方法

 process.envで環境変数を取得できる。

const env = process.env.変数名;
console.log(env);
実行結果
$ node app.js
undefind

dotenvで.envを読み込む

 dotenvは.envファイルを読み込んで、process.envに設定するモジュール。

1. dotenvをインストールする

$ npm install --save dotenv

2. 環境変数を取得する

app.js
require('dotenv').config();
const env = process.env.変数名;
console.log(env);
実行結果
$ node app.js
環境変数の値が表示される
3
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
3
0