简体   繁体   English

NodeJS依赖中的环境变量

[英]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-moduleNODE_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-applicationmy-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_ENVundefined

I will try to do some github repositories to reproduce it我会尝试做一些github存储库来重现它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM