How do I write to a local file using javascript

Greetings to the community. Hope you’re all doing OK.

I’m using javascript for programming on my own computer (not for a webpage on the net) so the usual security concerns are not an issue. How do I write to a file on my own computer? I can write and retrieve a string using local storage e.g. window.localStorage.setItem(‘games’, games) and
window.localStorage.getItem(‘games’), but I’d like to be able to write strings to a file on my computer.

Would greatly appreciate some help especially a block of code which I could use directly to do that.

Well you can say ‘the usual security concerns are not an issue’.

Javascript does not understand you.

The security barriers are in place for a reason.

That said;

What you’re describing is a good case for learning Node.js, or another Local Computer Javascript Execution environment. They are not limited by this security barrier, because they’re running code on your own computer.

Your web browser will not be able to do this, other than being able to prompt you to download a file.

You can’t with browser JavaScript.

https://nodejs.org/en/knowledge/file-system/how-to-write-files-in-nodejs/

Thanks much. When I said security not an issue I was hoping for some way to circumvent the problem when working in local environment but I take your point and will look into Node. Thanks again.

This looks like the way to go. Much appreciated.

1 Like

as is every web based hacker and virus injector. :stuck_out_tongue:

Node is definitely the way to go, plus it’s really the tool you’re trying to use (not for the web, access to the file system, etc.).

1 Like

So basically want to use a HTML page in the browser as a GUI for your application? In that case you might have a look at electron, which does exactly that – it starts a dedicated chrome instance so you can write your app like any web page. Of course, if you want to access the file system you’ll eventually have to use the node API anyway.

PS: Another option might be writing your own chrome extension or app, which would again provide some abstraction layers for file system access. For a simple app this might unnecessarily add complexity though.

1 Like

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