Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
how to make file when i press submit button in html file with NodeJS

when i press the submit button value of id will get and i want to make file but its showing an error for making file

code of html file:-

<label for="">Make New File : </label>
    <input type="text" id="inputfilename">
    <p id="output"></p>
    <br>
    <button id="submitbutton" onclick="submit()">Make File</button>

code for javascript and node js:-

function submit()
{
    const filename = document.getElementById("inputfilename").value;
    console.log(filename);

    //make file
    // const filepath = `C:\Users\asus\Desktop${filesystem}`;
    // console.log(filepath);
    
    const filesystem = require('fs');
    filesystem.writeFileSync('xyz.txt','Hiii',(err)=>{
    if(err)
    {
        //   console.log('Error making file...');
        document.getElementById("output").innerHTML = "Error making file...";
        return;
    }
    else
    {
        // console.log("File is created...");
        document.getElementById("output").innerHTML = "File is created!";
    }
    });
}

but its showing error like this in console:-

Uncaught ReferenceError: require is not defined
    at submit (indexjs.js:10:24)
    at HTMLButtonElement.onclick ((index):15:50)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

When you use javascript from client side you can use Blob and file-saver module for making a file.

import { saveAs } from 'file-saver';

var blob = new Blob(["Welcome to Websparrow.org."],
                { type: "text/plain;charset=utf-8" });

saveAs(blob, "static.txt");

about 4 years ago · Juan Pablo Isaza Report

0

Try this

html

<form onSubmit={handleSubmit}> 
  <label for="inputfilename">File Name: </label>
  <input type="text" name="inputfilename">
  <p id="content">some content</p>
  <button type="submit" id="submitbutton">Make File</button>
</form>

js

const fs = require('fs');

async function handleSubmit() {
  const filename = document.getElementById("inputfilename").value;
  const content = document.getElementById("content").innerHTML;

  try {
    await fs.writeFileSync(`/Users/joe/${filename}`, content);
  } catch (err) {
    console.log(err);
  }
}

You can look this Writing files with Node.js

about 4 years ago · Juan Pablo Isaza Report

0

Js

const filesystem = require('fs');
filesystem.writeFile(`demo.txt`, 'Hiii , Demo file content', (err) => {
    if (err) {
          console.log('Error making file...');
        return;
    }
    else {
        console.log("File is created..."); 
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!