Cant access variables from .env file in node.js application - node.js

I am trying to dockerize a strapi app with mongodb atlas database. The issue I am facing is that database file in /config is not reading the variable from .env file.
.env file
HOST=0.0.0.0
PORT=1337
DATABASE_HOST=xyz.mongodb.net
DATABASE_USERNAME=abc-admin
DATABASE_PASSWORD=12345xyz
ADMIN_JWT_SECRET=abcd1234
Database connection code
const {
DATABASE_HOST,
DATABASE_USERNAME,
DATABASE_PASSWORD
} = process.env;
module.exports = ({ env }) =>
({
defaultConnection: 'default',
connections: {
default: {
connector: 'mongoose',
settings: {
host: env('DATABASE_HOST', process.env.DATABASE_HOST),
srv: env.bool('DATABASE_SRV', true),
port: env.int('DATABASE_PORT', 27017),
database: env('DATABASE_NAME', 'xyz-dev'),
username: env('DATABASE_USERNAME', process.env.DATABASE_USERNAME),
password: env('DATABASE_PASSWORD', process.env.DATABASE_PASSWORD)
},
options: {
authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
ssl: env.bool('DATABASE_SSL', true),
},
},
},
});
I have tried with process.env and without it in the above file. But when I run the image after build it shows below error
error Error connecting to the Mongo database. URI does not have
hostname, domain name and tld
Any idea what I am doing wrong here? Thanks

One option is to use dotenv you need to import dotenv and run dotenv.config() before you can start using env variables
so change to
import dotenv from "dotenv";
dotenv.config()
// your code which user process.env
other option is to define all those env variable on your OS level. On unix you can add to ~/.bashrc file

Here's a bit more elaborate answer to your question (after reading your comments). Creating .env file means you just created it. It doesn't get automatically loaded. It's a typical way to use on unix machines, but has no relation to Node whatsoever.
What you need to do is somehow parse the content of that file (which is purely text), convert it to key-value pairs and pass it to node. There are many packages, and one that Amit showed is dotenv. It does all the work for you, and at the end, you get your variables injected inside process.env.
The simplest way would be to install this package (from npm) and use it as described. But if you cannot modify the code in any way, then you can simply parse the content of the file with a script, and then start the node server. Here's an example (taken from npm scripts: read .env file):
"scripts": {
"example": "some-lib --argument --domain $(grep DOMAIN .env | cut -d '=' -f2)"
}
The drawback here is that it doesn't work across various operating systems and that using a specific library for that is way more tested than your manual scripts.

Related

Read OS(Linux and MAC) environment variable to not expose backend IP's and Credentials in angular 9

I'm trying to build and deploy my angular 9 project in production environment. The main goal is to protect my back-end services IP address and credentials as these environments can't be exposed to anyone for security issue. Build and Serve works fine for current setups but the problem is i can't access/read environment variables except one that i created using shell. Only i can read the value of TEST1 variable but BASE_URL and NOTIFICATION_URL shows undefined. One important thing is that i have no idea about system OS environment variable and don't system environment will work in production build. I just googled and created those variables in shell and echo shows value properly but i'm not sure i'm in a proper way. Please Suggest me the suitable way or currently where i made mistake. And what about production?
Shell variables i created:
Custom Webpack
const webpack = require('webpack');
module.exports = {
plugins: [new webpack.DefinePlugin({
'process.env': {
BASE_URL: JSON.stringify(process.env.BASE_URL),
NOTIFICATION_URL: JSON.stringify(process.env.NOTIFICATION_URL),
TEST1: JSON.stringify(process.env.TEST1)
}
})]
}
typings.d.ts
// #ts-ignore
declare var process: Process;
interface Process {
env: Env
}
interface Env {
BASE_URL: string
NOTIFICATION_URL: string
TEST1: string
}
interface GlobalEnvironment{
process: Process;
}
environment.prod.ts
export const environment = {
production: true,
baseUrl: `${process.env.BASE_URL}`,
notificationUrl: `${process.env.NOTIFICATION_URL}`,
test: `${process.env.TEST1}`,
};
TO SET environment variables permanently on server
edit /etc/environment
Paste:
BASE_URL=http://localhost:8080/
TEST_URL=http://localhost:8080/
Save
To Apply on current run: source /etc/environment
Note: When you ssh into the machine and use export TEST=test, Then a session wide environment variable would be created, Only the commands in that session can access it. And it would get deleted after closing the ssh session.

