Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9309

いまさら聞けないprocess.env

$
0
0
はじめに Node.jsでパスワードや環境変数などを扱うとき、process.envを使いますよね。 prrocess.envについてきちんと理解していなかったので整理しました。 環境変数とは お使いのPCがMac/Linuxの場合、下記bashコマンドでPCの環境変数が見れます。 $ ENV 上記結果と、下記結果は同じものになります。 index.js console.log(process.env) $ node index.js process.envの使い方 dotenvをインストールしたあと、.envファイルを作ってその中に独自の環境変数を宣言することができます。 $ yarn add -D dotenv .env TEST_VARIABLE=hello!!! index.js require('dotenv').config(); console.log(process.env.TEST_VARIABLE) $ node index.js hello!!! 環境変数の上書き 先ほどの.envの中で宣言した環境変数は、下記のように上書きすることができます。 $ TEST_VARIABLE=bye!!! node index.js bye!!! $ export TEST_VARIABLE=byebye!!! $ node index.js byebye!!!

Viewing all articles
Browse latest Browse all 9309

Trending Articles