【问题标题】:Can't find config directory in JS file在 JS 文件中找不到配置目录
【发布时间】:2017-08-28 06:53:41
【问题描述】:

这是我的目录结构:

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

这里有一些用 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();

现在,当我使用 node myfile.js 运行此文件时,我看到以下错误:

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)。

【问题讨论】:

  • 你能把配置文件也发一下吗,也许有问题?
  • 已更新。请检查。
  • 请尝试从根文件夹运行应用程序,例如:node controllers/myfile.js
  • 配置看起来没问题,配置也一样,也许你正在导出一个不同的环境,比如export NODE_ENV=production或其他,它找不到你的配置...

标签: javascript node.js path directory config


【解决方案1】:

因为你有错误

在配置目录下找不到配置:/home/rc-user/node/controllers/config

由于您是从子文件夹启动应用程序,config 包正在从该相对路径中查找配置目录。

请使用

node controllers/myfile.js

这样config 就会从项目的根目录中找到配置目录。

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

默认的NODE_CONFIG_DIR是当前工作目录下的/config目录

【讨论】:

  • 是的,它奏效了。但是有没有办法为我的配置文件夹指定这个路径?
  • 如果这回答了您的问题,请考虑接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多