[英]Environment Variables not loading to process.env in Nodejs
I'm building out a nodejs api and have setup the dotenv
package as a dev dependency to load the variables to process.env
on developer's local machines.我正在构建一个 nodejs api 并将
dotenv
包设置为开发依赖项,以将变量加载到开发人员本地机器上的process.env
。
Note that when I log in I use sudo -i
to operate as root
.请注意,当我登录时,我使用
sudo -i
作为root
进行操作。
My intent is that during deployment, environment variables would be set in my Ubuntu host under /etc/environment
, loaded directly in to the process, and then the app would just run for that configuration.我的意图是在部署期间,环境变量将在我的 Ubuntu 主机中的
/etc/environment
下设置,直接加载到进程中,然后应用程序将只针对该配置运行。
To do this, I have a line at the start of server.js
:为此,我在
server.js
的开头有一行:
if(process.env.NODE_ENV === 'development') {
logger.info("Loading dotenv for development environment")
require('dotenv').config();
}
And developers will be instructed to add an environment variable to their system for NODE_ENV
.并且开发人员将被指示为
NODE_ENV
添加一个环境变量到他们的系统中。
Now, in my Ubuntu EC2 instance I've setup the /etc/environment
to have the environment variables I want (note that NODE_ENV being 'dev' here is just to avoid running dotenv):现在,在我的 Ubuntu EC2 实例中,我已经设置了
/etc/environment
以拥有我想要的环境变量(请注意,这里的 NODE_ENV 是 'dev' 只是为了避免运行 dotenv):
PORT=MYPORT
NODE_ENV=dev
APP_SECRET_KEY='MYSECRET'
APP_DATABASE_LOGIN=MYLOGIN
APP_DATABASE_PASSWORD='MYPASS'
APP_DATABASE_HOST=MYHOST
APP_DATABASE_NAME=MYDB
APP_DATABASE_PORT=MYDBPORT
And when I reboot and run printenv
they are all populated per the file.当我重新启动并运行
printenv
它们都按文件填充。
I have setup pm2
to run my application directly from server.js
without any additional configuration because as I understand it, process.env
is populated automatically from environment variables.我已将
pm2
设置为直接从server.js
运行我的应用程序,无需任何额外配置,因为据我所知, process.env
是从环境变量自动填充的。
However, when I log the values from process.env, I just get null for everything:但是,当我从 process.env 记录值时,我只会为所有内容获取 null:
logger.info({
connectionConfig: {
host: process.env.APP_DATABASE_HOST
, login: process.env.APP_DATABASE_LOGIN
, port: process.env.APP_DATABASE_PORT
, databaseName: process.env.APP_DATABASE_NAME
}
});
Is there something wrong with the configuration as-is here?这里的配置有问题吗?
Note: Per the answer below, I had mistakenly setup my environment variables AFTER starting pm2, and as such pm2 caching was missing them注意:根据下面的答案,我在启动 pm2 后错误地设置了我的环境变量,因此 pm2 缓存丢失了它们
The issue is that pm2
caches the environment variables.问题是
pm2
缓存了环境变量。
You have to do:你必须要做:
# all apps
pm2 restart all --update-env
# specific app
pm2 restart {pid} --update-env
If for some reason that doesn't work, the documented way is:如果由于某种原因不起作用,记录的方式是:
pm2 reload ecosystem.json --update-env
我遇到了同样的问题,这是因为 Visual Studio 和 Visual Studio 代码中的集成终端,除非您在管理模式下运行编辑器,否则它们似乎无法访问任何这些变量。所以要解决这个问题,您只需要在 Amin 模式下启动你的编辑器
Make sure you have this code in app.js file确保您在 app.js 文件中有此代码
> const path = require('path'); require('dotenv').config({ path:
> path.join(__dirname, '.env') });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.