How to run node app from sub-directory with .env variables of that sub-directory? - node.js

I have a Node.js App called app.js and its stored in the directory B.
To run this script correctly it needs enviroment variables. These are stored in a file called .env, which is also in directory B.
In my app.js the env-variables are loaded via require("dotenv").config();
and I can then access them with e.g. process.env.SOME_VAR
So if I am currently located in directory B, I can just use node app and my app will execute just fine.
But if I go to the parent directory A and try to run my app via
node ./B/app
it will not execute, because it seems to have no access to the enviroment variables of the .env file.
So, my question is, how can I run my script from its parent folder, if I want to keep the .env file in the same directory?

You could use dotenv to load the the content of the environment variable i.e.
const dotenv = require('dotenv');
// ...
// Then go ahead to load the .env file content into process.env
dotenv.config({ path: '/full/custom/path/to/your/env/vars' });

you can set path using {path : '../.env'}.
require('dotenv').config({path : '../.env'});

Brilliant! it worked for me, I put my .env inside /vars folder
and used this line
require('dotenv').config({path : 'vars/.env'});
Instead of this line
require("dotenv").config();

Related

Is there a way to use environment variable in another directory in a node.js app

I have a project with folder structure is as follows,
Root > client, server
I want to use the environment variables form the .env file of my Root directory in the /Server/src, But when I try to import it in /Server/src the environment variables are undefined.
Is there a way to use those environment variables in the Server directory?
// from Root directory
const dotenv = require("dotenv");
dotenv.config();
module.exports = {
NODE_ENV: process.env.NODE_ENV,
}
// from Server directory
const { NODE_ENV,} = require("./config");
console.log(NODE_ENV); //value returns undefined
I see what you're trying to do, but unless the file path is "./config" then it won't work. You said it is in the root directory, so likely you are not using the right path. There will need to be at least one ../ to get back out of the server directory and into the root directory to access the file you're exporting from.
That being said, why bother? You should just require dotenv in any file you need to access the environment variables. I don't know what you're saving by using this method.
Pretty much the same amount of code, but less, since you're not creating this extra layer of abstraction:
// from Server directory
require("dotenv").config();
console.log(process.env.NODE_ENV);
From the documentation here: https://www.npmjs.com/package/dotenv
You can specify a custom path in your client folder files to the .env file by going
require('dotenv').config({ path: '/custom/path/to/.env' })
:)

Type Error: require(..).load is not a function? [duplicate]

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.
Based on NPM documentation you should use dotenv.config().
This worked for me:
require('dotenv').config({path:'my-app/.env'});
I right-clicked on the .env file to get the relative path.
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().
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.

Getting undefined value of .env in nodejs

I am getting "undefned value of .env in nodejs. my .env file is on root directory.need help
my code for .env file:-
SECRET_KEY=mynameissuky
my app.js file:-
require('dotenv').config();
console.log(process.env.SECRET_KEY);
installed package - npm i dotenv
In the dotenv documentation, it is shown that the .env file is assumed to be in the current directory.
Path Default: path.resolve(process.cwd(), '.env')
process.cwd() method returns the current working directory of the Node.js process.
Specify a custom path if your file containing environment variables is
located elsewhere.
require('dotenv').config({ path: '/custom/path/to/.env' })
In your case, because your env file is located in the root directory you should specify a custom path for the .env file.
For example, my github.env is placed in the root directory and I use dotenv as following:
require('dotenv').config({path:'github.env'});

How to access dotenv variables in other files

I'm using the dotEnv package for my Node/Express server. I have the .env file in the root of my project. In the root I have a src folder where I import the package in my app.js file which is the entry point of my server like so: (root/src/app.js)
import dotenv from "dotenv";
dotenv.config();
This works fine and I can access it in the app.js without issue.
But I have a file called config.js in root/src/config that I also want to use the env variables. But when I use something like process.env.DB_HOST I get undefined. How can I use these env variables in this file (and subsequent files)? I managed to get it to work by using
require("dotenv").config({ path: `${__dirname}/../../.env` });
Is there an easier way to access the env variables without having to import the package with the correct path to the env in each file I want to use it in?

"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.
Based on NPM documentation you should use dotenv.config().
This worked for me:
require('dotenv').config({path:'my-app/.env'});
I right-clicked on the .env file to get the relative path.
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().
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.

Resources