Joseph Hill
Joseph Hill

Reputation: 1

node.js write to file problems

hey guys so I'm having problems with trying to get my readme file to generate. Every time I put node index.js in to the terminal it says the fs.writetofile is not a function. but it still lets me put in the information of the readme.

this is the index.js file

const inquirer = require('inquirer');
const fs = require('fs');
const generateMarkdown = require('./utils/generateMarkdown');
const path = require('path');

// TODO: Create an array of questions for user input
//this is the array of questions to put in the readme

function init(){

    inquirer.prompt([

        { 
          type: "input",
          message: "What is the title",
          name: "title",  
             },

        { 
          type: "input",
          message: "give a description",
          name: "description",  
             },

        {
           type: "input",
           message: "how do you install it",
           name: "installation",
        },
        
        {
            type: "input",
            message: "How do use it",
            name: "usage",
        },
          
        {
            type: "input",
            message: "github username",
            name: "name"
        },
        
      ]).then((data) => {
        const filename = `${data.title}.md`;

        fs.writeToFile(filename, generateMarkdown(data)), (err) =>{
          if (err){ 
              return console.log(err);
          }
          console.log('you have created a readme file.')
        }

      }
      );
    };

    
      
    //initialize app

    init();

//this is the generatmarkdownfile
    const InputPrompt = require("inquirer/lib/prompts/input");

//function for readme 
function generateMarkdown(data) {
  return `
  # ${InputPrompt.title}
    
  ## Description
      
  ${InputPrompt.description}
    
    
  ## Installation
    
  ${InputPrompt.installation}
    
  ## Usage
    
  ${InputPrompt.usage}
    
  ## Name
     
  ${InputPrompt.name}
  `;
  }
   

module.exports = generateMarkdown;

Upvotes: 0

Views: 73

Answers (1)

d_m
d_m

Reputation: 196

Method name to write to file is not called writeToFile but it's called writeFile actually.

So replace fs.writeToFile with fs.writeFile.

Edit: The code below fixes the syntax issue you have with that extra parenthesis. Check what I have written in the comments.

fs.writeFile(filename, generateMarkdown(data), (err) => {
    if (err) {
        return console.log(err);
    }
    console.log('you have created a readme file.')
});

Upvotes: 1

Related Questions