NodeJS not recognizing .env file

I have like 5 NodeJS services running, but I have a problem in one of those.
This is the nodemon.json file:
{
"watch": ["**/*.ts"],
"ext": "ts,json",
"ignore": ["./test/*.ts"],
"exec": "node -r ts-node/register -r dotenv/config Index.ts dotenv_config_path=$(pwd)/.env",
"env": {
"NODE_ENV": "development"
}
}
It's the same as the rest of services. When I run npm run dev I got error messages depending on which value is taking from the .env file, example:
const LOCAL_CONFIGURATION = {
PORT_APP: 8082,
MONGODB: {
SERVER: process.env.MONGO_DTE,
AUTH: {
auth: {
password:process.env.MONGO_PASSWORD,
user:process.env.MONGO_USER
}
},
},
MS_NOTIFICACION: "http://localhost:8089/notificacion",
ELASTIC_PATH: process.env.ELASTIC_PATH,
...COMMON,
};
The first error message is:
ConfigurationError: Missing node(s) option
That message is produced because it's not reading the value from process.env.ELASTIC_PATH, but if I put a hardcoed value like "http://with.the.correct.url" and it tries again to run, I get another error:
Error: Credentials must be provided when creating a service client
That error is because it's trying to read password:process.env.MONGO_PASSWORD and user:process.env.MONGO_USER
etc, so, there's a problem on reading the .env file. I know that .env file has those values, and the file is in UTF-8, without quotes, etc. The .env file is the same file as the other services, it works ok in the rest but I don't know why is not getting read here.
Any idea?
EDIT:
Plus, I put a console.log(process.env); in config.ts file and it shows values like this:
But there's no values from the .env for example, there in the picture there's a value called COMPUTERNAME so if I put console.log(process.env.COMPUTERNAME); I get: IBM-NOT87
Why is not getting the .env file?
Seems like you need to require/configure dotenv. Docs:
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
To further expand on #JBallin answer
you should use this on your app.js
Or if that does not work then you will need to explicitly add it to the file you are wanting to use those Variables
Sharing image, as its sometimes easier to see expanded
code here =>
require('dotenv/config') // require the dotenv/config at beginning of file
const express = require('express')
const mongoose = require('mongoose')
require('dotenv').config({ path: "./sample.env" });
In the file you are using environment variables,
As early as possible, require the "dotenv" and in the config() method, specify the path of the .env file, even if it in your root directory or the same directory where node starts.
The code for requiring and specifying file in the same directory is in the first line in the answer.
Also, for further reading 📖 , you can visit https://github.com/motdotla/dotenv#path
You cat try this.
-> npm i dotenv
and in code add this piece of code
require('dotenv').config({
path: 'your path here'
})
Install dotenv package
npm install --s dotenv
And add this require("dotenv").config(); in index.js/ts file.

Using Environment Variables in nuxt.config.js

I'm using Nuxt & Axios but having trouble using environment variables when building the application from my local machine.
I have installed the #nuxtjs/dotenv module in an attempt to fix this issue but still having problems.
Note: The environment variables work fine when building the app within my hosting providers environment. It is only building from my local machine that gives me trouble. My IDE is VS Code.
Here is my axios setup inside nuxt.config.js:
module.exports = {
...
buildModules: [
'#nuxtjs/dotenv'
],
modules: [
// Doc: https://axios.nuxtjs.org/usage
'#nuxtjs/axios'
],
axios: {
baseURL: process.env.BASE_URL
},
...
}
My .env file has the following:
BASE_URL="https://some.api.com"
The .env variables are not being recognized when building the app:
nuxt build
Instead, it just sets the axios base url to the same host:port that the server runs on by default. Ex: localhost:4000
I found the following documentation from #nuxtjs/dotenv module: https://github.com/nuxt-community/dotenv-module#using-env-file-in-nuxtconfigjs. This instructs you to add the following to the top of nuxt.config.js:
require('dotenv').config()
This works for building locally; my variables from .env are recognized! However, because dotenv is a dev dependency, this causes the build to crash when deployed to my hosting provider because the module isn't recognized.
I know that I can define the environment variables directly in the build command as follows but I would prefer NOT to do so:
NUXT_ENV_BASE_URL=some.api.com nuxt build
Is there an easy way to get environment variables to work locally inside of nuxt.config.js during the build process that also works well when deploying to production??
Thank you!
Updated 2020-09-26
As of 2.13.0 I have removed #nuxtjs/dotenv. My nuxt.config.js now simply reads as below with the dotenv imports removed. I made no other code changes and the rest functions exactly the same for me.
env: {
DB_HOST: process.env.DB_HOST
},
My .env contains the following.
DB_HOST=http://localhost:5001/
Original answer
I installed the following as a dev dependency; #nuxtjs/dotenv. Then I added the following to my nuxt.config.js. I found this import statement in an article and tried it. Thankfully it worked for me.
import dotenv from "dotenv";
dotenv.config();
env: {
DB_HOST: process.env.DB_HOST
},
I created a file called .env with the following content
DB_HOST=http://localhost:5001/
In nuxt version v2.13.0, support for Runtime Config was added. This adds proper support to read environment variables at runtime. Previously they could be read but were compiled into the application.
The standard documentation is pretty good: https://nuxtjs.org/guide/runtime-config/ .
There is also a great blog post on how to migrate. You remove the use of #nuxtjs/dotenv.
https://nuxtjs.org/blog/moving-from-nuxtjs-dotenv-to-runtime-config/
For example, in your nuxt.config.js, you define.
// Public env variables that are exposed on the frontend.
publicRuntimeConfig: {
someAccessKeyId: process.env.SOME_ACCESS_KEY_ID,
},
// Private env variables that are not be exposed on the frontend.
privateRuntimeConfig: {},
Then in your vue code, you access it via.
const { someAccessKeyId } = this.$config

