fs.writefile 不是函数/打字稿/JSON

fs.writefile is not a function / Typescript / JSON

每当我尝试将数据写入 .json 文件时,我都会收到错误消息。它说 'fs.writefile' 不是函数。

我的函数:

const fs = require('fs');


function write(data: Map<string, number>) {
    fs.writefile("users.json", JSON.stringify(data), function(err: any) {
        if (err) throw err;
        console.log('complete');
        }
    );
}

错误: (节点:4408)UnhandledPromiseRejectionWarning:类型错误:fs.writefile 不是函数

有人可以帮我解决这个问题吗?

Javascript 属性 名称区分大小写。 writeFile 方法有大写字母 F,但你有 writefile 和小写字母 f

参见:https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback