The `uri` parameter to `openUri()` must be a string, got "undefined" - javascript

I searched a lot about this, but none of them could help me.
When I run my project, I get this error:
/home/ali/Desktop/personalitytest-backend/node_modules/mongoose/lib/connection.js:428
throw new MongooseError('The uri parameter to openUri() must be a ' +
^
MongooseError: The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to
mongoose.connect() or mongoose.createConnection() is a string.
My index.js file:
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
rateLimit = new require('express-rate-limit')({
windowMs: 1000 * 60 * 10,
max: 500,
handler: (req, res) => {
res.json({
data: 'Your request was too much, please try again in 10 minutes later.',
status: 'error'
})
}
});
const Application = new class {
constructor() {
this.setConfig();
this.setupDB();
this.setRouters();
this.setupExpress();
}
setConfig() {
require('dotenv').config();
app.use(require('helmet')());
app.use(express.json());
}
setupDB() {
mongoose.Promise = global.Promise;
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useCreateIndex: true });
}
setRouters() {
app.use('/', require('./routes'));
}
setupExpress() {
app.listen(process.env.PORT, () => console.log(`Listening on port ${process.env.PORT}.`));
// app.listen(process.env.PORT, process.env.IP, () => console.log(`Listening on port ${process.env.PORT}.`));
}
}
My .env file:
PORT=3000
DATABASE_URL=mongodb://localhost:27017/PersonalityTest
JWT_SECRETKEY=asfdawetq312etr%!#$qe
If I simply write database url in mongoose.connect method, there will be no error.
For example, this doesn't have error:
mongoose.connect("mongodb://localhost:27017/PersonalityTest", { useNewUrlParser: true, useCreateIndex: true });

To read the .env-file you'll need to install something that will read that file, for instance the dotenv package
npm install dotenv --save
Then you require that package in your code
require('dotenv').config();
And according to the dotenv documentation you should do it
As early as possible in your application, require and configure dotenv.
Next you might need to add double quotation marks around your DATABASE_URL value
DATABASE_URL="mongodb://localhost:27017/PersonalityTest"

Have you checked if your .env variables can be readed from that index.js file?
For example,check out what you get when you log some of them to the console:
console.log(process.env.DATABASE_URL);
If you get 'undefined', then you could try especifiying the absolute path for your .env file like this:
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, './.env') });
I struggled with this problem recently and in my case that solved the issue. Regards.

This work for me
const dotenv = require('dotenv')
dotenv.config({path:__dirname+'/.env'});

You have to declare .env file after installing dotenv package.

Make sure the .env file is named .env and not config.env or anything else.
I had this problem from following a tutorial online and only recently figured out why I got this error.
file structure
console output

to put :
require('dotenv').config();
at the top of the file where all the other "require" are.
Then add process.env.DATABASE_URL in a variable:
const source = process.env.DATABASE_URL;
and therefore at the top of the file you will have:
require ('dotenv'). config ();
const source = process.env.DATABASE_URL;
lower:
mongoose.connect (source, {useNewUrlParser: true});

I had this error and funny enough I had another project that uses a similar setup. Went to that project and started it up with the same .env values and it had no issues.
So I copied the code over the my current project and started the current one but did not want to connect to the mongodb if I have it setup like this mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true },
and will only work if I use it like this mongoose.connect("mongodb://localhost:3000/posts", { useNewUrlParser: true, useCreateIndex: true });
The thing that bothered me is that it works in one project but not in the other so I decided I'm going to delete my node_modules folder and package-lock.json file and reinstall everything.
After that everything worked.
Also Check if you dont have another node_modules folder that is clashing with your current one. if so delete both with your package-lock.json file and reinstall again. make sure you are in the correct directory as well.

move the .env file from the routes folder or any other folder. And don't place it in any particular folder just let it be in the main folder just like app.js or index.js whichever you have. This might work!

Make sure that your .env file is also located at the same path where you are executing the nodemon command. Or else you will have to declare the path of .env as in the answer of #oxk4r

