You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js运行index.js报fs.writeToFile不是函数,无法正常生成README文件

问题排查与解决方法

1. 修复fs方法调用错误

你遇到的fs.writeToFile不是一个函数报错,是因为Node.js 内置fs模块根本没有writeToFile这个API,正确的写入文件方法名为fs.writeFile。同时你的代码还存在语法错误:回调函数被错误放在了方法调用的括号外侧,没有作为参数传入。

修正index.js中inquirer.prompt().then()内的代码如下:

}).then((data) => {
  const filename = `${data.title}.md`;
  // 替换为正确的fs.writeFile,回调函数放在方法括号内
  fs.writeFile(filename, generateMarkdown(data), (err) => {
    if (err){ 
        return console.log(err);
    }
    console.log('you have created a readme file.')
  })
}

2. 修复generateMarkdown参数调用错误

你当前的generateMarkdown文件导入了无用的InputPrompt模块,且没有使用用户输入的data参数,直接读取InputPrompt的属性会导致最终生成的README内容全部为undefined。

修正后的generateMarkdown.js完整代码如下:

// 删掉无用的InputPrompt导入
function generateMarkdown(data) {
  return `
  # ${data.title}
    
  ## Description
      
  ${data.description}
    
    
  ## Installation
    
  ${data.installation}
    
  ## Usage
    
  ${data.usage}
    
  ## Github 用户名
     
  ${data.name}
  `;
}

module.exports = generateMarkdown;

3. 验证效果

修改完成后重新在终端执行node index.js,完成信息输入后不会再报错,会在当前项目目录下生成对应名称的README文件,内容会正确填充你输入的信息。


内容的提问来源于stack exchange,提问作者Joseph Hill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 05:30:05