Local Saving W/O a webpage (JS)

Question:
I need to save a player’s data in a text-based project I’m working on, but without access to any windows or webpages, I’m not sure how I can use localStorage. Is there any package, etc. that could assist with this?

Repl link:
https://replit.com/@CraftEC/PRE-ALPHA-Reyara-Randomly-Generated-Text-based-Survival?v=1 (Originally Typescript, but I’m using JS files now)

//Saves data on player's turn
let plDtSave=JSON.stringify(playerData)
localStorage.setItem("SavePlayer", plDtSave)
let ptIxSave=JSON.stringify(playerData)
localStorage.setItem("SaveIndex", ptIxSave)
//Loaded at the start of the game through getItem().

Let me know if you need any additional information - I’m most likely to check this during 8AM-2PM EST.

I’m pretty sure there’s no way to write local files for individual users in raw JS. The best advice I could give is to turn your project into a website (just by making an HTML/CSS/JS replit) and showing the console output as text on the webpage. That way, localStorage should work and it also won’t be a console app anymore, so people can play it in their browsers.

2 Likes

You mean like saving a file? (It works in nodejs). Here is what I found from a quick google search: javascript - Writing to files in Node.js - Stack Overflow

1 Like

If you’re using NodeJS, I would suggest using the fs package built-in to NodeJS:

const fs = require("node:fs");

fs.writeFile("path/to/file.txt", "Some content", function(err) {
	if (err) {
		console.error("Error");
	}
});
fs.readFile("path/to/file.txt", "utf8", function(err, data) {
	if (err) {
		console.error("Error");
		return;
	}
	console.log(data);
});
// OR
fs.writeFileSync("path/to/file.txt", "Some content");
console.log(fs.readFileSync("path/to/file.txt", "utf8"));

Obviously, you could use a JSON file instead of a text file and then use JSON.stringify and JSON.parse to make sure everything is in the correct format.

1 Like

Got it, thank you very much. I’ve come to terms with the fact that I’ll probably have to alter most of the project in some way, but this will likely allow the least required alteration of code (to my knowledge) while providing a variety of benefits. Thanks for the input, y’all!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.