Create an empty file in Node.js?

后端 未结 4 1992
礼貌的吻别
礼貌的吻别 2020-12-23 23:55

For now I use

fs.openSync(filepath, \'a\')

But it\'s a little tricky. Is there a \'standard\' way to create an empty file in Node.js?

4条回答
  •  死守一世寂寞
    2020-12-24 00:40

    If you want it to be just like the UNIX touch I would use what you have fs.openSync(filepath, 'a') otherwise the 'w' will overwrite the file if it already exists and 'wx' will fail if it already exists. But you want to update the file's mtime, so use 'a' and append nothing.

提交回复
热议问题