Should I call dotenv in every node JS file?

I want to work with environment variables. Unfortunately, I am an inexperienced developer and decided very late to implement such a solution in my project.
I'm trying to inject environment variables, located in .env file, to all JS files (not all of them using environment variables, but I thought it would be faster and easier). Currently, I'm using dotenv package but it is obviously working in one file at once.
Should I use dotenv the standard way? Maybe there's a vulnerability I don't know of and that's why it is very unpopular to use environment variables this way.
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
You don't need to write require('dotenv').config() in every file. Just include this as the top statement in the index.js or the main file that got executed at the very first place when you run your program.
Like the comment on your file mentioned, you should have an entry point for your ENVs. You don't want to require('dotenv') in every file.
Instead, create a new file (named something like environment.js) that is in the utils or core folder.
require('dotenv').config();
/* eslint no-process-env:0 */
module.exports.default = {
env: process.env.env,
url: process.env.url,
apiUrl: process.env.apiUrl,
logLevel: process.env.logLevel,
db: {
host: process.env.db_host
port: process.env.db_port
}
// Grab everything in you .env file here
}
Then in each of your other files you can include your configs in a nice json object.
const config = require('../utils/environment');
dbConnector(config.db.host, config.db.port);
// blah blah blah
You accepted the first comment but it's not the right way/answer.
you can include require('dotenv').config() at the very beginning of your entry file (index.js , server.js ..) and you'll get the process.env variables anywhere in your app by just calling process.env.VARIABLE_NAME

How do I setup the dotenv file in Node.js?

