本文介绍了如何在Node.js App Heroku中的.env文件中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在名为 .env

MONGOCONNECTIONSTRING = connectionstring

当我运行 heroku本地网络时说

[确定]以KEY = VALUE格式加载ENV .env文件

我是尝试使用这样的环境变量

I am trying to use the environment variables like this

const connectionString = process.env.MONGOCONNECTIONSTRING

在我的index.js文件中

in my index.js file

当我尝试打印变量connectionstring时,它是未定义的。如何访问变量。

when I try to print the variable connectionstring it is undefined. How do I access the variables.

Heroku的说要这样做,但是由于变量永远不可用,我必须缺少一些东西。

Heroku's best practices say to do that however I must be missing something since the variables are never available.

推荐答案

Heroku上的常规做法是,您可以通过Heroku仪表板或CLI命令通过设置config vars来配置应用程序。建议使用CLI,因为您可以一次设置多个变量:

The general practice on Heroku is that you configure your application by setting config vars either via Heroku Dashboard or CLI command. Using CLI is recommended because you can set multiple variables at once:

heroku config:set MONGOCONNECTIONSTRING=connectionstring

.env 文件应仅用于本地开发,而您不应该将此文件提交到存储库。

.env file should be used only for local development and you shouldn't commit this file to the repository. It is actually mentioned in the docs that you linked.

我不知道您是否在Heroku上配置了MongoDB插件?如果是这样,它应该自动设置适当的连接字符串。也许您应该检查一下文档。

Also I wonder if you have MongoDB addon provisioned on Heroku? If so, it should set appropriate connection string automatically. Maybe you should check the docs for that.

这篇关于如何在Node.js App Heroku中的.env文件中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:50