[英]Environment variables in NodeJS depenency
Environment variables in NodeJS depenency NodeJS依赖中的环境变量
I have a NodeJS application which has a dependency of mine我有一个 NodeJS 应用程序,它具有我的依赖项
my-base-module: git+https://myuser:mytoken@gitlab.com/organization/my-base-module.git#v1.0.0
I am also using the dependency dotenv
, which in development I use the file .env
and in other environments I pass the variables through docker environment variables我还使用依赖项
dotenv
,在开发中我使用文件.env
,在其他环境中我通过.env
环境变量传递变量
The problem I have, and I do not know why and how to solve it is that in my-base-module
I do not share the same environment variables (and I need it to).我遇到的问题,我不知道为什么以及如何解决它是在
my-base-module
我不共享相同的环境变量(我需要它)。 It is like the variables loose the reference就像变量失去了引用
require('dotenv').config(); require('dotenv').config(); const env = process.env.NODE_ENV;
const env = process.env.NODE_ENV;
for example in that bit of code the NODE_ENV
variable inside my-base-module
is undefined.例如,在那段代码中,
my-base-module
的NODE_ENV
变量未定义。 However in the container is defined and with the right value但是在容器中定义并具有正确的值
Update 1更新 1
I am requiring dotenv
in both my-application
and my-base-module
.我在
my-application
和my-base-module
中都需要dotenv
。 If I enter to the container and I do $ echo $NODE_ENV
.如果我进入容器并执行
$ echo $NODE_ENV
。 I get production
.我得到
production
。
If my application process.env.NODE_ENV
also holds production.如果我的应用程序
process.env.NODE_ENV
也持有生产。 But in the dependency process.env.NODE_ENV
is undefined
但是在依赖
process.env.NODE_ENV
是undefined
I will try to do some github repositories to reproduce it我会尝试做一些github存储库来重现它
I was wrong, this works I made this small POC with these public repositories我错了,我用这些公共存储库制作了这个小 POC
https://github.com/agusgambina/try-base-module..https://github.com/agusgambina/try-application-dotenv https://github.com/agusgambina/try-base-module..https://github.com/agusgambina/try-application-dotenv
https://github.com/agusgambina/try-application-dotenv https://github.com/agusgambina/try-application-dotenv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.