dotenv文件未加载环境变量 [英] dotenv file is not loading environment variables

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

问题描述

我在根文件夹文件

NODE_ENV=development
NODE_HOST=localhost
NODE_PORT=4000
NODE_HTTPS=false
DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=user

文件夹中的

server.js文件. server.js文件的第一行是

And server.js file in the root/app/config/server.js folder. The first line of server.js file is

require('dotenv').config();

我还尝试了以下操作:

require('dotenv').config({path: '../.env'});

require('dotenv').config({path: '../../.env'});

但是,当我运行server.js文件时,不会加载我的env变量 从命令提示符

However, my env variable are not loaded when I run the server.js file from command prompt

node root/app/config/server.js

如果我使用 visual studio 并按 F5 ,它将加载!

If I use the visual studio and press F5, it loads!!

我不确定我在做什么错,我想念什么. 任何建议都将受到高度赞赏.谢谢.

I'm not sure what I'm doing wrong, what I'm missing. Any suggestion is highly appreciate. Thanks.

推荐答案

使用require('dotenv').config({path:__dirname+'/./../../.env'})如何?

您的问题似乎是执行路径.

Your problem seems to be the execution path.

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

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