I am trying to use the dotenv NPM package and it is not working for me. I have a file config/config.js with the following content:
'use strict';
var dotenv = require('dotenv');
dotenv.load();
console.log('config');
I have another file .env at the root of my application folder. I also have an environment variable TWILIO_ACCOUNT_SID.
This is the process I go through while trying to use the environment variables in a certain function:
$ node
> require('./config/config.js');
config
{}
> process.env.TWILIO_ACCOUNT_SID
undefined
I defined the TWILIO_ACCOUNT_SID in my .env file but as soon as I try to output the value in my console, I get an error stating that the variable is undefined.
I will be very grateful for any support in troubleshooting this issue.
In my case, every time I tried to get a key from the .env file using process.env.MY_KEY, it returned undefined.
I suffered from this problem for two hours just because I named the file something like keys.env which is not considered to be a .env file.
So here is the troubleshooting list:
The filename should be .env (I believe .env.test is also acceptable).
Make sure you are requiring it as early as possible in your application using this statement require('dotenv').config();
The .env file should be in the root directory of your project.
Follow the "file writing rules" like DB_HOST=localhost, no need to wrap values in double/single quotes.
Also, check the documentation of the package on the NPM site.
I solved this using:
require('dotenv').config({path: __dirname + '/.env'})
or with an absolute path:
C:\\asd\\auhsd\\.env
If it does not find the .env file, it will return undefined.
Save yourself some troubleshooting time and log your require call, like so:
console.log(require('dotenv').config())
You should see an error with more detailed info on the problem.
Had the same issue recently. Check your .env file and use equal sign not colon. Here's an example:
key=value
instead of:
key:value
I had the same problem. I realized my file was somehow encoded in UCS-2 BE BOM. Converting my .env file to UTF-8 fixed it (you can easily do that using Notepad++, for example).
i didn't put my environment variables in the right format as was in the dotenv module documentation e.g. i was doing export TWILIO_CALLER_ID="+wwehehe" and so the dotenv module wasn't parsing my file correctly. When i noticed that i removed the export keyword from the declarations and everything worked fine.
I had the same problem and I tried 4 hours to find the fault. In my case, it was bizarre.
When I tried "node app.js", it worked. When I wanted a daemon to start it, it did not work.
How did I solve my problem?
I replaced:
var dotenv = require('dotenv');
dotenv.load();
with:
var dotenv = require('dotenv').config({path: path.join(__dirname, '.env')})
Make sure that variables are not already set. Dotenv won't override them.
If variables are set then you will have to remove them. In powershell you can use the following command - as mentioned here:
Remove-Item Env:\MyTestVariable
I had a problem also with .env variables not loading and being undefined.
What I tried:
index.js:
import dotenv from 'dotenv';
dotenv.config();
import models from './models';
models.js
import Sequelize from 'sequelize';
const sequelize = new Sequelize(
process.env.DATABASE,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
dialect: 'postgres',
}
);
Apparently, because of how loading the imports works in nodejs, the import of models in index.js caused that the models.js was executed before dotenv.config(). Therefore I got undefined values from process.env.
When I changed models.js to do the dotenv configuration like:
import Sequelize from 'sequelize';
import dotenv from 'dotenv';
dotenv.config();
const sequelize = new Sequelize(
process.env.DATABASE,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
dialect: 'postgres',
}
);
it started to work!
Take care that you also execute your Node script from the ROOT folder.
E.g. I was using a testing script in a subfolder called ./bin/test.js.
Calling it like: node ./bin/test.js worked totally fine.
Calling it from the subfolder like:
$ pwd
./bin
$ node ./test.js
causes dotenv to not find my ./.env file.
I am using NodeJS on windows 10. I used process.env.var-name to access the variables but failed because it gives me windows path variables as a JSON object, so I installed dotenv ( npm install dotenv ). dotenv gets process envirnoment variables from your project's .evn file
npm install dotenv or yarn add dotenv
const dotenv = require('dotenv');
dotenv.config();
process.env.variable_name
output
Make sure to set cwd in the pm2 config to the correct directory for any calls to dotenv.config().
Example:
Your index.js file is in /app/src, your .env file is in /app. Your index.js file has this
dotenv.config({path: "../.env"});
Your pm2 json config should have this:
"cwd": "/app/src", "script": "index.js"
You could also use dotenv.config({path: path.join(__dirname, "../.env")}); to avoid the CWD issue. You will still have a problem if you move the .env or the index.js file relative to each other.
Working Solution:
If you are using webpack (which you definitely should), use a very handy plugin dotenv-webpack which solves the issue of reading environment variables from .env file
Make sure .env is in root directory of your project.
Steps to install the plugin:
npm i -D dotenv-webpack
In webpack.config file:
const Dotenv = require('dotenv-webpack');
module.exports = {
...
plugins: [
new Dotenv(),
...
],
...
};
Now you can call any environment variable defined in .env file using process.env in any js file
My code structure using is as shown below
-.env
-app.js
-build
-src
|-modules
|-users
|-controller
|-userController.js
I have required .env at the top of my app.js
require('dotenv').config();
import express = require('express');
import bodyParser from 'body-parser';
import mongoose = require('mongoose');
The process.env.PORT works in my app.listen function. However, on my userController file not sure how this is happening but my problem was I was getting the secretKey value and type as string when I checked using console.log() but getting undefined when trying it on jwt.sign() e.g.
console.log('Type: '+ process.env.ACCESS_TOKEN_SECRET)
console.log(process.env.ACCESS_TOKEN_SECRET)
Result:
string
secret
jwt.sign giving error
let accessToken = jwt.sign(userObj, process.env.ACCESS_TOKEN_SECRET); //not working
Error was
Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'.
Type 'undefined' is not assignable to type 'Secret'.
My Solution:
After reading the documentation. I required the env again in my file( which I probably should have in the first place ) and saved it to variable 'environment'
let environment = require('dotenv').config();
console logging environment this gives:
{
parsed: {
DB_HOST: 'localhost',
DB_USER: 'root',
DB_PASS: 'pass',
PORT: '3000',
ACCESS_TOKEN_SECRET: 'secretKey',
}
}
Using it on jwt.sign not works
let accessToken = jwt.sign(userObj, environment.parsed.ACCESS_TOKEN_SECRET);
Hope this helps, I was stuck on it for hours. Please feel free to add anything to my answer which may help explain more on this.
There's a lot of confusion about this topic and in these answers. I'm not surprised, that no single answer was accepted. Hopefully yet.
The answer by Basheer indeed solves most of the problems. However, there are few things you still need to know. Especially, if you're coming, like me, from frontend background and wants to add secrets to your frontend. Possibly, related to the introduction of some Server-Side Rendering (SSR) logic in the app.
Most probably you've seen this code in your webpack settings in a frontend app to solve the issue, as a frontend developer.
/* Custom webpack properties. */
const dotenv = require('dotenv-webpack');
module.exports = {
plugins: [
new dotenv(), // Handle environemntal variables on localhost, but on the Server-Side Rendering (SSR). There's no access to "process.env" on the browser.
],
};
Now, it'll work out fine, if you render on the server (SSR) across your app if the .env file is in the root of your project. However, it might not work if you have some custom server-related settings. An example of such situation is Angular Universal, Nuxt.js handles this much easier in which require('dotenv').config() in your next.config.js and makes you good to go. That's due to difference in philosophies between how Angular and Vue.js are handling SSR. To get Angular Universal app from Angular that's just 1 command, but the SSR app isn't as nicely organized as Nuxt.js. It comes with a price that to generate Nuxt.js app from Vue.js, you basically have to generate a new Nuxt.js project and copy files due to quite some differences between Nuxt.js and Vue.js setup. Don't know how React/Next.js and Svelte/Sapper solves this, but if similarly to Angular then you also might consider reading further.
Now, you've some server-related logic in a separated folder called server and let say the file is called main.ts. Maybe apart SSR in that file, you can also have sending mail (nodemailer?) logic. Then you'd like to use process.env, but apparently it doesn't work, even though you have the logic defined in webpack. That's where the require('dotenv').config(); is needed, even if you're using different syntax for import (such as import { Express } from 'express'; for example), require('dotenv').config(); will work like that. Don't feel confused. As long as .env is in the root of your app (don't confuse with server folder) and the variables have correct syntax inside that file, e.g.
MAIL_ACCOUNT=mymail#mydomain.com
MAIL_HOST=smtp.mydomain.com
MAIL_PORT=587
It'll work.
Last scenario, in the SSR app you realised that to host this app you need something called Serverless/Cloud Functions/FaaS. Here, I know only Firebase scenario. In your project, to deploy such app you might have functions folder, from which you deploy the SSR app to the Cloud Functions for Firebase, in this example. What a surprise, on a deployment mail is not working and after hours of figuring out what's happening in the logs you can see process.env.VARIABLE_NAME returning undefined. The reason is that as of today the CLI cannot merge files from other locations and indeed the .env file has to be manually copied to the functions folder. Once copy/paste the .env file to functions and deploy, it'll work.
What you can use for debugging is one of those:
console.log(require('dotenv').config());
console.log(require('dotenv').config({debug: true}));
However, be careful with your secrets, because these will be revealed when your .env setup will be done. Trying to access one of the secrets and trying to log its value in the logs might be more secure option. Especially, if you have many secrets and don't want to rewrite all.
Hope so this one post will cover most of the scenarios.
My problem was stupid. I created the .env in a text editor, and when I saved it it actually saved as
'.env.txt'
which was only visible after I did a
'ls -a'
in terminal and saw the file name.
A quick:
mv .env.txt .env
And I was in business
The '.env' file should be in the root directory of your node js server file (server.js or for me).
If you placed the '.env' file at the root of your project, it won't work. My mistake was that I have the server.js file nested in a folder named 'controller'.
So I had to fix it by placing the .env file in the same directory as the server.js file.
For React apps created with the create-react-app template, you don't need to use dotenv directly. react-scripts does that for you.
Simply creates a .env file in the top level directory of your project and add all your envs there, but notice that they MUST start with REACT_APP prefix, otherwise they will be ignored.
More details in their documentation. I just spent a couple of hours dealing with this and hope it will save you some time.
Had the same problem. I used dotenv-webpack and need to define
plugins: [
new Dotenv()
]
in both webpack production and webpack base files (I use webpack merge).
If was not defined in both files then it did not work.
If you are facing this problem it could be that the environment variable(s) is added/loaded after the file that requires the specific variable
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const morgan = require('morgan');
const passport = require('passport'); //you want to use process.env.JWT_SECRET (you will get undefined)
dotenv.config();
in the above case, you will get undefined for the process.env.JWT_SECRET
So the solution is that you put dotenv.config() before const passport = require('passport');
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const morgan = require('morgan');
dotenv.config();
const passport = require('passport'); //you want to use process.env.JWT_SECRET (you will get the value for the enviroment variable)
In my case, I've created a wrapper JS file in which I have the logic to select the correct variables according to my environment, dynamically.
I have these two functions, one it's a wrapper of a simple dotenv functionality, and the other discriminate between environments and set the result to the process.env object.
setEnvVariablesByEnvironment : ()=>{
return new Promise((resolve)=>{
if (process.env.NODE_ENV === undefined || process.env.NODE_ENV ==='development'){
logger.info('Lower / Development environment was detected');
environmentManager.getEnvironmentFromEnvFile()
.then(envFile => {
resolve(envFile);
});
}else{
logger.warn('Production or Stage environment was detected.');
resolve({
payload: process.env,
flag: true,
status: 0,
log: 'Returned environment variables placed in .env file.'
});
}
});
} ,
/*
Get environment variables from .env file, using dotEnv npm module.
*/
getEnvironmentFromEnvFile: () => {
return new Promise((resolve)=>{
logger.info('Trying to get configuration of environment variables from .env file');
env.config({
debug: (process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'development')
});
resolve({
payload: process.env,
flag: true,
status: 0,
log: 'Returned environment variables placed in .env file.'
});
});
},
So, in my server.js file i only added the reference:
const envManager = require('./lib/application/config/environment/environment-manager');
And in my entry-point (server.js), it's just simple as use it.
envManager.setEnvVariablesByEnvironment()
.then(envVariables=>{
process.env= envVariables.payload;
const port = process.env.PORT_EXPOSE;
microService.listen(port, '0.0.0.0' , () =>{
let welcomeMessage = `Micro Service started at ${Date.now()}`;
logger.info(welcomeMessage);
logger.info(`${configuration.about.name} port configured -> : ${port}`);
logger.info(`App Author: ${configuration.about.owner}`);
logger.info(`App Version: ${configuration.about.version}`);
logger.info(`Created by: ${configuration.about.author}`);
});
});
I had to literally use no name for the .env file, just have the .env extension and save the file like that and it worked.
I solved this just renaming the file to .env
to y file was named config.env , when I renamed to .env , it works.
I spent a lot of time going through these fixes. I was developing locally and just had to restart the server because the .env file isn't hot reloaded.
is dotenv installed in your project?
Try to install it using npm install dotenv in your project.
Once it is installed load it in any files where you need it using const env = require('dotenv').config().
You can then use in any line where you need to. For example to call port from .env use: process.env.PORT
If you use "firebase-functions" to host your sever-side-rendered application, you should be aware of this one:
error: Error: ENOENT: no such file or directory, open 'C:\Codes\url_shortener\functions\.env'
Means you have to store the .env file in the functions folder as well.
Found this one by:
console.log(require('dotenv').config())
I cloned a repo from Github and went through every one of the suggestions here. After a lot of frustration, I realized that npm install did not install any of the modules and my node_modules folder was empty the whole time.
QUICK FIX:
1) delete your node_modules folder
2) delete your package-lock.json
3) run npm install
const dotenv = require('dotenv'),
path = require('path')
dotenv.config({path: path.join(__dirname, '../.env')})
I had the same problem. I had created a file named .env, but in reality the file ended up being .env.txt.
I created a new file, saved it in form of 'No Extension' and boom, the file was real .env and worked perfectly.
This is how i fix my issue
Intially had this in .env of the root of my project
const db_port = 90101
const db_host="localhost"
const db_username="name"
const db_password="pwd"
const db_name="db"
And all my env variables where undefined.
I fixed it by removing all the const and using just key=value insted of
const key="value"
db_port = 90101
db_host=localhost
db_username=name
db_password=pws
db_name=db

Resources