Help us understand the problem. What is going on with this article?

dotenvで環境変数ではなく .env 使う | Node.js

$ npm install dotenv --save

プロジェクトのルートディレクトリに .env ファイルを作成します。

# これはコメント行
KEY1=VALUE1
KEY2=VALUE2

実装

require('dotenv').config();

if (typeof process.env.KEY1 == 'undefined') {
  console.error('Error: "KEY1" is not set.');
  process.exit(1);
}

console.log(process.env.KEY1);  //=> VALUE1
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away