Error: Configuration property "mongoURI" is not defined

Viewed 7684

I try to start a project but when I try to connect the mongo with the server I encounter an error that shows that the connection is not working because it can not find the folder where the connection string.

my db file:

const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI')


const connectDB = async () => {

try{
    await mongoose.connect(db);
    console.log('MongoDB connected..');
}catch(err){
    console.error(err.message);
    process.exit(1)
}
}

module.exports = connectDB;

my defualt.json:

{
 "mongoURI": "mongodb+srv://<username>:<password>@devconector- 
 zfloj.mongodb.net/test?retryWrites=true&w=majority"
}

the error: WARNING: No configurations found in configuration directory:C:\Users\Yair Azaria\Desktop\תכנות\node.js\DevConnector\config WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment. C:\Users\Yair Azaria\Desktop\node.js\DevConnector\node_modules\config\lib\config.js:203 throw new Error('Configuration property "' + property + '" is not defined'); ^

Error: Configuration property "mongoURI" is not defined at Config.get(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\node_modules\config\lib\config.js:203:1)
at Object.<anonymous> 
(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\config\db.js:3:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> 
(C:\Users\YairAzaria\Desktop\תכנות\node.js\DevConnector\server.js:2:19)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
4 Answers

you need to have a config folder on the root level of your project and a default.json file in that config folder.

In the default.json file, have your mongoURI value set up. And with that you should be able to have a value for mongoURI when you access it in your program (that you are doing now as shown in your code).

$ npm install config
$ mkdir config
$ vi config/default.json
{
  "mongoURI": ""mongodb://userID:password@mongoBox:27001/myMongoDB"
}

look in this link for more information https://www.npmjs.com/package/config

I guess you are trying to get the dbURI from config.json file which does not exist in the folder. In your case you have named it as default.json so just replace the file name in the import section of your db.js and it would work fine. As shown below

const mongoose = require('mongoose');
const defualt = require('defualt');
const db = defualt.mongoURI; 

const connectDB = async () => {

try{
    await mongoose.connect(db);
    console.log('MongoDB connected..');
}catch(err){
    console.error(err.message);
    process.exit(1)
}
}

Let me know the results!

So the issue is that you did not properly spell "default", you spelt it as "defualt.json" instead of "default.json"

You have created config folder inside "src" folder. kindly move the config folder one directory out/up.

Related