Use the fs. writeFile() method to write content to the specified file. The syntax format is as follows:
fs.writeFile(file,data[,options],callback)
Parameter interpretation:
File: string specifying a file path, representing the file storage path, a required parameter
Data: content to be written, required parameter
Options: What format to write the file in? Optional parameter. The default value is utf8
Callback: the callback function after the file is written, a required parameter
// 1.导入fs文件系统模块 const fs=require('fs') // 2.调用fs.writeFile()方法,写入文件内容 // 参数一:指定一个文件路径的字符串,表示文件的存放路径,必选参数 // 参数二:要写入的内容,必选参数 // 参数三:文件写入完成后的回调函数,必选参数 fs.writeFile('./file.txt','hello Node.js',function(err,datastr){ console.log(err) })