节点dotenv文件未为测试环境加载 [英] node dotenv files not loading for test env

查看:62
本文介绍了节点dotenv文件未为测试环境加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个dotenv文件,一个用于开发,另一个用于测试.

I have two dotenv files, one for development and another for test.

const dotenv = require('dotenv');

if (process.env && process.env.NODE_ENV) {
  dotenv.config({path: '.env.' + process.env.NODE_ENV});
} else {
  dotenv.config({path: '.env.development'});
}

const http = require('http');
const app = require('../src/app');

const port = parseInt(process.env.PORT, 10) || 8000;
app.set('port', port);

const server = http.createServer(app);
server.listen(port);

这是我的问题:

在我的情况下,服务器何时加载dotenv文件?如果我在 test env中运行,为什么我对那些process.env变量未定义?在我看来,该文件仅运行一次,当我更改NODE_ENV时,它不会更改要加载的文件.

When does server load dotenv files in my case? If I run in test env, why do I get undefined for those process.env variables? It seems to me this file only runs once, when I change NODE_ENV, it does not change which file to load.

简而言之:

我的开发dotenv正在工作,但是将其更改为 test dotenv

My development dotenv is working, but just having trouble when changing it to test dotenv

推荐答案

我应该有多个.env文件吗?

Should I have multiple .env files?

不.我们强烈建议您不要使用主要" .env文件和环境" .env文件,例如.env.test.您的配置应有所不同部署之间,并且您之间不应共享价值环境.

No. We strongly recommend against having a "main" .env file and an "environment" .env file like .env.test. Your config should vary between deploys, and you should not be sharing values between environments.

来自 dotenv文档

这篇关于节点dotenv文件未为测试环境加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