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

使用Inquirer v8.2.4时出现.then is not a function错误求助

问题解决:TypeError: inquirer.createPromptModule(...).then is not a function

你的问题出在两个关键API用法错误上,和Inquirer版本无关:

错误1:createPromptModule用法错误

Inquirer v8中,createPromptModule()方法不接受questions参数,它的作用是创建一个自定义的prompt函数。正确流程是先调用createPromptModule()得到prompt函数,再用这个函数传入questions,才会返回可调用.then()的Promise。

错误2:文件写入方法错误

你在writeToFile里误用了FontFaceSet.writeToFile,这是不存在的方法,应该使用已引入的fs模块的fs.writeFile。


修正后的完整代码

const inquirer = require("inquirer");
const fs = require("fs");
const generateMarkdown = require("./util/generateMarkdown.js");

const questions = [
    {
        type: "input",
        message: "What is your Github username?",
        name: "Username",
    },
    {
        type: "input",
        message: "What is your Email adress?",
        name: "email" 
    },
    {
        type: "input",
        message: "What is your projects name?",
        name: "project",
    },
    {
        type: "input",
        message: "Description of your project:",
        name: "description",
    },
    {
        type: "checkbox",
        message: "What license does your project require? (SPACEBAR to checkmark and ENTER to confirm)",
        name: "license",
        choices: ['MIT', 'APACHE 2.0', 'GPL 3.0', 'BSD 3', 'N/A'],
    },
    {
        type: "input",
        message: "What command should run dependencies?",
        name: "install",
    },
    {
        type: "input",
        message: "what command should be used to run tests?",
        name: "test",
    },
    {
        type: "input",
        message: "What does the user need to know about this repo?",
        name: "use",
    },
    {
        type: "input",
        message: "How does the user contribute to this repo?",
        name: "contribute",
    },
];

function writeToFile(fileName, data) {
    // 修正:使用fs.writeFile替代不存在的FontFaceSet.writeToFile
    fs.writeFile(fileName, data, function (err) {
        if (err) throw err;
        console.log("File Created");
    })
}

function init() {
    // 修正:先创建prompt模块函数,再传入questions调用
    const prompt = inquirer.createPromptModule();
    prompt(questions).then((data) => {
        const readmeFile = generateMarkdown(data);
        writeToFile("generatedReadMe.md", readmeFile);
    })
}

init();

简化写法(可选)

如果不需要自定义prompt模块,直接用inquirer.prompt(questions)更简洁,效果完全一致:

function init() {
    inquirer.prompt(questions).then((data) => {
        const readmeFile = generateMarkdown(data);
        writeToFile("generatedReadMe.md", readmeFile);
    })
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 09:15:36