filename or file descriptor
v0.1.29
Asynchronously writes data to a file, replacing the file if it already exists.
A path to a file. If a URL is provided, it must use the file: protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
Generated using TypeDoc
When
fileis a filename, asynchronously writes data to the file, replacing the file if it already exists.datacan be a string or a buffer.When
fileis a file descriptor, the behavior is similar to callingfs.write()directly (which is recommended). See the notes below on using a file descriptor.The
encodingoption is ignored ifdatais a buffer.The
modeoption only affects the newly created file. See open for more details.If
optionsis a string, then it specifies the encoding:It is unsafe to use
fs.writeFile()multiple times on the same file without waiting for the callback. For this scenario, createWriteStream is recommended.Similarly to
fs.readFile-fs.writeFileis a convenience method that performs multiplewritecalls internally to write the buffer passed to it. For performance sensitive code consider using createWriteStream.It is possible to use an
AbortSignalto cancel anfs.writeFile(). Cancelation is "best effort", and some amount of data is likely still to be written.Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering
fs.writeFileperforms.