有没有一种方法可以在附加数据之前清除 csv 文件中的内容?

Is there a method for clearing out the contents in a csv file before appending data?

我想在添加新数据之前覆盖或清除 csv 文件中的内容。有方法吗?

只需使用 fs.writeFile 写入文件,它将覆盖现有文件(如果存在)。

const fs = require('fs');
fs.writeFile('message.csv', 'new content', (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
});