&quot“ dotenv.load()不是函数”在尝试运行节点脚本的同时

发布于 2025-02-13 07:06:35 字数 520 浏览 1 评论 0原文

我正在尝试复制导入脚本,以将我的Firebase RTD数据传达给Algolia。尝试运行脚本时,它会失败,并说dotenv.load不是一个函数。

我在与index.js文件的目录中有.env文件。我已经尝试将.ENV文件移动,但这无济于事。这是index.js的开始代码:

const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');

//load values from the ./env file in this direcotry into process.env
dotenv.load();

//config firebase
firebase.initializeApp({
    databaseURL: process.env.FIREBASE_DATABASE_URL,
});

我该怎么办?在要求上使用.config()也无济于事。

I am trying to replicate the import script to get my Firebase RTD data to Algolia. When trying to run the script, it fails and says dotenv.load is not a function.

I have the .env file in the same directory as the index.js file. I have tried moving the .env file around but that doesn't help. Here is the beginning code for the index.js:

const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');

//load values from the ./env file in this direcotry into process.env
dotenv.load();

//config firebase
firebase.initializeApp({
    databaseURL: process.env.FIREBASE_DATABASE_URL,
});

What can I do? Using .config() on the requirement does not help either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

沉睡月亮 2025-02-20 07:06:35

基于 npm文档您应该使用dotenv.config()。

Based on NPM documentation you should use dotenv.config().

合久必婚 2025-02-20 07:06:35

这对我有用:

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

我在.env文件上右键单击以获取相对路径。

This worked for me:

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

I right-clicked on the .env file to get the relative path.

櫻之舞 2025-02-20 07:06:35

也许这取决于.env文件的路径,也取决于您执行脚本的方式。也许要运行的firebase命令(例如firebase servions - 仅函数)在路径透视图中运行node Index.js功能)。

换句话说,您需要确定主脚本的路径,然后确定.env文件的相对路径,并将此路径设置为dotenv.config()。

Maybe it depends on the path of .env file and also, the way you are executing the script. Maybe the firebase commands to run (e.g., firebase serve --only functions) is different of to run node index.js in the path perspective (if you are using a cloud function).

In other words, you need to determine path of your main script and, then, determine the relative path of your .env file to this and set this path to dotenv.config().

傲鸠 2025-02-20 07:06:35

要加上 @ahmad的答案, documentation 对于包装,您要使用dotenv .config()加载.env文件。另外,根据您拥有路径的位置,您将传递对象{path/path/to/your/file}。如果您需要从Nodejs标准库中的PATH软件包来解决路径以确保您获得文件的正确路径,则可能会有所帮助。

To add on to @ahmad's answer, the documentation for the package asks to have you use dotenv.config() to load your .env file. Additionally depending on where you have the path, you'd pass the object { path: /path/to/your/file }. It would possibly help if you required the path package from the nodejs standard library to resolve paths to ensure you're getting the correct path to the file.

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