简体   繁体   English

如何仍然能够从.gitignore 读取文件?

[英]How to still be able to read files from .gitignore?

Hi I'm trying to create a Discord bot using Discord.js and deploy it to Heroku.嗨,我正在尝试使用 Discord.js 创建一个 Discord 机器人并将其部署到 Heroku。

I have successfully deploy it to Heroku, but the logs shows an error.我已成功将其部署到 Heroku,但日志显示错误。 Here is where I think the problem is:这是我认为问题所在:

app[worker.1]: Error: Cannot find module './config.json'

config.json cant be found because i put the config.json file inside a .gitignore file. config.json找不到,因为我将config.json文件放在.gitignore文件中。 This is because i have some credentials (bot token) that i need to protect inside that file.这是因为我有一些我需要在该文件中保护的凭据(机器人令牌)。 However, the main file needs access to the config.json in order to run.但是,主文件需要访问config.json才能运行。

Is there any way to still protect the contents of the config.json file while still being able to run the program?有没有办法在仍然能够运行程序的同时保护config.json文件的内容? The GitHub repository is set to private, so should i exclude config.json from .gitignore instead? GitHub 存储库设置为私有,所以我应该从.gitignore中排除config.json吗?

If it's just a small side project, and your GitHub repository is private, there should be no issue in leaving the config.json out of your .gitignore .如果它只是一个小项目,并且您的 GitHub 存储库是私有的,那么将config.json离开您的.gitignore应该没有问题。 However, for larger or open-source projects, you can use Heroku's config vars , and that should do the trick.但是,对于较大的或开源项目,您可以使用 Heroku 的config vars ,这应该可以解决问题。 I recommend using config vars but if you just want to throw your code up on Heroku, leaving it out of .gitignore should be fine.我建议使用 config vars,但如果您只想将代码放在 Heroku 上,将其从.gitignore中删除应该没问题。 I would recommend config vars though, they work reliably.我会推荐配置变量,但它们工作可靠。

Thanks, Jackson谢谢, Jackson

You should use environmental variables to save tokens.您应该使用环境变量来保存令牌。 Then you change your config.json to point to those environmental variables, and not the actual token.然后你改变你的 config.json 指向那些环境变量,而不是实际的令牌。

More info on how Heroku does env variables here: https://devcenter.heroku.com/articles/config-vars有关 Heroku 如何在此处执行环境变量的更多信息: https://devcenter.heroku.com/articles/config-vars

(I'm assuming it's not having issues finding your config because it's looking for config.json when the file is actually named config.js or vice versa) (我假设它在查找您的配置时没有问题,因为它正在寻找 config.json 当文件实际命名为 config.js 时,反之亦然)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM