0

I have made a website meant to me used by only one person, so I want to dynamically write to .env file on Heroku without it resting,
because this is meant only for one person. I don’t want to deal with a database.

Something like this:

require(`dotenv`).config();

console.log(process.env.MYVAL); // Not my value
process.env.MYVAL = "MYVAL"
console.log(process.env.MYVAL); // MYVAL
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I’d suggest looking for other solutions. What about [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)? – JBallin Oct 13 '21 at 04:10
  • Admitedly it is petty cool but i want to persist and not clear after cache reset and also the validition script is on the server side –  Oct 13 '21 at 04:25
  • #1 What is **website ment**? #2 Do you need to change any environment variable at runtime without restart? #3 Where are you using that vars? Just for render html with some template or to connect to a database? – JRichardsz Oct 13 '21 at 04:47
  • #1 keeping a secret value to prove that it is the authorised user (hashed password) #2 yes - obiviously #3 you probably know where this is going –  Oct 13 '21 at 05:15
  • This is 100% an [XY problem](https://meta.stackexchange.com/q/66377/248627). Why do you want to do this? What are you trying to achieve? Environment variables are _not_ for maintaining state (and this will work particularly badly on Heroku where dynos get discarded all the time). – ChrisGPT was on strike Oct 13 '21 at 12:01
  • Please don't respond to people trying to help you for free with snarky comments like "yes - obviously". If it were obvious we wouldn't be asking for clarification. [Be nice](https://stackoverflow.com/conduct). – ChrisGPT was on strike Oct 13 '21 at 12:03

3 Answers3

0

You can set the environment variables in the settings tab on your Heroku dashboard and also using the command line. Please check the following documentation to get more information.

Configuration and Config Vars

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashis
  • 98
  • 1
  • 7
  • Yes i know that but thats not what i want i want to overrite the env var MYVAL to "MYVAL" programmatically and envfile will not work as heroku's filesystem is ephirical and all changes will be reverted to orignal –  Oct 13 '21 at 04:00
  • @Ashwin, I assure you, any environment variables you set will _also_ be lost whenever your dyno restarts, just like the ephemeral filesystem. You cannot maintain state this way. – ChrisGPT was on strike Oct 13 '21 at 12:05
  • thats the problem –  Oct 14 '21 at 02:50
0

You need to persist data (even if it is a single value). Therefore you should not write to Heroku file system nor storing it in environment variables (Heroku configuration variables).

I understand using a database could be not worth it, and in this case I would use an external file storage (Amazon S3, Dropbox, and even using GitHub private repository).

On Files on Heroku you can see some options and (Python) code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Beppe C
  • 11,256
  • 2
  • 19
  • 41
  • your answer is correct but any code samples to do it in node.js would be appreciated –  Oct 14 '21 at 03:00
  • I used Python and Java, but this is a good reference if you want to use Dropbox (you can have a free account), https://dropbox.tech/developers/writing-a-file-with-the-dropbox-javascript-sdk – Beppe C Oct 14 '21 at 06:55
  • hey just to notify you the dropbox.js link is broken and also i was intrested in github private repo –  Oct 14 '21 at 10:17
  • ah ok, I assumed dropbox as it is proper remote file storage. Using GitHub private repo is indeed possible, I have done this in Python but I am sure you can google/find an example in JS too. The workflow would be similar. – Beppe C Oct 14 '21 at 10:19
  • In your Medium blog post: 1) *Github* → *[GitHub](https://pmortensen.eu/world/EditOverflow.php?LookUpTerm=Github)* 2) *Gitlab* → *[GitLab](https://pmortensen.eu/world/EditOverflow.php?LookUpTerm=Gitlab)* – Peter Mortensen Oct 30 '21 at 11:49
0

You could use the heroku api to do that
but it will have to restart the dyno Docs