- Assertion testing
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Policies
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Worker threads
- Zlib
Node.js v14.21.3 documentation
Table of contents
- File system
- Promise example
- Callback example
- Synchronous example
- Promises API
- Class:
FileHandle
filehandle.appendFile(data[, options])
filehandle.chmod(mode)
filehandle.chown(uid, gid)
filehandle.close()
filehandle.datasync()
filehandle.fd
filehandle.read(buffer, offset, length, position)
filehandle.read([options])
filehandle.readFile(options)
filehandle.readv(buffers[, position])
filehandle.stat([options])
filehandle.sync()
filehandle.truncate(len)
filehandle.utimes(atime, mtime)
filehandle.write(buffer[, offset[, length[, position]]])
filehandle.write(string[, position[, encoding]])
filehandle.writeFile(data, options)
filehandle.writev(buffers[, position])
fsPromises.access(path[, mode])
fsPromises.appendFile(path, data[, options])
fsPromises.chmod(path, mode)
fsPromises.chown(path, uid, gid)
fsPromises.copyFile(src, dest[, mode])
fsPromises.lchmod(path, mode)
fsPromises.lchown(path, uid, gid)
fsPromises.lutimes(path, atime, mtime)
fsPromises.link(existingPath, newPath)
fsPromises.lstat(path[, options])
fsPromises.mkdir(path[, options])
fsPromises.mkdtemp(prefix[, options])
fsPromises.open(path, flags[, mode])
fsPromises.opendir(path[, options])
fsPromises.readdir(path[, options])
fsPromises.readFile(path[, options])
fsPromises.readlink(path[, options])
fsPromises.realpath(path[, options])
fsPromises.rename(oldPath, newPath)
fsPromises.rmdir(path[, options])
fsPromises.rm(path[, options])
fsPromises.stat(path[, options])
fsPromises.symlink(target, path[, type])
fsPromises.truncate(path[, len])
fsPromises.unlink(path)
fsPromises.utimes(path, atime, mtime)
fsPromises.watch(filename[, options])
fsPromises.writeFile(file, data[, options])
- Class:
- Callback API
fs.access(path[, mode], callback)
fs.appendFile(path, data[, options], callback)
fs.chmod(path, mode, callback)
fs.chown(path, uid, gid, callback)
fs.close(fd[, callback])
fs.copyFile(src, dest[, mode], callback)
fs.createReadStream(path[, options])
fs.createWriteStream(path[, options])
fs.exists(path, callback)
fs.fchmod(fd, mode, callback)
fs.fchown(fd, uid, gid, callback)
fs.fdatasync(fd, callback)
fs.fstat(fd[, options], callback)
fs.fsync(fd, callback)
fs.ftruncate(fd[, len], callback)
fs.futimes(fd, atime, mtime, callback)
fs.lchmod(path, mode, callback)
fs.lchown(path, uid, gid, callback)
fs.lutimes(path, atime, mtime, callback)
fs.link(existingPath, newPath, callback)
fs.lstat(path[, options], callback)
fs.mkdir(path[, options], callback)
fs.mkdtemp(prefix[, options], callback)
fs.open(path[, flags[, mode]], callback)
fs.opendir(path[, options], callback)
fs.read(fd, buffer, offset, length, position, callback)
fs.read(fd, [options,] callback)
fs.readdir(path[, options], callback)
fs.readFile(path[, options], callback)
fs.readlink(path[, options], callback)
fs.readv(fd, buffers[, position], callback)
fs.realpath(path[, options], callback)
fs.realpath.native(path[, options], callback)
fs.rename(oldPath, newPath, callback)
fs.rmdir(path[, options], callback)
fs.rm(path[, options], callback)
fs.stat(path[, options], callback)
fs.symlink(target, path[, type], callback)
fs.truncate(path[, len], callback)
fs.unlink(path, callback)
fs.unwatchFile(filename[, listener])
fs.utimes(path, atime, mtime, callback)
fs.watch(filename[, options][, listener])
fs.watchFile(filename[, options], listener)
fs.write(fd, buffer[, offset[, length[, position]]], callback)
fs.write(fd, string[, position[, encoding]], callback)
fs.writeFile(file, data[, options], callback)
fs.writev(fd, buffers[, position], callback)
- Synchronous API
fs.accessSync(path[, mode])
fs.appendFileSync(path, data[, options])
fs.chmodSync(path, mode)
fs.chownSync(path, uid, gid)
fs.closeSync(fd)
fs.copyFileSync(src, dest[, mode])
fs.existsSync(path)
fs.fchmodSync(fd, mode)
fs.fchownSync(fd, uid, gid)
fs.fdatasyncSync(fd)
fs.fstatSync(fd[, options])
fs.fsyncSync(fd)
fs.ftruncateSync(fd[, len])
fs.futimesSync(fd, atime, mtime)
fs.lchmodSync(path, mode)
fs.lchownSync(path, uid, gid)
fs.lutimesSync(path, atime, mtime)
fs.linkSync(existingPath, newPath)
fs.lstatSync(path[, options])
fs.mkdirSync(path[, options])
fs.mkdtempSync(prefix[, options])
fs.opendirSync(path[, options])
fs.openSync(path[, flags[, mode]])
fs.readdirSync(path[, options])
fs.readFileSync(path[, options])
fs.readlinkSync(path[, options])
fs.readSync(fd, buffer, offset, length, position)
fs.readSync(fd, buffer, [options])
fs.readvSync(fd, buffers[, position])
fs.realpathSync(path[, options])
fs.realpathSync.native(path[, options])
fs.renameSync(oldPath, newPath)
fs.rmdirSync(path[, options])
fs.rmSync(path[, options])
fs.statSync(path[, options])
fs.symlinkSync(target, path[, type])
fs.truncateSync(path[, len])
fs.unlinkSync(path)
fs.utimesSync(path, atime, mtime)
fs.writeFileSync(file, data[, options])
fs.writeSync(fd, buffer[, offset[, length[, position]]])
fs.writeSync(fd, string[, position[, encoding]])
fs.writevSync(fd, buffers[, position])
- Common Objects
- Class:
fs.Dir
- Class:
fs.Dirent
- Class:
fs.FSWatcher
- Class:
fs.StatWatcher
- Class:
fs.ReadStream
- Class:
fs.Stats
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isDirectory()
stats.isFIFO()
stats.isFile()
stats.isSocket()
stats.isSymbolicLink()
stats.dev
stats.ino
stats.mode
stats.nlink
stats.uid
stats.gid
stats.rdev
stats.size
stats.blksize
stats.blocks
stats.atimeMs
stats.mtimeMs
stats.ctimeMs
stats.birthtimeMs
stats.atimeNs
stats.mtimeNs
stats.ctimeNs
stats.birthtimeNs
stats.atime
stats.mtime
stats.ctime
stats.birthtime
- Stat time values
- Class:
fs.WriteStream
fs.constants
- Class:
- Notes
File system#
Source Code: lib/fs.js
The fs
module enables interacting with the file system in a
way modeled on standard POSIX functions.
To use the promise-based APIs:
import * as fs from 'fs/promises';
const fs = require('fs/promises');
To use the callback and sync APIs:
import * as fs from 'fs';
const fs = require('fs');
All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
Promise example#
Promise-based operations return a promise that is fulfilled when the asynchronous operation is complete.
import { unlink } from 'fs/promises';
try {
await unlink('/tmp/hello');
console.log('successfully deleted /tmp/hello');
} catch (error) {
console.error('there was an error:', error.message);
}
const { unlink } = require('fs/promises');
(async function(path) {
try {
await unlink(path);
console.log(`successfully deleted ${path}`);
} catch (error) {
console.error('there was an error:', error.message);
}
})('/tmp/hello');
Callback example#
The callback form takes a completion callback function as its last
argument and invokes the operation asynchronously. The arguments passed to
the completion callback depend on the method, but the first argument is always
reserved for an exception. If the operation is completed successfully, then
the first argument is null
or undefined
.
import { unlink } from 'fs';
unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
const { unlink } = require('fs');
unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
The callback-based versions of the fs
module APIs are preferable over
the use of the promise APIs when maximal performance (both in terms of
execution time and memory allocation are required).
Synchronous example#
The synchronous APIs block the Node.js event loop and further JavaScript
execution until the operation is complete. Exceptions are thrown immediately
and can be handled using try…catch
, or can be allowed to bubble up.
import { unlinkSync } from 'fs';
try {
unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
} catch (err) {
// handle the error
}
const { unlinkSync } = require('fs');
try {
unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
} catch (err) {
// handle the error
}
Promises API#
The fs/promises
API provides asynchronous file system methods that return
promises.
The promise APIs use the underlying Node.js threadpool to perform file system operations off the event loop thread. These operations are not synchronized or threadsafe. Care must be taken when performing multiple concurrent modifications on the same file or data corruption may occur.
Class: FileHandle
#
A <FileHandle> object is an object wrapper for a numeric file descriptor.
Instances of the <FileHandle> object are created by the fsPromises.open()
method.
All <FileHandle> objects are <EventEmitter>s.
If a <FileHandle> is not closed using the filehandle.close()
method, it will
try to automatically close the file descriptor and emit a process warning,
helping to prevent memory leaks. Please do not rely on this behavior because
it can be unreliable and the file may not be closed. Instead, always explicitly
close <FileHandle>s. Node.js may change this behavior in the future.
filehandle.appendFile(data[, options])
#
data
<string> | <Buffer> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable> | <Stream>options
<Object> | <string>- Returns: <Promise> Fulfills with
undefined
upon success.
Alias of filehandle.writeFile()
.
When operating on file handles, the mode cannot be changed from what it was set
to with fsPromises.open()
. Therefore, this is equivalent to
filehandle.writeFile()
.
filehandle.chmod(mode)
#
Modifies the permissions on the file. See chmod(2)
.
filehandle.chown(uid, gid)
#
uid
<integer> The file's new owner's user id.gid
<integer> The file's new group's group id.- Returns: <Promise> Fulfills with
undefined
upon success.
Changes the ownership of the file. A wrapper for chown(2)
.
filehandle.close()
#
- Returns: <Promise> Fulfills with
undefined
upon success.
Closes the file handle after waiting for any pending operation on the handle to complete.
import { open } from 'fs/promises';
let filehandle;
try {
filehandle = await open('thefile.txt', 'r');
} finally {
await filehandle?.close();
}
filehandle.datasync()
#
- Returns: <Promise> Fulfills with
undefined
upon success.
Forces all currently queued I/O operations associated with the file to the
operating system's synchronized I/O completion state. Refer to the POSIX
fdatasync(2)
documentation for details.
Unlike filehandle.sync
this method does not flush modified metadata.
filehandle.fd
#
- <number> The numeric file descriptor managed by the <FileHandle> object.
filehandle.read(buffer, offset, length, position)
#
buffer
<Buffer> | <TypedArray> | <DataView> A buffer that will be filled with the file data read.offset
<integer> The location in the buffer at which to start filling. Default:0
length
<integer> The number of bytes to read. Default:buffer.byteLength
position
<integer> The location where to begin reading data from the file. Ifnull
, data will be read from the current file position, and the position will be updated. Ifposition
is an integer, the current file position will remain unchanged.- Returns: <Promise> Fulfills upon success with an object with two properties:
bytesRead
<integer> The number of bytes readbuffer
<Buffer> | <TypedArray> | <DataView> A reference to the passed inbuffer
argument.
Reads data from the file and stores that in the given buffer.
If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
filehandle.read([options])
#
options
<Object>buffer
<Buffer> | <TypedArray> | <DataView> A buffer that will be filled with the file data read. Default:Buffer.alloc(16384)
offset
<integer> The location in the buffer at which to start filling. Default:0
length
<integer> The number of bytes to read. Default:buffer.byteLength
position
<integer> The location where to begin reading data from the file. Ifnull
, data will be read from the current file position, and the position will be updated. Ifposition
is an integer, the current file position will remain unchanged. Default::null
- Returns: <Promise> Fulfills upon success with an object with two properties:
bytesRead
<integer> The number of bytes readbuffer
<Buffer> | <TypedArray> | <DataView> A reference to the passed inbuffer
argument.
Reads data from the file and stores that in the given buffer.
If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
filehandle.readFile(options)
#
options
<Object> | <string>encoding
<string> | <null> Default:null
signal
<AbortSignal> allows aborting an in-progress readFile
- Returns: <Promise> Fulfills upon a successful read with the contents of the
file. If no encoding is specified (using
options.encoding
), the data is returned as a <Buffer> object. Otherwise, the data will be a string.
Asynchronously reads the entire contents of a file.
If options
is a string, then it specifies the encoding
.
The <FileHandle> has to support reading.
If one or more filehandle.read()
calls are made on a file handle and then a
filehandle.readFile()
call is made, the data will be read from the current
position till the end of the file. It doesn't always read from the beginning
of the file.
filehandle.readv(buffers[, position])
#
buffers
<Buffer[]> | <TypedArray[]> | <DataView[]>position
<integer> The offset from the beginning of the file where the data should be read from. Ifposition
is not anumber
, the data will be read from the current position.- Returns: <Promise> Fulfills upon success an object containing two properties:
bytesRead
<integer> the number of bytes readbuffers
<Buffer[]> | <TypedArray[]> | <DataView[]> property containing a reference to thebuffers
input.
Read from a file and write to an array of <ArrayBufferView>s
filehandle.stat([options])
#
options
<Object>bigint
<boolean> Whether the numeric values in the returned <fs.Stats> object should bebigint
. Default:false
.
- Returns: <Promise> Fulfills with an <fs.Stats> for the file.
filehandle.sync()
#
- Returns: <Promise> Fufills with
undefined
upon success.
Request that all data for the open file descriptor is flushed to the storage
device. The specific implementation is operating system and device specific.
Refer to the POSIX fsync(2)
documentation for more detail.
filehandle.truncate(len)
#
Truncates the file.
If the file was larger than len
bytes, only the first len
bytes will be
retained in the file.
The following example retains only the first four bytes of the file:
import { open } from 'fs/promises';
let filehandle = null;
try {
filehandle = await open('temp.txt', 'r+');
await filehandle.truncate(4);
} finally {
await filehandle?.close();
}
If the file previously was shorter than len
bytes, it is extended, and the
extended part is filled with null bytes ('\0'
):
If len
is negative then 0
will be used.
filehandle.utimes(atime, mtime)
#
Change the file system timestamps of the object referenced by the <FileHandle> then resolves the promise with no arguments upon success.
This function does not work on AIX versions before 7.1, it will reject the
promise with an error using code UV_ENOSYS
.
filehandle.write(buffer[, offset[, length[, position]]])
#
buffer
<Buffer> | <TypedArray> | <DataView>offset
<integer> The start position from withinbuffer
where the data to write begins. Default:0
length
<integer> The number of bytes frombuffer
to write. Default:buffer.byteLength - offset
position
<integer> The offset from the beginning of the file where the data frombuffer
should be written. Ifposition
is not anumber
, the data will be written at the current position. See the POSIXpwrite(2)
documentation for more detail.- Returns: <Promise>
Write buffer
to the file.
The promise is resolved with an object containing two properties:
bytesWritten
<integer> the number of bytes writtenbuffer
<Buffer> | <TypedArray> | <DataView> a reference to thebuffer
written.
It is unsafe to use filehandle.write()
multiple times on the same file
without waiting for the promise to be resolved (or rejected). For this
scenario, use fs.createWriteStream()
.
On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
filehandle.write(string[, position[, encoding]])
#
string
<string>position
<integer> The offset from the beginning of the file where the data fromstring
should be written. Ifposition
is not anumber
the data will be written at the current position. See the POSIXpwrite(2)
documentation for more detail.encoding
<string> The expected string encoding. Default:'utf8'
- Returns: <Promise>
Write string
to the file. If string
is not a string, the promise is
rejected with an error.
The promise is resolved with an object containing two properties:
bytesWritten
<integer> the number of bytes writtenbuffer
<string> a reference to thestring
written.
It is unsafe to use filehandle.write()
multiple times on the same file
without waiting for the promise to be resolved (or rejected). For this
scenario, use fs.createWriteStream()
.
On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
filehandle.writeFile(data, options)
#
data
<string> | <Buffer> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable> | <Stream>options
<Object> | <string>- Returns: <Promise>
Asynchronously writes data to a file, replacing the file if it already exists.
data
can be a string, a buffer, an <AsyncIterable> or <Iterable> object.
The promise is resolved with no arguments upon success.
If options
is a string, then it specifies the encoding
.
The <FileHandle> has to support writing.
It is unsafe to use filehandle.writeFile()
multiple times on the same file
without waiting for the promise to be resolved (or rejected).
If one or more filehandle.write()
calls are made on a file handle and then a
filehandle.writeFile()
call is made, the data will be written from the
current position till the end of the file. It doesn't always write from the
beginning of the file.
filehandle.writev(buffers[, position])
#
buffers
<Buffer[]> | <TypedArray[]> | <DataView[]>position
<integer> The offset from the beginning of the file where the data frombuffers
should be written. Ifposition
is not anumber
, the data will be written at the current position.- Returns: <Promise>
Write an array of