Mac下面的環境變量設置和Linux差不多,一般為這幾個文件(左邊的先加載):
/etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc
#打開~/.bash_profile,添加 export NODE_ENV=dev #保存退出后執行以下命令立即生效 source ~/.bash_profile
btw, 我發現如果使用node ./bin/www來啟動express的話,可以直接在程序中通過process.env.NODE_ENV讀取到環境變量。但是用pm2進程管理來啟動express則讀取不到,一定要用pm2的配置文件來設置環境,具體查閱http://pm2.keymetrics.io/docs/usage/application-declaration/#generate-configuration