const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then((con) => {
// console.log(con.connections);
console.log('DB connection successful');
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`app running on port ${port}...`);
});
Now the problem you facing is related to the path of // config.env to check that you accessing is correct// console.log(process.env); so that it will show you are get access of .env, if not then your path-
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
Now again console.log(process.env) if you get this in your console read their property is mentioned in config.env file present.
Check the link of your DATABASE and DATABASE_PASSWORD is correct or not or go to mongoDB Atlas change your password for your cluster and again try it

I know what the problem is. Make sure first you write dotenv.config({path: './config.env'}); And then you use your process.env.DATABASE_URL
Don't use process.env.DATABASE_URL before dotenv.config({path:'./config.env'});

Reference to the github repository:
https://github.com/realabbas/serverless-lambda-node-express-mongodb
Extending oxk4r's answer, I have the following tree showing the directory structure of the app:
.
├── code-of-conduct.md
├── .env
├── .gitignore
├── _config.yml
├── contributing.md
├── lib
│   ├── app.js
│   ├── db.js
│   └── routes
│   ├── index.js
│   └── notes
│   ├── note.js
│   └── notes.controller.js
├── LICENSE
├── package.json
├── package-lock.json
├── README.md
├── secrets.json
├── server.js
└── serverless.yml
We primarily use the dotenv package to reference the environmental variables in the .env file.
The important thing to note here is db.js and the .env files. We need to reference the .env file from the db.js file. We do as below:
db.js
const path = require('path');
const mongoose = require('mongoose')
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
console.log('here...', path.resolve(__dirname, '../.env'), process.env.MONGODB_URL)
mongoose.connect(process.env.MONGODB_URL
, { useNewUrlParser: true })
.env
MONGODB_URL="mongodb://127.0.0.1:27017/game-of-thrones"
As we see the tree, the .env file id one level above the db.js file. So, we reference it by the path package as:
path.resolve(__dirname, '../.env')

I had the same issue and it still persisted even after following all that was stated here. Was losing my mind until I went over the .env and realised I typed DB_NAME:mongodb+srv:/....., changed it to DB_NAME=mongodb+srv:/..... and the variable was no longer undefined, Mongoose was able to read it.

Check your key and value in your Heroku Config Vars under your app settings. Seems silly, but I had MONGODB_URL instead of _URI and it took me forever to realize my mistake.

mongoose.connect(process.env.MONGO_URI).then(() => {
console.log('DB Conected...')
})
In my case while connecting to database, I wrote wrong spelling of MONGO_URI , I wrote MANGO_URI there (but in .env file I had given name as MONGO_URI), Please do check & correct it, I know it not proper solution but sometime we do such small mistakes.

I found that when I write
dotenv.config();
before
mongoose.connect(process.env.LOCAL_MONGO);
and my problem solved.
Not written in the constructor.

import dotenv from "dotenv";
dotenv.config();
Or:
const dotenv = require("dotenv");
dotenv.config();
No need to add "" for database URL.

Change the .env file to the root of your app at the same level as the package.json

Create a .env file and provide MONGOURI in it.
Example:
MONGO_URI=mongodb+srv://<password>#cluster0.wlhevoe.mongodb.net/test
2.In index.js , require like this
const dotenv = require('dotenv')
dotenv.config({ path: './.env' }) // Here you must take care to provide the correct path of the .env file
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true })
.then(() => console.log('DB Connection Successful'));
This worked for me. Thanks

In my case, I had left out the brackets after the word config. So the correct config is below. No more errors now.
require('dotenv').config()

Related

Initialize firebase config with .env file? [duplicate]

