node.js写文件问题



嘿,伙计们,所以我有问题,试图让我的自述文件生成。每次我把节点index.js终端fs说。Writetofile不是一个函数。但是它仍然允许我输入自述文件的信息。

这是index.js文件

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;

写入文件的方法名不叫writeToFile,实际上叫writeFile

fs.writeFile代替fs.writeToFile

编辑:下面的代码修复了这个额外括号的语法问题。查看我在评论中所写的内容。

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

相关内容

  • 没有找到相关文章

最新更新