What's the best way to overwrite a file using fs in node.js

后端 未结 3 636
耶瑟儿~
耶瑟儿~ 2021-01-01 08:35

I\'m trying to overwrite an existing file. I\'m first checking if the file exists using:

fs.existsSync(path)

If file does not exit I\'m cre

3条回答
  •  借酒劲吻你
    2021-01-01 09:26

    Found the solution. fs.writeFileSync gets a third arguments for options that can be an object. So if you get the "File already exist" you should do.

    fs.writeFileSync(path,content,{encoding:'utf8',flag:'w'})
    

提交回复
热议问题