I have .env file at root folder file
NODE_ENV=development
NODE_HOST=localhost
NODE_PORT=4000
NODE_HTTPS=false
DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=user
And server.js file in the root/app/config/server.js folder.
The first line of server.js file is
require('dotenv').config();
I also tried following:
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../../.env'});
However, my env variable are not loaded when I run the server.js file
from command prompt
node root/app/config/server.js
If I use the visual studio and press F5, it loads!!
I'm not sure what I'm doing wrong, what I'm missing.
Any suggestion is highly appreciate. Thanks.
How about use require('dotenv').config({path:__dirname+'/./../../.env'}) ?
Your problem seems to be the execution path.
This solved my issues in Node v8.14.1:
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
Simply doing require('dotenv').config({path:__dirname+'/./../../.env'}) resulted in a location that resolved as /some/path/to/env/./../../.env
Here is a single-line solution:
require('dotenv').config({ path: require('find-config')('.env') })
This will recurse parent directories until it finds a .env file to use.
You can also alternatively use this module called ckey inspired from one-liner above.
.env file from main directory.
# dotenv sample content
USER=sample#gmail.com
PASSWORD=iampassword123
API_KEY=1234567890
some js file from sub-directory
const ck = require('ckey');
const userName = ck.USER; // sample#gmail.com
const password = ck.PASSWORD; // iampassword123
const apiKey = ck.API_KEY; // 1234567890
If you are invoking dotenv from a nested file, and your .env file is at the project root, the way you want to connect the dots is via the following:
require('dotenv').config({path:'relative/path/to/your/.env'})
One of the comments in #DavidP's answer notes logging the output of dotenv.config with
console.log(require("dotenv").config())
This will output a log of the config and display errors. In my case it indicated the config method was referencing the current directory instead of the
parent directory which contained my .env file. I was able to reference that with the following
require('dotenv').config({path: '../.env'})
I've had this problem and it turned out that REACT only loads variables prefixed with REACT_APP_
VueJs can have a similar issue as it expects variables to be prefixed with: VUE_APP_
In the remote case that you arrive till this point, my issue was quite dumber:
I wrongly named my env variables with colon ":" instead of equals "=". Rookie mistake but the resulting behavior was not loading the misspelled variables assignment.
# dotenv sample content
# correct assignment
USER=sample#gmail.com
# wrong assignment (will not load env var)
USER : sample#gmail.com
Be sure to load .env at the beginning of the entry file (e.g. index.js or server.js). Sometimes, the order of execution loads the environment variables after the services are initiated. And, by using __dirname, it can easily point to the file required relative to the current file.
Here my project structure is like this.
.
├─ src
│ └─ index.ts
└─ .env
// index.ts
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({path: path.join(__dirname, '..', '.env')});
...
This solved the issue for me:
const path = require('path');
require('dotenv').config({
path: path.resolve('config.env'),
});
Try this:
const dotenv = require('dotenv');
dotenv.config({ path: process.cwd() + '/config/config.env' });
worked for me idk how??
You can first debug by using console.log(require('dotenv').config()).
In my scenario, my .env file is in root directory and I need to use it in a nested directory. The result gives me { parsed: { DATABASE_URL: 'mongodb://localhost/vidly', PORT: '8080' } }. So I simply parse the result and store it in a variable const dotenv = require('dotenv').config().parsed;. Then access my DATABASE_URL like a JS object: dotenv.DATABASE_URL
It took me a few head scratches, and the tip to log the output of the require statement to console was really helpful. console.log(require('dotenv').config());
Turns out I was running my app from my user/ directory with nodemon application_name/. and that was making dotenv look for the .env file in my home dir instead of the app's. I was lazy by skipping one cd and that cost me a few minutes.
const path = require('path');
const dotenv = require('dotenv');
dotenv.config({ path: path.resolve(__dirname, '../config.env') })
In my case .env was read fine, but not .env.local.
Updating package.json to name .env into .env.local ( cp ./.env.local .env) solved the problem:
"myscript": "cp ./.env.local .env && node ./scripts/myscript.js"
if config.env file and index.js file both present in the same directory:
then, file: index.js
const path = require('path');
// Set port from environment variables
dotenv.config({path: 'config.env'})
const PORT = process.env.PORT || 8080
file: config.env:
PORT = 4000
One time I have got the same problem. Dotenv did not load .env file. I tried to fix this problem with a path config, to put .env file in a root folder, to put .env in the folder where the file is running and nothing helps me. Then I just trashed Node_modules folder, reinstall all dependencies and it works correctly
You can need the path of the .env file relative to the current working directory from where the application was launched.
You can create this path like this:
const path = require('path')
require('dotenv').config({path: path.relative(process.cwd(), path.join(__dirname,'.env'))});
process.cwd() returns the absolute path of the working directory.
__dirname returns the absolute path of the application.
path.join() adds the path of the .env-file to the path of the application. so if your .env file is nested deeper, just add the folders (e.g. path.join(__dirname, 'config', 'secret','.env'))
path.relative() creates the relative path.
I'am using:
import findUp from 'find-up';
dotenv.config({ path: findUp.sync('.env') });
I found an option debug: true to be sent in the config which showed me the following:
[dotenv][DEBUG] "PORT" is already defined in `process.env` and was NOT overwritten
I added overwrite: true and got it working:
[dotenv][DEBUG] "PORT" is already defined in `process.env` and WAS overwritten
I know I might be too late answering, but decided to share my findings after hours of checking the documentation.
Typescript.
if you are trying to resolve __dirname and you are compiling your source folder in another folder, make sure that you edit __dirname. in my case i am compiling ts in dist folder and env files are not located in dist folder, but in the root folder. so you should delete /dist from __dirname. To debug it, you can call error() function which returns error if there is problem with reading env file.
require('dotenv').config({
path: __dirname.replace('\dist','') + `${process.env.NODE_ENV}.env`
}); # To debug .error()
another thing, when you set env variables make sure that following:
no space between variable and (&&) as following
"scripts": {
"build": "npx tsc",
"start": "set NODE_ENV=production&& node dist/index.js",
},
The fastest fix here, just into seconds, add the variables to the platform you are using in my case render.com
What worked for me using Playwright / typescript:
1 create file and place at the root of the project: global-setup.ts
add inside:
async function globalSetup() {
// Configure ENV variables
require('dotenv').config()
}
export default globalSetup
2 Then use this file into playwright.config.ts
as: globalSetup: require.resolve('./global-setup'),
In this way, global conf is created, and pickup in every single test.
Use only absolute path if you are using some other tool(installed globally) to call your config file somewhere ...
require('dotenv').config({
path: '/path/to/my/project/.env',
});
One silly mistake I did was i used colon ":" in place of "="
I used below
USER:root
But it should be
USER=root
If nothing helps put your .env outside the src folder if you have folder structure like
-src/
- index.js
- env.js (where you call dotenv.config)
// you may call dotenv.config anywhere but entry point is best.
.env (file outside the src folder)
My failer was the path keyword . should be the P not capital Letter . that was so funny

