åãã½ã¼ã¹ã³ã¼ããåãã¢ããªã±ã¼ã·ã§ã³ã§ãããããã¤ã«ãã£ã¦è¨å®ãç°ãªããã®ãããã
å種APIãã¼ãããã¼ã¿ãã¼ã¹ã®è¨å®ãªã©ãæ¬çªç°å¢ã¨éçºç°å¢ã§ã¯ç°ãªãè¨å®ã使ããã¨ãå¤ãã
ãããã£ããã®ããã¼ãã³ã¼ãã£ã³ã°ããã¹ãã§ã¯ãªãã
ã¾ããGitHubã§ã³ã¼ããå ¬éããå ´åãAPIãã¼ãªã©ã®æ©å¯æ å ±ãæ¸ãããã«ã¯ãããªãã
ãã®ãããªå ´åã«ä½¿ãã®ããç°å¢å¤æ°ã§ããã
ç°å¢å¤æ°ã®æ£ç¢ºãªå®ç¾©ã¯ç¥ããªãããã³ã¼ãã®å¤ããè¨å®ãè¡ãå¤æ°ãã¨ããæãã ãããã
ãããå©ç¨ãããã¨ã§ãAPIãã¼çãã³ã¼ãã«æ¸ããã«æ¸ãã
process.env
Node.jsã«ãããç°å¢å¤æ°ã¯ãprocess.env
ã¨ãããªãã¸ã§ã¯ãã«æ ¼ç´ãããã
ãã®ãªãã¸ã§ã¯ãã¯æåããåå¨ãã¦ãããäºããããã¤ãã®ç°å¢å¤æ°ãå
¥ã£ã¦ããã
ã³ãã³ãã©ã¤ã³ã§ç¢ºèªã§ããã
$ node > process.env
ããã¤ã表示ãããã¨æãã
ã¢ããªã±ã¼ã·ã§ã³æ¯ã«process.env
ã®ä¸èº«ã追å ãã¦å©ç¨ããã®ããåºæ¬çãªä½¿ãæ¹ã§ããã
ãªããèªåã§å®ç¾©ããç°å¢å¤æ°ã¯æ
£ç¿ã¨ãã¦ãNODE_***
ã¨ããå½¢å¼ã§å½åãããã¨ãå¤ãã
èµ·åæã«è¨å®
ãããããå
·ä½çãªä½¿ãæ¹ãè¦ã¦ããã
ã¾ãã以ä¸ã®ãããªã³ã¼ããç¨æããã
// key.js let key = process.env.NODE_KEY; console.log(key);
ç°å¢å¤æ°NODE_KEY
ãå©ç¨ãã¦ããã
ããã$ node key.js
ã§å®è¡ããã¨ãundefined
ã表示ããããNODE_KEY
ã«ã¤ãã¦å®ç¾©ãã¦ããªãã®ã§ãå½ç¶ã§ããã
ç°å¢å¤æ°ã®å®ç¾©æ¹æ³ã¯ããã¤ãããããæãç°¡åãªã®ã¯ãèµ·åæã«è¨å®ããæ¹æ³ã§ããã
以ä¸ã®ããã«ãkey.js
ãå®è¡ããã
$ NODE_KEY=2 node key.js
ããã¨ã2
ã¨è¡¨ç¤ºãããã
ç°å¢å¤æ°ãè¤æ°ããå ´åã¯ãå ¨ã¦æ¸ãã
// $ NODE_KEY=2 NODE_NAME=tom node key.js let key = process.env.NODE_KEY; let name = process.env.NODE_NAME; console.log(key); // 2 console.log(name); // tom
foreverã«ãããã¼ã¢ã³åã§ããåãããã«ä½¿ããã
$ ç°å¢å¤æ°ã®è¨å® forever start ãã¡ã¤ã«å
envify
èµ·åæã«ç°å¢å¤æ°ãè¨å®ããæ¹æ³ã¯ãbrowserify
ã§ãã³ãã«ãããã¡ã¤ã«ã§ã¯ä½¿ããªãã
// env.js const outer = require('./outer.js'); console.log(process.env.NODE_KEY); console.log(outer.num);
// outer.js const num = process.env.NODE_NUM; module.exports.num = num;
ãã®2ã¤ããã³ãã«ãã¦env-b.js
ã¨ãããã¡ã¤ã«ãä½ãããããå®è¡ãã¦ããä¸æããããªãã
$ browserify env.js -o env-b.js $ NODE_KEY=hoge NODE_NUM=9 node env-b.js undefined undefined
ãã³ãã«ãããã¡ã¤ã«ã§ãç°å¢å¤æ°ã使ãã«ã¯ãenvify
ã¨ããnpmã¢ã¸ã¥ã¼ã«ã使ãã°ããã
ããã使ã£ã¦ããã¡ã¤ã«ã®èµ·åæã§ã¯ãªããã«ãæã«ãç°å¢å¤æ°ãè¨å®ããã
$ npm install envify
以ä¸ã®ããã«ä½¿ãã
$ ç°å¢å¤æ°ã®è¨å® browserify å ãã¡ã¤ã« -t envify > ä½æãããã¡ã¤ã«
$ NODE_KEY=hoge NODE_NUM=9 browserify env.js -t envify > env-b.js $ node env-b.js hoge 9
dotenv
2018/7/14 追è¨
dotenv
ããå©ç¨ããããdirenv
ã«ã¤ãã¦æ¸ããã
direnv を使った環境変数の管理 - 30歳からのプログラミング
追è¨çµãã
ããã¾ã§ã«èª¬æããæ¹æ³ã§ã¯ããã¡ãã¡èµ·åæããã«ãæã«ç°å¢å¤æ°ãå
¥åããªãã¨ãããªãã
ãµã³ãã«ã®ããã«ç°¡åãªã±ã¼ã¹ãªããããããããªãããå®éã«ã¯ãã£ã¨è¤éã§åé·ã«ãªã£ã¦ãã¾ãã ããã
ãããé²ãããã«ãäºãå
¥åãã¦ãããç°å¢å¤æ°ãèªåçã«èªã¿è¾¼ã¾ããæ¹æ³ãããã
dotenv
ã¨ããnpmã¢ã¸ã¥ã¼ã«ã使ããã¨ã§ãå¯è½ã«ãªãã
$ npm install dotenv
äºåã«.env
ã¨ãããã¡ã¤ã«ãã«ã¼ããã£ã¬ã¯ããªã«ç¨æãã¦ããã¨ãããã«è¨è¿°ãã¦ãããå
容ãç°å¢å¤æ°ã¨ãã¦èªã¿è¾¼ããã¨ãåºæ¥ãã
ã³ã¼ãã«require('dotenv').config();
ã¨æ¸ããã¨ã§ãèªã¿è¾¼ããã
// .env NODE_KEY = hoge NODE_NUM = 9
// env.js require('dotenv').config(); const key = process.env.NODE_KEY; const num = process.env.NODE_NUM; console.log(key); console.log(num);
$ node env.js hoge 9
èµ·åãªãã·ã§ã³ãæå®ããå ´åã¯ããã¡ããåªå ãããã
$ NODE_KEY=option node env.js option 9
dotenvã®æ³¨æç¹
dotenv
ã使ç¨ããæ¹æ³ã«ã¯ãåé¡ç¹ã¨ãããã注æç¹ãããã
ããã¯ã.env
ãã¡ã¤ã«ã.gitignore
ããã®ãå¿ãã¦ãã¾ãã¨ãGitHubã«ããã·ã¥ããéã«ãªãã¸ããªã«ç»é²ããã¦ãã¾ããç°å¢å¤æ°ã®ä¸èº«ããããä¸ã«å
¬éããã¦ãã¾ããã¨ã§ããã
å人çã«ããããä¸çªæ©ã¾ããã
ãããªãã¹ãããªããã°ãããã¨ãããã¨ãªã®ãããããªããã人éã®æ³¨æåã«é ¼ãã®ã¯ã¨ã³ã¸ãã¢ããããªãããã¹ãã¼ãã§ã¯ãªãã¨æãã¦ãã¾ãã
ãã ããã£ã¨èª¿ã¹ãéãã§ã¯ãdotenv
ãå©ç¨ãããã®æ¹æ³ããã使ããã¦ãããããã
ç´°ãã使ãæ¹ã«ã¯ãããããã¨å·¥å¤«ã®ä½å°ãããã®ã ãããã©ã