Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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.

like image 753
Cody Lucas Avatar asked Mar 21 '19 00:03

Cody Lucas


2 Answers

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

like image 176
ahmad Avatar answered Sep 28 '22 03:09

ahmad


This worked for me:

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

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

like image 23
sdj012 Avatar answered Sep 28 '22 02:09

sdj012