node dotenv won't work with pm2

I have an application where locally (without pm2) all the environment variables in the .env file work just fine using dotenv.
But on the server where I'm using pm2 to run the app, the environment variables remain undefined.
The pm2 commands I'm using to run the app on server are:
pm2 start myapp/app.js
pm2 startup
pm2 save
dotenv will read .env file located in the current directory.
When you call pm2 start myapp/app.js it won't search for myapp/.env.
.env // It will try to load this, which doesn't exist
myapp/
app.js
So you have two solutions
use path option:
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });
Or call your script from inside myapp/
pm2 start app.js
A good pattern here is to remove dotenv from your code and "require" it on the command line. This makes your code nicely transportable between any environment (including cloud-based) - which is one of the main features of environment variables.
Note: you will still need to install dotenv in your project via npm when running it on a server.
a) code up your .env file alongside your script (e.g. app.js)
b) to run your script without pm2:
node -r dotenv/config app.js
c) in pm2.config.js:
module.exports = {
apps : [{
name : 'My Application',
script : 'app.js',
node_args : '-r dotenv/config',
...
}],
}
and then
pm2 start pm2.config.js
note: the use of dotenv/config on the command line is one of the best practices recommended by dotenv themselves
edit 2021: for completeness - as my answer has got some ticks, I wanted to add a 4th option to the list:
d) combined pm2/env config
module.exports = { apps : [{
name : 'My Application',
script : 'app.js',
env : {
PORT: 5010,
DB_STRING: 'mongodb://localhost:27017',
...
},
}]};
This will be useful if you are treating your pm2.config as environmental configuration and outside of git etc. It just negates the need for a separate .env, which may suit you. It negates the need for dotenv completely as pm2 injects the env variables into your script's process
you have kill you pm2 process first
try
pm2 kill
then restart pm2 using
pm2 start app.js
I had the same problem but it wasnt explained clearly so here is the solution based on
github user vmarchaud comment.
This also fixes the issue people had with #Andy Lorenz solution.
In my case i wanted to create an ecosystem file for multiple apps but i was keep getting
Error: Cannot find module 'dotenv/config'
The solution was easy.
You have to declar cwd, aka the project folder where the dotenv/config will be read from.
module.exports = {
apps: [{
name: 'app1 name',
script: 'app1.js',
cwd: '/path/to/folder/',
exec_mode: 'fork_mode',
node_args: '-r dotenv/config',
}, {
name: 'app2 name',
script: 'app2.js',
cwd: '/path/to/folder/',
instances: 'max',
exec_mode: 'cluster',
node_args: '-r dotenv/config',
}],
};
You can parse .env using dotenv lib end set them manually in ecosystem.config.js
ecosystem.config.js:
const { calcPath, getEnvVariables } = require('./helpers');
module.exports = {
apps: [
{
script: calcPath('../dist/app.js'),
name: 'dev',
env: getEnvVariables(),
},
],
};
helpers.js:
const path = require('path');
const dotenv = require('dotenv');
const fs = require('fs');
function calcPath(relativePath) {
return path.join(__dirname, relativePath);
}
// this function will parce `.env` file but not set them to `process.env`
const getEnvVariables = () => {
const envConfig = dotenv.parse(fs.readFileSync(calcPath('.env')));
const requiredEnvVariables = ['MODE'];
for (envVariable of requiredEnvVariables) {
if (!envConfig[envVariable]) {
throw new Error(`Environment variable "${envVariable}" is not set`);
}
}
return envConfig;
};
None of this worked for me because I was using cluster mode.
I installed dotenv as dev dependency at the root (I was using yarn workspaces too).
Then I did this:
require('dotenv').config({ path: 'path/to/your/.env' })
module.exports = {
apps: [
{
name: 'app',
script: 'server/dist/index.js',
instances: 2,
exec_mode: 'cluster',
instance_var: 'APP_INSTANCE_SEQ',
// listen_timeout: 10000,
// restart_delay: 10000,
}
]
}
I use a much simpler version of #Marcos answer:
.env
app.js
for example we need to store token in .env file and pass it right to app.js:
inside .env
token=value
inside app.js:
require('dotenv').config();
console.log(process.env.token)
Also, don't forget. If you add .env file to .gitignore and then git pull you repo on VPS or smth, you need to copy .env file manually, otherwise your app won't work.
And in some cases it's important in what area you are using your config, so make sure that NODE_ENV=production string is added to your .env file.
After all you could use pm2 start app.js right from your app's folder.
This was my project setup..
/src/app.ts
which than compiled into dist folder.
/dist/app.js
my .env file was outside dist folder so it wasn't accessible.
this is the command i tried.
pm2 start app.js --env=.env

