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

261
Visualizações
node.js writeFileSync is throwing error even when the right arg is being passes

I am trying to create a folder and then inside that create a file (index.html,styles.css,app.js),but it's throwing error that string must be passed as arg but I am already passing a string.

<pre>
let fs = require('fs');
let fileName ="";
fileName = process.argv[2] || 'demoProject';
try{
    fs.mkdirSync(fileName);
    fs.writeFileSync((fileName+"/index.html").toString());
    fs.writeFileSync((fileName+"/style.css").toString());
    fs.writeFileSync((fileName+"/app.js").toString());
}catch(e){
    console.log(e);
}
</pre>

the error given by this is :

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
at Object.writeFileSync (node:fs:2146:5)
at Object. (C:\Users\monojit debnath\Desktop\NodeJsPractice\Practice2.js:6:8)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'ERR_INVALID_ARG_TYPE'
}

</pre>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The fs.writeFileSync() needs 2 parameters.

  • path - The filepath to write
  • data - The data to write to the file. (Type Buffer OR string

You miss the data parameter. So it only knows the file, but NOT what content to write.

The result would be something like:

<pre>
const fs = require('fs');
const dirName = process.argv[2] || 'demoProject';
try{
    fs.mkdirSync(dirName);
    fs.writeFileSync(`${dirName}/index.html`, '<!-- boilerplate here -->');
    fs.writeFileSync(`${dirName}/style.css`, '');
    fs.writeFileSync(`${dirName}/app.js`, '');
}catch(e){ console.error(e); }
</pre>

docs

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