[英]require process.env in node.js?
I have a .env
file and it has 我有一个
.env
文件,它有
ABC='abc'
and when I do process.env.ABC
in my app.js I can get the abc
value. 当我在app.js中执行
process.env.ABC
,我可以获得abc
值。 How do I require it to be use in my models' files? 我如何要求在模型文件中使用它? I do the same thing
process.env.ABC
in my models file, it got undefined. 我在我的模型文件中执行了相同的
process.env.ABC
,但未定义。 I assume I have to require it? 我想我必须要吗?
You can use dotenv to require
the .env
file and store in the a variable when you start your app. 您可以使用dotenv
require
.env
文件,并在启动应用程序时将其存储在变量中。 Once you have this, you can either pass it as a method argument or wrap your models in module.exports = (env) => { return myModel; }
一旦有了它,就可以将其作为方法参数传递,也可以将模型包装在
module.exports = (env) => { return myModel; }
module.exports = (env) => { return myModel; }
In express, it would be something like 在表达上,它会像
const env = require('dotenv').config({path:
'/custom/path/to/your/env'});
app.use((req, res, next) => {
app.env = env;
});
Now, you can access the env
by passing req
to the models and get
the env
using req.app.get('env')
现在,您可以通过将
req
传递给模型来访问env
,并使用req.app.get('env')
get
env
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.