关于node.js:Node-config将不会在Heroku中使用custom-environment-variables.json中的变量

Node-config won't use variables from custom-environment-variables.json in Heroku

我正在使用node-config模块获取mongo连接字符串和jwt密钥。

例如:

1
2
if(!config.get('jwtPrivateKey')){
    throw new Error('FATAL ERROR: jwtPrivateKey is not defined');

我的custom-environment-variables.json文件如下所示:

1
2
3
4
{
   "jwtPrivateKey":"video-app_jwtPrivateKey",
   "db":"video-app_db"
}

default.json

1
2
3
4
{
   "db":"default db",
   "jwtPrivateKey":"default key"
}

production.json

1
2
3
4
{
   "db":"production db",
   "jwtPrivateKey":"production key"
}

长话短说-尽管环境变量是在heroku中设置的,但node-config不会查看在custom-environment-variables.json中设置的值。我可以更改NODE_ENV并获取相关的json文件的硬编码值,但是从未使用过环境变量,这似乎与docs

相矛盾


我有同样的问题。从heroku的键中删除了连字符,以便从自定义环境变量读取数据。

so custom-environment-variables.json:

1
2
3
4
{
   "jwtPrivateKey":"video-app_jwtPrivateKey",
   "db":"video-app_db"
}

成为:

1
2
3
4
{
   "jwtPrivateKey":"videoapp_jwtPrivateKey",
   "db":"videoapp_db"
}

Heroku文档:

Config var policies

  • Config var keys should use only alphanumeric characters and the underscore character (_) to ensure that they are accessible from all
    programming languages. Config var keys should not include the hyphen
    character.
  • Config var data (the combination of all keys and values) cannot exceed 32kb for each app.
  • Config var keys should not begin with a double underscore (__).
  • A config vara€?s key should not begin with HEROKU_ unless it is set by the Heroku platform itself.

您可以在Heroku中以这种方式配置vars


因此,我遇到了同样的问题:我部署了我的应用程序,在Heroku中设置了环境变量,但是没有任何效果(不同于本地机器,一切正常)。
似乎问题出在我不小心将所有node_modules文件夹添加到了git中。发生这种情况是因为我在进行首次提交后创建了一个.gitignore文件(如果我正确的话)。

我所做的对我有用:
我从git中删除了node_modules并提交/推送了对Heroku的更改:

1
git rm -rf node_modules

然后提交并推送。

这是我收到的错误消息:

Error: /app/node_modules/bcrypt/lib/binding/bcrypt_lib.node: invalid ELF header

我花了两天时间试图弄清楚。
希望对您有所帮助。