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

190
Views
Node JS creating or appending file question

I am so new to node.js.

I am trying to export node.js code that generates file, and use it on script.js (which isn't js) by importing.

Can someone help?

index.js

// import fs module in which writeFile function is defined.
const fsLibrary = require('fs');

// Data which will need to add in a file.
let data = "hfsdafaweaff";

function execute() {
    
  // Write data in 'newfile.txt' .
  fsLibrary.writeFile('newfile.txt', data, (error) => {
          
      // In case of a error throw err exception.
      if (error) throw err;
  });

}


var http = require('http');
var fs = require('fs');

const PORT=8080; 

fs.readFile('./index.html', function (err, html) {

    if (err) throw err;    

    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(PORT);
});

module.export = execute;

script.js

import {execute} from './index.js'

document.getElementById("button").onclick = function() {
  console.log('hi');
  execute();
}

I couldn't find how to export node.js code to ordinary js.

about 4 years ago · Juan Pablo Isaza
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!