我正在使用 fs.writeFileSync 但文件没有出现在磁盘上,也没有错误

I'm using fs.writeFileSync but the file doesn't appear on the disk and there is no error either

问了一个类似的问题,它的解决方案说使用 try, catch 方法,我试过了,但文件仍然没有写入,也没有错误记录到控制台。

我试过更改目录,运行 powershell 作为管理员还验证了我的代码是否存在来自多个来源的任何可能的错误,但似乎没有任何帮助。

这是我写的代码。

const fs = require('fs');

let originalNote = {
   title : 'Some Title',
   body : 'Some Body'
}
//Converting object into JSON string 
let originalNoteString = JSON.stringify(originalNote);

//Writing JSON string to 'notes.json' file
try{
   fs.writeFileSync = ('file.json', originalNoteString);
} catch(err){
   console.log(err);
}

您需要函数调用而不是赋值

fs.writeFileSync('file.json', originalNoteString);