&quot“ dotenv.load()不是函数”在尝试运行节点脚本的同时
我正在尝试复制导入脚本,以将我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基于 npm文档您应该使用dotenv.config()。
Based on NPM documentation you should use dotenv.config().
这对我有用:
我在.env文件上右键单击以获取相对路径。
This worked for me:
I right-clicked on the .env file to get the relative path.
也许这取决于.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 runnode 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 todotenv.config()
.要加上 @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 thepath
package from the nodejs standard library to resolve paths to ensure you're getting the correct path to the file.