最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't find config directory in JS file - Stack Overflow

programmeradmin2浏览0评论

This is my directory structure:

/node
. /config
  . default.js
. node_modules
. /controllers
  . myfile.js
  . other files
. app.js

And here's some of code written in myfile.js

const express = require('express');
const app = express();
const mysql = require('mysql');
const path = require('path');
const config = require('config');
const bodyParser = require('body-parser');
const moment = require('moment');
app.use(bodyParser.json({type: '*/*'}));
var client;
const redis = require("redis");
client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});

const connectionPool = mysql.createPool({
    host: config.get('database.host'),
    user: config.get('database.user'),
    password: config.get('database.password'),
    database: config.get('database.dbname'),
    connectionLimit: 2
});

function selectUpdatedData() {

    connectionPool.query("SELECT * FROM rc_devices_notifications", function (err, rows, fields) {
        //DO STUFF
        process.exit();
    });
}

selectUpdatedData();

Now, when I run this file using node myfile.js, I see following error:

WARNING: No configurations found in configuration directory:/home/rc-user/node/controllers/config
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
/home/rc-user/node/node_modules/config/lib/config.js:181
    throw new Error('Configuration property "' + property + '" is not defined');
    ^

Error: Configuration property "database.host" is not defined
    at Config.get (/home/rc-user/node/node_modules/config/lib/config.js:181:11)
    at Object.<anonymous> (/home/rc-user/node/controllers/update_trackers.js:20:18)
    at Module._pile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)

However, when I run app.js (my server, that too uses config), it shows no error. It gets config files.

What am I doing wrong?

EDIT: The default.json file:

{
  "database" : {
    "host" :"something",
    "user" : "something",
    "password" : "something",
    "dbname" : "something"
  },
  "parameters" : {
    "apnkey" : "something",
    "apnkeyid" : "something",
    "apnteamid" : "something",
    "googleAPI" : "something",
    "authorization" : "something",
    "push_app_id" : "something"
  }
}

This is the config file (default.json) in config directory.

This is my directory structure:

/node
. /config
  . default.js
. node_modules
. /controllers
  . myfile.js
  . other files
. app.js

And here's some of code written in myfile.js

const express = require('express');
const app = express();
const mysql = require('mysql');
const path = require('path');
const config = require('config');
const bodyParser = require('body-parser');
const moment = require('moment');
app.use(bodyParser.json({type: '*/*'}));
var client;
const redis = require("redis");
client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});

const connectionPool = mysql.createPool({
    host: config.get('database.host'),
    user: config.get('database.user'),
    password: config.get('database.password'),
    database: config.get('database.dbname'),
    connectionLimit: 2
});

function selectUpdatedData() {

    connectionPool.query("SELECT * FROM rc_devices_notifications", function (err, rows, fields) {
        //DO STUFF
        process.exit();
    });
}

selectUpdatedData();

Now, when I run this file using node myfile.js, I see following error:

WARNING: No configurations found in configuration directory:/home/rc-user/node/controllers/config
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
/home/rc-user/node/node_modules/config/lib/config.js:181
    throw new Error('Configuration property "' + property + '" is not defined');
    ^

Error: Configuration property "database.host" is not defined
    at Config.get (/home/rc-user/node/node_modules/config/lib/config.js:181:11)
    at Object.<anonymous> (/home/rc-user/node/controllers/update_trackers.js:20:18)
    at Module._pile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)

However, when I run app.js (my server, that too uses config), it shows no error. It gets config files.

What am I doing wrong?

EDIT: The default.json file:

{
  "database" : {
    "host" :"something",
    "user" : "something",
    "password" : "something",
    "dbname" : "something"
  },
  "parameters" : {
    "apnkey" : "something",
    "apnkeyid" : "something",
    "apnteamid" : "something",
    "googleAPI" : "something",
    "authorization" : "something",
    "push_app_id" : "something"
  }
}

This is the config file (default.json) in config directory.

Share Improve this question edited Aug 28, 2017 at 7:04 Vikas asked Aug 28, 2017 at 6:53 VikasVikas 7491 gold badge10 silver badges33 bronze badges 4
  • 1 can you post the config file also, maybe there is some problem ? – Alexandru Olaru Commented Aug 28, 2017 at 7:02
  • Updated. Please check. – Vikas Commented Aug 28, 2017 at 7:05
  • please try running app from the root folder like: node controllers/myfile.js – Raghav Garg Commented Aug 28, 2017 at 7:07
  • The config seems ok, and the configuration also, maybe you are exporting a different environment like export NODE_ENV=production or other and it can't find your configuration... – Alexandru Olaru Commented Aug 28, 2017 at 7:08
Add a ment  | 

1 Answer 1

Reset to default 6

As you have in error

No configurations found in configuration directory:/home/rc-user/node/controllers/config

Since you are starting your app from subfolder, the config package is finding the config directory from that relative path.

Please use

node controllers/myfile.js

So that config will find the config directory from the root of the project.

You can use environment variable NODE_CONFIG_DIR for setting custom config directory path. Read more about NODE_CONFIG_DIR.

The default NODE_CONFIG_DIR is the /config directory under the current working directory

发布评论

评论列表(0)

  1. 暂无评论