在找不到JS文件config目录

问题描述:

这是我的目录结构node myfile.js,我看到以下错误:在找不到JS文件config目录

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._compile (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) 

然而,当我运行app.js(我的服务器,这也使用配置),它翔没有错误。它获取配置文件。

我在做什么错?

编辑:default.json文件:

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

这是config目录中的配置文件(default.json)。

+1

你也可以发布配置文件,也许有一些问题吗? –

+0

已更新。请检查。 –

+0

请尝试从根文件夹中运行应用程序,例如:'node controllers/myfile.js' – RaghavGarg

正如你在错误

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

既然你开始从子文件夹应用程式后,config包是找到从相对路径config目录。

请使用

node controllers/myfile.js 

这样config会发现从项目的根目录下的config目录。

您可以使用环境变量NODE_CONFIG_DIR来设置自定义配置目录路径。阅读更多关于NODE_CONFIG_DIR

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

+0

是的,它的工作。但是没有任何方法可以为我的配置文件夹指定这个路径吗? –

+0

已更新的答案,请查看。 – RaghavGarg

+0

好吧,它的工作原理。谢谢。 –