Javascript dotenv 文件未加载环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42335016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dotenv file is not loading environment variables
提问by Zammy Page
I have .env file at root folderfile
我在根文件夹文件中有 .env文件
NODE_ENV=development
NODE_HOST=localhost
NODE_PORT=4000
NODE_HTTPS=false
DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=user
And server.jsfile in the root/app/config/server.jsfolder.
The first line of server.jsfile is
和server.js文件root/app/config/server.js夹中的文件。server.js文件的第一行是
require('dotenv').config();
require('dotenv').config();
I also tried following:
我还尝试了以下操作:
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../../.env'});
require('dotenv').config({path: '../../.env'});
However, my env variable are not loaded when I run the server.jsfile
from command prompt
但是,当我server.js从命令提示符运行文件时,我的 env 变量没有加载
node root/app/config/server.js
node root/app/config/server.js
If I use the visual studioand press F5, it loads!!
如果我使用Visual Studio并按F5,它会加载!!
I'm not sure what I'm doing wrong, what I'm missing. Any suggestion is highly appreciate. Thanks.
我不确定我做错了什么,我错过了什么。任何建议都非常感谢。谢谢。
回答by Yonghoon Lee
How about use require('dotenv').config({path:__dirname+'/./../../.env'})?
怎么用require('dotenv').config({path:__dirname+'/./../../.env'})?
Your problem seems to be the execution path.
您的问题似乎是执行路径。
回答by DavidP
This solved my issues in Node v8.14.1:
这解决了我在 Node 中的问题v8.14.1:
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
Simply doing require('dotenv').config({path:__dirname+'/./../../.env'})resulted in a location that resolved as /some/path/to/env/./../../.env
简单地做require('dotenv').config({path:__dirname+'/./../../.env'})导致一个位置解析为/some/path/to/env/./../../.env
回答by zero_cool
Adding a little nuance to the answer above - if you are invoking dotenvfrom a nested file, and your .envfile is at the project root, the way you want to connect the dots is via the following:
为上面的答案添加一点细微差别 - 如果您dotenv从嵌套文件调用,并且您的.env文件位于项目根目录,则您想要连接点的方式是通过以下方式:
require('dotenv').config({path:'relative/path/to/your/.env'})
回答by Sasha Sachi
Here is a single-line solution:
这是一个单行解决方案:
require('dotenv').config({ path: require('find-config')('.env') })
This will recurse parent directories until it finds a .env file to use.
这将递归父目录,直到找到要使用的 .env 文件。
You can also alternatively use this module called ckeyinspired from one-liner above.
你也可以使用这个名为ckey 的模块,灵感来自上面的 one-liner。
.env file from main directory.
.env 文件来自主目录。
# dotenv sample content
[email protected]
PASSWORD=iampassword123
API_KEY=1234567890
some js file from sub-directory
来自子目录的一些js文件
const ck = require('ckey');
const userName = ck.USER; // [email protected]
const password = ck.PASSWORD; // iampassword123
const apiKey = ck.API_KEY; // 1234567890
回答by Oleg Puzankin
One time I have got the same problem. Dotenv did not load .env file. I tried to fix this problem with a path config, to put .env file in a root folder, to put .env in the folder where the file is running and nothing helps me. Then I just trashed Node_modules folder, reinstall all dependencies and it works correctly
有一次我遇到了同样的问题。Dotenv 没有加载 .env 文件。我试图通过路径配置解决这个问题,将 .env 文件放在根文件夹中,将 .env 放在文件运行的文件夹中,但没有任何帮助。然后我只是删除了 Node_modules 文件夹,重新安装了所有依赖项并且它可以正常工作

