Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda