如何添加 .env 文件的路径并使用它?

发布于 2025-01-09 06:05:36 字数 1037 浏览 4 评论 0原文

我想将路径放入此处:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2025-01-16 06:05:36

您可以做的最简单的事情就是更改变量的名称。

使用诸如 path 之类的内容代替 PATH

或者 FileFILE

。env 文件中的 PATH 变量不会显示您在 .env 文件。

这是一个示例:

.env 文件

PATH=./service_account.js
path=./service_account.js

index.js 文件

require('dotenv').config();

console.log(`process.env.PATH ` + process.env.PATH);
console.log(`\n`)
console.log(`process.env.path ` + process.env.path);

const Function = require(process.env.path);

console.log(Function)

输出:

output

The easiest thing you can do is change the name of the variable.

Use something like path instead of PATH.

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 file

PATH=./service_account.js
path=./service_account.js

index.js file

require('dotenv').config();

console.log(`process.env.PATH ` + process.env.PATH);
console.log(`\n`)
console.log(`process.env.path ` + process.env.path);

const Function = require(process.env.path);

console.log(Function)

Output:

output

我要还你自由 2025-01-16 06:05:36

在根文件夹中创建 .env 文件并将所有变量放入 .env 文件中。
您还可以给出 .env 文件的相对路径,如下所示:

require('dotenv').config({path:'pathToENV'})

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:

require('dotenv').config({path:'pathToENV'})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文