Reputation: 692
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.
Upvotes: 17
Views: 23458
Reputation: 121
This worked for me:
require('dotenv').config({path:'my-app/.env'});
I right-clicked on the .env file to get the relative path.
Upvotes: 12
Reputation: 1499
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()
.
Upvotes: 2
Reputation: 489
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.
Upvotes: 1