Cody Lucas
Cody Lucas

Reputation: 692

"dotenv.load() is not a function" while trying to run a Node script

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

Answers (4)

sdj012
sdj012

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

Luiz Fernando da Silva
Luiz Fernando da Silva

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

mralanlee
mralanlee

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

LionKing
LionKing

Reputation: 536

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

Upvotes: 49

Related Questions