File Handling in Javascript

Is it possible to edit files with Javascript? I’m using it for a project and I don’t know if you can read/write files in JS.

if you mean in node.js, you can use the fs module to read/write files.

heres an example on how to write files using it

const fs = require('fs');

fs.writeFile('/path/to/file', 'Hello, world!', function(err) {
  if (err) throw err;
  console.log('File saved!');

and how to read files

const fs = require('fs');

fs.readFile('/path/to/file', 'utf8', function(err, data) {
  if (err) throw err;

im not completely sure about client-side but i assume this?

// create the text content
const text = "Hello, world!";

// create a Blob object with the text content
const blob = new Blob([text], { type: "text/plain" });

// create a URL for the Blob object
const url = URL.createObjectURL(blob);

// create an <a> element with the download attribute
const link = document.createElement("a"); = "file.txt";
link.href = url;

// programmatically click on the <a> element;

here are some additional resources if you need them

if anything is wrong/doesnt work just let me know so i can delete/edit this post to avoid confusion

1 Like

I have no clue how to use the node preset. I don’t know if the main website preset comes with node or not.

can you share the project name/website?

Its just the base website preset.

oh is this javascript for running on a webpage? above answer is about nodejs running on the server