Dotenv not loading properly

I am trying to access some environment variables using process.env that were loaded by dotenv.
My folder structure :
.env
src
-- - server.js
My server.js configuration :
(...)
import auth from './middleware/auth'
import dotenv from 'dotenv'
dotenv.load({
path: '../',
silent: process.env.NODE_ENV === 'production'
})
auth()
// Instantiate app
const app = express();
The file where I try to access process.env variable :
(...)
module.exports = function() {
console.log("env", process.env.MONGODB_URI)
var options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeader()
options.secretOrKey = process.env.JWT_SECRET
Which logs env, undefined, and then crashes with
TypeError: JwtStrategy requires a secret or key
Even if I move .env into src (same directory as server) and remove path in config, it fails.
It appears that when you specify the path, you need to make it full:
require('dotenv').config({path: __dirname + '/../.env'});
.env being your file
Try this; this should work.
import {} from 'dotenv/config'
import somethingElse from 'somethingElse'
...
[the rest of your code]
This works because of how ES6 modules imports modules.
If you want to dig into more.
Please refer this. https://hacks.mozilla.org/2015/08/es6-in-depth-modules/
As a summary :
When you run a module containing an import declaration, the modules it
imports are loaded first, then each module body is executed in a
depth-first traversal of the dependency graph, avoiding cycles by
skipping anything already executed.
Hope this will help someone.
I'm using require('dotenv').config() on my main nodejs .js entry file and it works just fine.
From the docs:
Path
Default: .env
You can specify a custom path if your file containing environment
variables is named or located differently.
require('dotenv').config({path: '/custom/path/to/your/env/vars'})
use may use:
require('dotenv').config({ path: require('find-config')('.env') })
This will recurse parent directories until it finds a .env file to use.
You can also alternatively use this module called ckey inspired from one-liner above.
.env file from main directory.
# dotenv sample content
USER=sample#gmail.com
PASSWORD=iampassword123
API_KEY=1234567890
some js file from sub-directory
const ck = require('ckey');
const userName = ck.USER; // sample#gmail.com
const password = ck.PASSWORD; // iampassword123
const apiKey = ck.API_KEY; // 1234567890
If you're using a mono-repo which uses a single .env file across multiple packages/work-spaces you can use the following to find the root .env file.
Install the find-up package from npm: https://www.npmjs.com/package/find-up
import find from 'find-up';
export const findEnv = () => find.sync(process.env.ENV_FILE || '.env');
you have to set the dotenv configs at the very top level of your app:
import dotenv from 'dotenv'
dotenv.load({
path: '../',
silent: process.env.NODE_ENV === 'production'
})
(...)
import auth from './middleware/auth'
auth()
// Instantiate app
const app = express();
The order of imports in this case matters since you are loading the environment variables.

dotenv file is not loading environment variables

I have .env file at root folder file
NODE_ENV=development
NODE_HOST=localhost
NODE_PORT=4000
NODE_HTTPS=false
DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=user
And server.js file in the root/app/config/server.js folder.
The first line of server.js file is
require('dotenv').config();
I also tried following:
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../../.env'});
However, my env variable are not loaded when I run the server.js file
from command prompt
node root/app/config/server.js
If I use the visual studio and press F5, it loads!!
I'm not sure what I'm doing wrong, what I'm missing.
Any suggestion is highly appreciate. Thanks.
How about use require('dotenv').config({path:__dirname+'/./../../.env'}) ?
Your problem seems to be the execution path.
This solved my issues in Node v8.14.1:
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
Simply doing require('dotenv').config({path:__dirname+'/./../../.env'}) resulted in a location that resolved as /some/path/to/env/./../../.env
Here is a single-line solution:
require('dotenv').config({ path: require('find-config')('.env') })
This will recurse parent directories until it finds a .env file to use.
You can also alternatively use this module called ckey inspired from one-liner above.
.env file from main directory.
# dotenv sample content
USER=sample#gmail.com
PASSWORD=iampassword123
API_KEY=1234567890
some js file from sub-directory
const ck = require('ckey');
const userName = ck.USER; // sample#gmail.com
const password = ck.PASSWORD; // iampassword123
const apiKey = ck.API_KEY; // 1234567890
If you are invoking dotenv from a nested file, and your .env file is at the project root, the way you want to connect the dots is via the following:
require('dotenv').config({path:'relative/path/to/your/.env'})
One of the comments in #DavidP's answer notes logging the output of dotenv.config with
console.log(require("dotenv").config())
This will output a log of the config and display errors. In my case it indicated the config method was referencing the current directory instead of the
parent directory which contained my .env file. I was able to reference that with the following
require('dotenv').config({path: '../.env'})
I've had this problem and it turned out that REACT only loads variables prefixed with REACT_APP_
VueJs can have a similar issue as it expects variables to be prefixed with: VUE_APP_
In the remote case that you arrive till this point, my issue was quite dumber:
I wrongly named my env variables with colon ":" instead of equals "=". Rookie mistake but the resulting behavior was not loading the misspelled variables assignment.
# dotenv sample content
# correct assignment
USER=sample#gmail.com
# wrong assignment (will not load env var)
USER : sample#gmail.com
This solved the issue for me:
const path = require('path');
require('dotenv').config({
path: path.resolve('config.env'),
});
Be sure to load .env at the beginning of the entry file (e.g. index.js or server.js). Sometimes, the order of execution loads the environment variables after the services are initiated. And, by using __dirname, it can easily point to the file required relative to the current file.
Here my project structure is like this.
.
├─ src
│ └─ index.ts
└─ .env
// index.ts
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({path: path.join(__dirname, '..', '.env')});
...
Try this:
const dotenv = require('dotenv');
dotenv.config({ path: process.cwd() + '/config/config.env' });
worked for me idk how??
You can first debug by using console.log(require('dotenv').config()).
In my scenario, my .env file is in root directory and I need to use it in a nested directory. The result gives me { parsed: { DATABASE_URL: 'mongodb://localhost/vidly', PORT: '8080' } }. So I simply parse the result and store it in a variable const dotenv = require('dotenv').config().parsed;. Then access my DATABASE_URL like a JS object: dotenv.DATABASE_URL
It took me a few head scratches, and the tip to log the output of the require statement to console was really helpful. console.log(require('dotenv').config());
Turns out I was running my app from my user/ directory with nodemon application_name/. and that was making dotenv look for the .env file in my home dir instead of the app's. I was lazy by skipping one cd and that cost me a few minutes.
const path = require('path');
const dotenv = require('dotenv');
dotenv.config({ path: path.resolve(__dirname, '../config.env') })
In my case .env was read fine, but not .env.local.
Updating package.json to name .env into .env.local ( cp ./.env.local .env) solved the problem:
"myscript": "cp ./.env.local .env && node ./scripts/myscript.js"
if config.env file and index.js file both present in the same directory:
then, file: index.js
const path = require('path');
// Set port from environment variables
dotenv.config({path: 'config.env'})
const PORT = process.env.PORT || 8080
file: config.env:
PORT = 4000
One time I have got the same problem. Dotenv did not load .env file. I tried to fix this problem with a path config, to put .env file in a root folder, to put .env in the folder where the file is running and nothing helps me. Then I just trashed Node_modules folder, reinstall all dependencies and it works correctly
You can need the path of the .env file relative to the current working directory from where the application was launched.
You can create this path like this:
const path = require('path')
require('dotenv').config({path: path.relative(process.cwd(), path.join(__dirname,'.env'))});
process.cwd() returns the absolute path of the working directory.
__dirname returns the absolute path of the application.
path.join() adds the path of the .env-file to the path of the application. so if your .env file is nested deeper, just add the folders (e.g. path.join(__dirname, 'config', 'secret','.env'))
path.relative() creates the relative path.
I'am using:
import findUp from 'find-up';
dotenv.config({ path: findUp.sync('.env') });
I found an option debug: true to be sent in the config which showed me the following:
[dotenv][DEBUG] "PORT" is already defined in `process.env` and was NOT overwritten
I added overwrite: true and got it working:
[dotenv][DEBUG] "PORT" is already defined in `process.env` and WAS overwritten
I know I might be too late answering, but decided to share my findings after hours of checking the documentation.
Typescript.
if you are trying to resolve __dirname and you are compiling your source folder in another folder, make sure that you edit __dirname. in my case i am compiling ts in dist folder and env files are not located in dist folder, but in the root folder. so you should delete /dist from __dirname. To debug it, you can call error() function which returns error if there is problem with reading env file.
require('dotenv').config({
path: __dirname.replace('\dist','') + `${process.env.NODE_ENV}.env`
}); # To debug .error()
another thing, when you set env variables make sure that following:
no space between variable and (&&) as following
"scripts": {
"build": "npx tsc",
"start": "set NODE_ENV=production&& node dist/index.js",
},
The fastest fix here, just into seconds, add the variables to the platform you are using in my case render.com
What worked for me using Playwright / typescript:
1 create file and place at the root of the project: global-setup.ts
add inside:
async function globalSetup() {
// Configure ENV variables
require('dotenv').config()
}
export default globalSetup
2 Then use this file into playwright.config.ts
as: globalSetup: require.resolve('./global-setup'),
In this way, global conf is created, and pickup in every single test.
Use only absolute path if you are using some other tool(installed globally) to call your config file somewhere ...
require('dotenv').config({
path: '/path/to/my/project/.env',
});
One silly mistake I did was i used colon ":" in place of "="
I used below
USER:root
But it should be
USER=root
If nothing helps put your .env outside the src folder if you have folder structure like
-src/
- index.js
- env.js (where you call dotenv.config)
// you may call dotenv.config anywhere but entry point is best.
.env (file outside the src folder)
My failer was the path keyword . should be the P not capital Letter . that was so funny

How to use nconf with flatiron properly

I am trying to build a small framework using flatiron. I want to use nconf to load in all my configuration files so theyre available anywhere in my app. in my root directory I have my app.js, which i want to pull in the config data from config/bootstrap.js.
config/config/js
module.exports =
{ 'app' :
{ "host" : "localhost"
, "port" : process.env.port || 3000
}
}
bootstrap.js:
var nconf = require('nconf')
// database config
, dsource = require('./datasource')
// general or user config
, config = require('./config')
// allow overrides
nconf.overrides({
'always': 'be this value'
});
// add env vars and args
nconf.env().argv();
// load in configs from the config files
var defaults = {}
// so we can iterate over each config file
, confs = [dsource, config]
// for every config file
confs.forEach(function(conf)
{
// get each key
for (var key in conf)
{
// and add it to the defaults object
defaults[key] = conf[key]
}
})
// save the defaults object
nconf.defaults(defaults)
// logging this here works and properly shows the port setting
console.log('app port : ' + nconf.get('app:port'))
module.exports = nconf
so when console logging from in the file. everything seems to load fine. But when I try to export it, and require it from app.js as conf.get('app:port') it doesnt work.
app.js (just a vanilla app.js from 'flatiron create app')
var flatiron = require('flatiron')
, app = flatiron.app
, path = require('path')
, conf = require('./config/bootstrap')
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
app.use(flatiron.plugins.http);
app.router.get('/', function () {
this.res.json({ 'hello': 'world' })
});
// this doesnt work, conf
app.start(conf.get('app:port'));
So how can I get this to work properly so config is available anywhere in my app. ideally i would like to be able to have the config available from anywhere from something like app.config
Is this the best way to use nconf? I cant seem to find many examples. all the ones i see are just pulling config info from inside the actual nconfig example file. not from outside the file anywhere as app.config
Or am i not using it properly? Is there a better way to do it. Ideally i want to use this bootstrap file to load in all my configs, as well as resources/views (RVP style app) so its all loaded up.
This is the general idea i have for a layout, for an idea
|-- conf/
| |-- bootstrap.js
| |-- config.js
|-- resources
| |-- creature.js
|-- views/
|-- presenters/
|-- app.js
|-- package.json
Your config is available from everywhere you have access to app like this:
app.config.get('google-maps-api-key')
if you loaded it like this:
app.config.file({ file: path.join(__dirname, 'config', 'config.json') })
This is the right way to load a JSON config:
nconf.use('file', {
file: process.cwd() + '/config.ini'
, format: nconf.formats.json
});

Categories

Resources