Ask coding questions

← Back to all posts
Files don't update in editor after writing?
DuyNguyen27 (1)

I'm using Nodejs, and I'm doing a bit of reading/writing to files using fs.writeFileSync and fs.readFileSync.

Writing a file the first time is fine. Writing it the second time, the file in the editor doesn't update. It contains the old data. However, reading it back in using Nodejs, the contents of the file are as expected.

Is there a way to force the editor to display the file properly?


Here's a snip of what I'm talking about

var fs = require('fs');
function update() {
  var myContent = "This text was written at " + (new Date().toGMTString());
  fs.writeFileSync('helloworld.txt', myContent);

  console.log(fs.readFileSync("helloworld.txt").toString());
}
setInterval(update, 5000);

The code will read and output the correct results every 5 seconds, but the file in the editor won't update. This causes some major problems with data integrity. I'm trying to store an accurate log of events, and that doesn't appear to be possible on repl.it.

https://repl.it/@DuyNguyen27/Test

Answered by eankeen (771) [earned 5 cycles]
View Answer
Commentshotnewtop
eankeen (771)

Yeah so that's actually a known issue
Whether you create files from the CLI or within your program, it won't always be persistent across page refreshes. Sometimes, like you stated, the file won't even be created or written to.

They're planning to fix this issue sometime in the future. You can checkout the progression of this fix on the feedback website. :)

a5rocks (535)

So programmatic file writing is wonky in repl...

Try becoming an explorer, or just host your logs somewhere else (I recommend MongoDB Atlas).

But if you press "stop" all the edits should happen. However, if you don't stop your repl and it shutsdown after a while I don't think it saves.

https://repl.it/site/docs/repls/files#file-changes

Pythonier (303)

Well, replit is really slow when it comes to updating files.

DuyNguyen27 (1)

@Pythonier It's not that it's slow. It's that it never gets updated at all. If I refresh the repl.it window, the whole program loses the data. It reverts to whatever I last saw when I clicked on the document. It's more apparent with this code, where I print the contents of the file BEFORE writing it.

var fs = require('fs');
function update() {
  console.log("Before: " + fs.readFileSync("helloworld.txt").toString());

  var myContent = "This text was written at " + (new Date().toGMTString());
  fs.writeFileSync('helloworld.txt', myContent);

  console.log("After:  " +fs.readFileSync("helloworld.txt").toString());
}


setInterval(update, 5000);

This is the output I get:

Before: This text was written at Sat, 11 May 2019 16:58:19 GMT
After:  This text was written at Sat, 11 May 2019 17:06:57 GMT
Before: This text was written at Sat, 11 May 2019 17:06:57 GMT
After:  This text was written at Sat, 11 May 2019 17:07:02 GMT

I hit F5 and run it again, and this is the output I get

Before: This text was written at Sat, 11 May 2019 16:58:19 GMT
After:  This text was written at Sat, 11 May 2019 17:08:01 GMT
Before: This text was written at Sat, 11 May 2019 17:08:01 GMT
After:  This text was written at Sat, 11 May 2019 17:08:06 GMT

I lost whatever I wrote onto the file. It's definitely an intermittent problem, and I was able to replicate it with the above output. It's hard to know if my data is safe.

java_santiago_j (0)

@DuyNguyen27 I've been having the same exact problem, it's inconsistent , stopping doesn't ensure it will actually be written either. If you have the program read the contents of the file back to you it'll read it back properly, but upon clicking on the written file, it still has the old data.

ash15khng (471)

I think once the program stops the editor should display it properly?

DuyNguyen27 (1)

@ash15khng The program stops after I click run. The setInterval runs on its own, and there's no way to stop it in this example. It stops if I refresh the page, but the contents of the file is the older write, so I lose anything written since then when I refresh the page.