Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
Read and Write files from 'fs' not working asynchronously

I am trying to build a script which read and write some files. The functions are working fine but I am having trouble making the read/write functions asynchronously. The main problem I have is that I am trying to write a file and then I am trying to import it. For example: given these two functions, the first one is writing to ./myFile.js and the second function is trying to import the file like this:

var fs = require('fs');
const myFile = require('./myFile')

async function writeMyFile() {
    return new Promise((resolve, reject) => {
        fs.promises.readFile('./myFile.js', 'utf8').then((file) => {

        const replacedData = file + 'newLine'

            fs.promises
                .writeFile('./myFile.js', replacedData)
                .then(() => resolve('Writted'))
                .catch(() => reject('Error'));
        });
    });
}

function importMyFile() {
    console.log(myFile) // this is always empty
}

await writeMyFile()
importMyFile()

The thing is that I can see myFile is being written correctly, but I cannot see it in the importMyFile function. When I try these functions separately it works (executing the first one and then the second one)

I tried the following:

  • Using the promise functions from fs library: fs.promises.readFile() and fs.promises.writeFile() (example above)
  • Returning a Promise which resolves when writeMyFile ends. (example above)
  • Importing the file directly in the readMyFile function like this:
function importMyFile() {
    const myFile = require('./myFile')
    console.log(myFile) // this is always empty
}
  • Without returning new Promise like this:
async function writeMyFile() {
    const file = await fs.promises.readFile('./myFile.js', 'utf8')

    const replacedData = file + 'newLine'

    await fs.promises.writeFile('./myFile.js', replacedData)
}

I also see that in the console, the order of the logs are these:

'Writted'
{} //empty file

Keep in mind that this is a simplified version of my case.

EDIT the file I am trying to write and import looks like this ALWAYS:

module.exports = []

And the it is populated, it looks like this:

const myModule = {
    name: 'name',
    surname: 'surname'
}

module.exports = [myModule]

I hope someone knows what is going on here. Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using require is different than readFile. Using require loads the .js file as a module and brings in its exported elements for use in your code. If you're just interested in printing out the text inside of the .js file, then use fs.readFileSync to do so (you don't need promises if you use the synchronous version of the call).

The code you have will add the string "newLine" to the end of the file, but if your intent was to add an actual newline it should be adding "\n" instead.

A simpler version of your code might look like this:

function writeMyFile() {
  const file = fs.readFileSync('./myFile.js', 'utf8');
  const replacedData = file + '\n';
  fs.writeFileSync('./myFile.js', replacedData);
  return 'Written';
}

function importMyFile() {
  // actually read the file instead of using require()
  const file = fs.readFileSync('./myFile.js', 'utf8');
  console.log(file);
}

writeMyFile()
importMyFile()

Since no asynchronous calls are made, there's no need for promises or awaiting.

If you did intend to use the .js code inside, and it's important to use require then note that once you load it that way into memory, it does not get refreshed after the file on disk changes. Even if you call require a subsequent time it will be smart and say "oh, I already have that one in memory" and not reload it. So delay calling require('./myFile') until after any necessary changes are made and put the require statement inside of importMyFile().

about 4 years ago · Juan Pablo Isaza Relatório

0

await writeMyFile()
importMyFile()

Has to be inside an async function.

For example, calling them like this, and this should work.


(async ()=>{

await writeMyFile()
importMyFile()

})()

Also the output, {} means that there is no module exported from myfile.js.

Edit: The "myFile" variable is containing old data. As it was imported at the beginning of execution. The importMyFile should re import myfile.js.


function importMyFile() {
    myFile = require('./myFile')
    console.log(myFile)  
}

After this the new changes by the writeMyFile will be shown in the console, if the function changed myfile.js's export.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda