如何添加 .env 文件的路径并使用它?
我想将路径放入此处:
const serviceAccount = require("./service_account.json");
放入 .env 文件中,如下所示:
PATH="./service_account.json"
并得到如下所示:
require('dotenv').config();
const serviceAccount = require(process.env.PATH);
错误:
Error: Cannot find module 'C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\dotnet\;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\Program Files (x86)\LINQPad5;C:\Program Files\nodejs\;C:\Users\santo\AppData\Local\Microsoft\WindowsApps;C:\Users\santo\.dotnet\tools;C:\Users\santo\AppData\Local\Programs\Fiddler;C:\Users\santo\AppData\Roaming\npm'
Require stack:
- C:\internal_bestmposlite-dashboard\BestMPOS-Lite\list_user_data.js
I wanted to put the path in here:
const serviceAccount = require("./service_account.json");
into a .env file like this:
PATH="./service_account.json"
and getting the like this:
require('dotenv').config();
const serviceAccount = require(process.env.PATH);
error:
Error: Cannot find module 'C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\dotnet\;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\Program Files (x86)\LINQPad5;C:\Program Files\nodejs\;C:\Users\santo\AppData\Local\Microsoft\WindowsApps;C:\Users\santo\.dotnet\tools;C:\Users\santo\AppData\Local\Programs\Fiddler;C:\Users\santo\AppData\Roaming\npm'
Require stack:
- C:\internal_bestmposlite-dashboard\BestMPOS-Lite\list_user_data.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做的最简单的事情就是更改变量的名称。
使用诸如
path
之类的内容代替PATH
。或者
File
、FILE
等。env 文件中的
PATH
变量不会显示您在.env 文件。
这是一个示例:
.env
文件index.js
文件输出:
The easiest thing you can do is change the name of the variable.
Use something like
path
instead ofPATH
.Or
File
,FILE
, etc.the
PATH
variable in env files will not show what you put inside of the.env
file.Here is an example:
.env
fileindex.js
fileOutput:
在根文件夹中创建 .env 文件并将所有变量放入 .env 文件中。
您还可以给出 .env 文件的相对路径,如下所示:
Create .env file at root folder and put your all variables inside .env file.
You can also give relative path of .env file like that: