The node: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 'node:fs/promises';
To use the callback and sync APIs:
import * as fs from 'node:fs';
All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
The `node:fs` module enables interacting with the file system in a way modeled on standard POSIX functions. To use the promise-based APIs: ```js import * as fs from 'node:fs/promises'; ``` To use the callback and sync APIs: ```js import * as fs from 'node:fs'; ``` All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
(access path)
(access path callback)
(access path mode)
(access path mode callback)
Tests a user's permissions for the file or directory specified by path
.
The mode
argument is an optional integer that specifies the accessibility
checks to be performed. mode
should be either the value fs.constants.F_OK
or a mask consisting of the bitwise OR of any of fs.constants.R_OK
, fs.constants.W_OK
, and fs.constants.X_OK
(e.g.fs.constants.W_OK | fs.constants.R_OK
). Check File access constants
for
possible values of mode
.
The final argument, callback
, is a callback function that is invoked with
a possible error argument. If any of the accessibility checks fail, the error
argument will be an Error
object. The following examples check if package.json
exists, and if it is readable or writable.
import { access, constants } from 'node:fs';
const file = 'package.json';
// Check if the file exists in the current directory.
access(file, constants.F_OK, (err) => {
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
});
// Check if the file is readable.
access(file, constants.R_OK, (err) => {
console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
});
// Check if the file is writable.
access(file, constants.W_OK, (err) => {
console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
});
// Check if the file is readable and writable.
access(file, constants.R_OK | constants.W_OK, (err) => {
console.log(`${file} ${err ? 'is not' : 'is'} readable and writable`);
});
Do not use fs.access()
to check for the accessibility of a file before calling fs.open()
, fs.readFile()
, or fs.writeFile()
. Doing
so introduces a race condition, since other processes may change the file's
state between the two calls. Instead, user code should open/read/write the
file directly and handle the error raised if the file is not accessible.
write (NOT RECOMMENDED)
import { access, open, close } from 'node:fs';
access('myfile', (err) => {
if (!err) {
console.error('myfile already exists');
return;
}
open('myfile', 'wx', (err, fd) => {
if (err) throw err;
try {
writeMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
});
write (RECOMMENDED)
import { open, close } from 'node:fs';
open('myfile', 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
console.error('myfile already exists');
return;
}
throw err;
}
try {
writeMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
read (NOT RECOMMENDED)
import { access, open, close } from 'node:fs';
access('myfile', (err) => {
if (err) {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
}
throw err;
}
open('myfile', 'r', (err, fd) => {
if (err) throw err;
try {
readMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
});
read (RECOMMENDED)
import { open, close } from 'node:fs';
open('myfile', 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
}
throw err;
}
try {
readMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
The "not recommended" examples above check for accessibility and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.
In general, check for the accessibility of a file only if the file will not be used directly, for example when its accessibility is a signal from another process.
On Windows, access-control policies (ACLs) on a directory may limit access to
a file or directory. The fs.access()
function, however, does not check the
ACL and therefore may report that a path is accessible even if the ACL restricts
the user from reading or writing to it.
Asynchronously tests a user's permissions for the file specified by path.
Parameters:
path
: PathLike
- A path to a file or directory. If a URL is provided, it must use the file:
protocol.callback
: NoParamCallback
mode
: number | undefined
Returns: void
Tests a user's permissions for the file or directory specified by `path`. The `mode` argument is an optional integer that specifies the accessibility checks to be performed. `mode` should be either the value `fs.constants.F_OK`or a mask consisting of the bitwise OR of any of `fs.constants.R_OK`, `fs.constants.W_OK`, and `fs.constants.X_OK` (e.g.`fs.constants.W_OK | fs.constants.R_OK`). Check `File access constants` for possible values of `mode`. The final argument, `callback`, is a callback function that is invoked with a possible error argument. If any of the accessibility checks fail, the error argument will be an `Error` object. The following examples check if `package.json` exists, and if it is readable or writable. ```js import { access, constants } from 'node:fs'; const file = 'package.json'; // Check if the file exists in the current directory. access(file, constants.F_OK, (err) => { console.log(`${file} ${err ? 'does not exist' : 'exists'}`); }); // Check if the file is readable. access(file, constants.R_OK, (err) => { console.log(`${file} ${err ? 'is not readable' : 'is readable'}`); }); // Check if the file is writable. access(file, constants.W_OK, (err) => { console.log(`${file} ${err ? 'is not writable' : 'is writable'}`); }); // Check if the file is readable and writable. access(file, constants.R_OK | constants.W_OK, (err) => { console.log(`${file} ${err ? 'is not' : 'is'} readable and writable`); }); ``` Do not use `fs.access()` to check for the accessibility of a file before calling `fs.open()`, `fs.readFile()`, or `fs.writeFile()`. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible. **write (NOT RECOMMENDED)** ```js import { access, open, close } from 'node:fs'; access('myfile', (err) => { if (!err) { console.error('myfile already exists'); return; } open('myfile', 'wx', (err, fd) => { if (err) throw err; try { writeMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); }); ``` **write (RECOMMENDED)** ```js import { open, close } from 'node:fs'; open('myfile', 'wx', (err, fd) => { if (err) { if (err.code === 'EEXIST') { console.error('myfile already exists'); return; } throw err; } try { writeMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); ``` **read (NOT RECOMMENDED)** ```js import { access, open, close } from 'node:fs'; access('myfile', (err) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } open('myfile', 'r', (err, fd) => { if (err) throw err; try { readMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); }); ``` **read (RECOMMENDED)** ```js import { open, close } from 'node:fs'; open('myfile', 'r', (err, fd) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } try { readMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); ``` The "not recommended" examples above check for accessibility and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any. In general, check for the accessibility of a file only if the file will not be used directly, for example when its accessibility is a signal from another process. On Windows, access-control policies (ACLs) on a directory may limit access to a file or directory. The `fs.access()` function, however, does not check the ACL and therefore may report that a path is accessible even if the ACL restricts the user from reading or writing to it. Asynchronously tests a user's permissions for the file specified by path. **Parameters:** - `path`: `PathLike` - A path to a file or directory. If a URL is provided, it must use the `file:` protocol. - `callback`: `NoParamCallback` - `mode`: `number | undefined` **Returns:** `void`
(access-sync path)
(access-sync path mode)
Synchronously tests a user's permissions for the file or directory specified
by path
. The mode
argument is an optional integer that specifies the
accessibility checks to be performed. mode
should be either the value fs.constants.F_OK
or a mask consisting of the bitwise OR of any of fs.constants.R_OK
, fs.constants.W_OK
, and
fs.constants.X_OK
(e.g.fs.constants.W_OK | fs.constants.R_OK
). Check File access constants
for
possible values of mode
.
If any of the accessibility checks fail, an Error
will be thrown. Otherwise,
the method will return undefined
.
import { accessSync, constants } from 'node:fs';
try {
accessSync('etc/passwd', constants.R_OK | constants.W_OK);
console.log('can read/write');
} catch (err) {
console.error('no access!');
}
Parameters:
path
: PathLike
mode
: number | undefined
Returns: void
Synchronously tests a user's permissions for the file or directory specified by `path`. The `mode` argument is an optional integer that specifies the accessibility checks to be performed. `mode` should be either the value `fs.constants.F_OK` or a mask consisting of the bitwise OR of any of `fs.constants.R_OK`, `fs.constants.W_OK`, and `fs.constants.X_OK` (e.g.`fs.constants.W_OK | fs.constants.R_OK`). Check `File access constants` for possible values of `mode`. If any of the accessibility checks fail, an `Error` will be thrown. Otherwise, the method will return `undefined`. ```js import { accessSync, constants } from 'node:fs'; try { accessSync('etc/passwd', constants.R_OK | constants.W_OK); console.log('can read/write'); } catch (err) { console.error('no access!'); } ``` **Parameters:** - `path`: `PathLike` - `mode`: `number | undefined` **Returns:** `void`
(append-file file data callback)
(append-file path data options callback)
Asynchronously append data to a file, creating the file if it does not yet
exist. data
can be a string or a Buffer
.
The mode
option only affects the newly created file. See {@link open } for more details.
import { appendFile } from 'node:fs';
appendFile('message.txt', 'data to append', (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
If options
is a string, then it specifies the encoding:
import { appendFile } from 'node:fs';
appendFile('message.txt', 'data to append', 'utf8', callback);
The path
may be specified as a numeric file descriptor that has been opened
for appending (using fs.open()
or fs.openSync()
). The file descriptor will
not be closed automatically.
import { open, close, appendFile } from 'node:fs';
function closeFd(fd) {
close(fd, (err) => {
if (err) throw err;
});
}
open('message.txt', 'a', (err, fd) => {
if (err) throw err;
try {
appendFile(fd, 'data to append', 'utf8', (err) => {
closeFd(fd);
if (err) throw err;
});
} catch (err) {
closeFd(fd);
throw err;
}
});
Asynchronously append data to a file, creating the file if it does not exist.
Parameters:
file
: PathOrFileDescriptor
- 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.path
: PathOrFileDescriptor
- filename or file descriptordata
: string | Uint8Array
- The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.callback
: NoParamCallback
options
: WriteFileOptions
Returns: void
Asynchronously append data to a file, creating the file if it does not yet exist. `data` can be a string or a `Buffer`. The `mode` option only affects the newly created file. See {@link open } for more details. ```js import { appendFile } from 'node:fs'; appendFile('message.txt', 'data to append', (err) => { if (err) throw err; console.log('The "data to append" was appended to file!'); }); ``` If `options` is a string, then it specifies the encoding: ```js import { appendFile } from 'node:fs'; appendFile('message.txt', 'data to append', 'utf8', callback); ``` The `path` may be specified as a numeric file descriptor that has been opened for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will not be closed automatically. ```js import { open, close, appendFile } from 'node:fs'; function closeFd(fd) { close(fd, (err) => { if (err) throw err; }); } open('message.txt', 'a', (err, fd) => { if (err) throw err; try { appendFile(fd, 'data to append', 'utf8', (err) => { closeFd(fd); if (err) throw err; }); } catch (err) { closeFd(fd); throw err; } }); ``` Asynchronously append data to a file, creating the file if it does not exist. **Parameters:** - `file`: `PathOrFileDescriptor` - 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. - `path`: `PathOrFileDescriptor` - filename or file descriptor - `data`: `string | Uint8Array` - The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. - `callback`: `NoParamCallback` - `options`: `WriteFileOptions` **Returns:** `void`
(append-file-sync path data)
(append-file-sync path data options)
Synchronously append data to a file, creating the file if it does not yet
exist. data
can be a string or a Buffer
.
The mode
option only affects the newly created file. See {@link open } for more details.
import { appendFileSync } from 'node:fs';
try {
appendFileSync('message.txt', 'data to append');
console.log('The "data to append" was appended to file!');
} catch (err) {
// Handle the error
}
If options
is a string, then it specifies the encoding:
import { appendFileSync } from 'node:fs';
appendFileSync('message.txt', 'data to append', 'utf8');
The path
may be specified as a numeric file descriptor that has been opened
for appending (using fs.open()
or fs.openSync()
). The file descriptor will
not be closed automatically.
import { openSync, closeSync, appendFileSync } from 'node:fs';
let fd;
try {
fd = openSync('message.txt', 'a');
appendFileSync(fd, 'data to append', 'utf8');
} catch (err) {
// Handle the error
} finally {
if (fd !== undefined)
closeSync(fd);
}
Parameters:
path
: PathOrFileDescriptor
- filename or file descriptordata
: string | Uint8Array
options
: WriteFileOptions | undefined
Returns: void
Synchronously append data to a file, creating the file if it does not yet exist. `data` can be a string or a `Buffer`. The `mode` option only affects the newly created file. See {@link open } for more details. ```js import { appendFileSync } from 'node:fs'; try { appendFileSync('message.txt', 'data to append'); console.log('The "data to append" was appended to file!'); } catch (err) { // Handle the error } ``` If `options` is a string, then it specifies the encoding: ```js import { appendFileSync } from 'node:fs'; appendFileSync('message.txt', 'data to append', 'utf8'); ``` The `path` may be specified as a numeric file descriptor that has been opened for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will not be closed automatically. ```js import { openSync, closeSync, appendFileSync } from 'node:fs'; let fd; try { fd = openSync('message.txt', 'a'); appendFileSync(fd, 'data to append', 'utf8'); } catch (err) { // Handle the error } finally { if (fd !== undefined) closeSync(fd); } ``` **Parameters:** - `path`: `PathOrFileDescriptor` - filename or file descriptor - `data`: `string | Uint8Array` - `options`: `WriteFileOptions | undefined` **Returns:** `void`
(chmod path mode callback)
Asynchronously changes the permissions of a file. No arguments other than a possible exception are given to the completion callback.
See the POSIX chmod(2)
documentation for more detail.
import { chmod } from 'node:fs';
chmod('my_file.txt', 0o775, (err) => {
if (err) throw err;
console.log('The permissions for file "my_file.txt" have been changed!');
});
Parameters:
path
: PathLike
mode
: Mode
callback
: NoParamCallback
Returns: void
Asynchronously changes the permissions of a file. No arguments other than a possible exception are given to the completion callback. See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail. ```js import { chmod } from 'node:fs'; chmod('my_file.txt', 0o775, (err) => { if (err) throw err; console.log('The permissions for file "my_file.txt" have been changed!'); }); ``` **Parameters:** - `path`: `PathLike` - `mode`: `Mode` - `callback`: `NoParamCallback` **Returns:** `void`
(chmod-sync path mode)
For detailed information, see the documentation of the asynchronous version of this API: {@link chmod }.
See the POSIX chmod(2)
documentation for more detail.
Parameters:
path
: PathLike
mode
: Mode
Returns: void
For detailed information, see the documentation of the asynchronous version of this API: {@link chmod }. See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail. **Parameters:** - `path`: `PathLike` - `mode`: `Mode` **Returns:** `void`
(chown path uid gid callback)
Asynchronously changes owner and group of a file. No arguments other than a possible exception are given to the completion callback.
See the POSIX chown(2)
documentation for more detail.
Parameters:
path
: PathLike
uid
: number
gid
: number
callback
: NoParamCallback
Returns: void
Asynchronously changes owner and group of a file. No arguments other than a possible exception are given to the completion callback. See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail. **Parameters:** - `path`: `PathLike` - `uid`: `number` - `gid`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(chown-sync path uid gid)
Synchronously changes owner and group of a file. Returns undefined
.
This is the synchronous version of {@link chown }.
See the POSIX chown(2)
documentation for more detail.
Parameters:
path
: PathLike
uid
: number
gid
: number
Returns: void
Synchronously changes owner and group of a file. Returns `undefined`. This is the synchronous version of {@link chown }. See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail. **Parameters:** - `path`: `PathLike` - `uid`: `number` - `gid`: `number` **Returns:** `void`
(close fd)
(close fd callback)
Closes the file descriptor. No arguments other than a possible exception are given to the completion callback.
Calling fs.close()
on any file descriptor (fd
) that is currently in use
through any other fs
operation may lead to undefined behavior.
See the POSIX close(2)
documentation for more detail.
Parameters:
fd
: number
callback
: NoParamCallback | undefined
Returns: void
Closes the file descriptor. No arguments other than a possible exception are given to the completion callback. Calling `fs.close()` on any file descriptor (`fd`) that is currently in use through any other `fs` operation may lead to undefined behavior. See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `callback`: `NoParamCallback | undefined` **Returns:** `void`
(close-sync fd)
Closes the file descriptor. Returns undefined
.
Calling fs.closeSync()
on any file descriptor (fd
) that is currently in use
through any other fs
operation may lead to undefined behavior.
See the POSIX close(2)
documentation for more detail.
Parameters:
fd
: number
Returns: void
Closes the file descriptor. Returns `undefined`. Calling `fs.closeSync()` on any file descriptor (`fd`) that is currently in use through any other `fs` operation may lead to undefined behavior. See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail. **Parameters:** - `fd`: `number` **Returns:** `void`
(copy-file src dest callback)
(copy-file src dest mode callback)
Asynchronously copies src
to dest
. By default, dest
is overwritten if it
already exists. No arguments other than a possible exception are given to the
callback function. Node.js makes no guarantees about the atomicity of the copy
operation. If an error occurs after the destination file has been opened for
writing, Node.js will attempt to remove the destination.
mode
is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE
).
fs.constants.COPYFILE_EXCL
: The copy operation will fail if dest
already
exists.fs.constants.COPYFILE_FICLONE
: The copy operation will attempt to create a
copy-on-write reflink. If the platform does not support copy-on-write, then a
fallback copy mechanism is used.fs.constants.COPYFILE_FICLONE_FORCE
: The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support
copy-on-write, then the operation will fail.import { copyFile, constants } from 'node:fs';
function callback(err) {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
}
// destination.txt will be created or overwritten by default.
copyFile('source.txt', 'destination.txt', callback);
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback);
Parameters:
src
: PathLike
dest
: PathLike
mode
: number
callback
: NoParamCallback
Returns: void
Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it already exists. No arguments other than a possible exception are given to the callback function. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination. `mode` is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`). * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already exists. * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used. * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail. ```js import { copyFile, constants } from 'node:fs'; function callback(err) { if (err) throw err; console.log('source.txt was copied to destination.txt'); } // destination.txt will be created or overwritten by default. copyFile('source.txt', 'destination.txt', callback); // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback); ``` **Parameters:** - `src`: `PathLike` - `dest`: `PathLike` - `mode`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(copy-file-sync src dest)
(copy-file-sync src dest mode)
Synchronously copies src
to dest
. By default, dest
is overwritten if it
already exists. Returns undefined
. Node.js makes no guarantees about the
atomicity of the copy operation. If an error occurs after the destination file
has been opened for writing, Node.js will attempt to remove the destination.
mode
is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE
).
fs.constants.COPYFILE_EXCL
: The copy operation will fail if dest
already
exists.fs.constants.COPYFILE_FICLONE
: The copy operation will attempt to create a
copy-on-write reflink. If the platform does not support copy-on-write, then a
fallback copy mechanism is used.fs.constants.COPYFILE_FICLONE_FORCE
: The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support
copy-on-write, then the operation will fail.import { copyFileSync, constants } from 'node:fs';
// destination.txt will be created or overwritten by default.
copyFileSync('source.txt', 'destination.txt');
console.log('source.txt was copied to destination.txt');
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
copyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL);
Parameters:
src
: PathLike
- source filename to copydest
: PathLike
- destination filename of the copy operationmode
: number | undefined
- modifiers for copy operation.Returns: void
Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it already exists. Returns `undefined`. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination. `mode` is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`). * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already exists. * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used. * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail. ```js import { copyFileSync, constants } from 'node:fs'; // destination.txt will be created or overwritten by default. copyFileSync('source.txt', 'destination.txt'); console.log('source.txt was copied to destination.txt'); // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. copyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL); ``` **Parameters:** - `src`: `PathLike` - source filename to copy - `dest`: `PathLike` - destination filename of the copy operation - `mode`: `number | undefined` - modifiers for copy operation. **Returns:** `void`
(cp source destination callback)
(cp source destination opts callback)
Asynchronously copies the entire directory structure from src
to dest
,
including subdirectories and files.
When copying a directory to another directory, globs are not supported and
behavior is similar to cp dir1/ dir2/
.
Parameters:
source
: string | URL
destination
: string | URL
opts
: CopyOptions
callback
: (err: ErrnoException | null) => void
Returns: void
Asynchronously copies the entire directory structure from `src` to `dest`, including subdirectories and files. When copying a directory to another directory, globs are not supported and behavior is similar to `cp dir1/ dir2/`. **Parameters:** - `source`: `string | URL` - `destination`: `string | URL` - `opts`: `CopyOptions` - `callback`: `(err: ErrnoException | null) => void` **Returns:** `void`
(cp-sync source destination)
(cp-sync source destination opts)
Synchronously copies the entire directory structure from src
to dest
,
including subdirectories and files.
When copying a directory to another directory, globs are not supported and
behavior is similar to cp dir1/ dir2/
.
Parameters:
source
: string | URL
destination
: string | URL
opts
: CopySyncOptions | undefined
Returns: void
Synchronously copies the entire directory structure from `src` to `dest`, including subdirectories and files. When copying a directory to another directory, globs are not supported and behavior is similar to `cp dir1/ dir2/`. **Parameters:** - `source`: `string | URL` - `destination`: `string | URL` - `opts`: `CopySyncOptions | undefined` **Returns:** `void`
(create-read-stream path)
(create-read-stream path options)
Unlike the 16 KiB default highWaterMark
for a stream.Readable
, the stream
returned by this method has a default highWaterMark
of 64 KiB.
options
can include start
and end
values to read a range of bytes from
the file instead of the entire file. Both start
and end
are inclusive and
start counting at 0, allowed values are in the
[0, Number.MAX_SAFE_INTEGER
] range. If fd
is specified and start
is
omitted or undefined
, fs.createReadStream()
reads sequentially from the
current file position. The encoding
can be any one of those accepted by Buffer
.
If fd
is specified, ReadStream
will ignore the path
argument and will use
the specified file descriptor. This means that no 'open'
event will be
emitted. fd
should be blocking; non-blocking fd
s should be passed to net.Socket
.
If fd
points to a character device that only supports blocking reads
(such as keyboard or sound card), read operations do not finish until data is
available. This can prevent the process from exiting and the stream from
closing naturally.
By default, the stream will emit a 'close'
event after it has been
destroyed. Set the emitClose
option to false
to change this behavior.
By providing the fs
option, it is possible to override the corresponding fs
implementations for open
, read
, and close
. When providing the fs
option,
an override for read
is required. If no fd
is provided, an override for open
is also required. If autoClose
is true
, an override for close
is
also required.
import { createReadStream } from 'node:fs';
// Create a stream from some character device.
const stream = createReadStream('/dev/input/event0');
setTimeout(() => {
stream.close(); // This may not close the stream.
// Artificially marking end-of-stream, as if the underlying resource had
// indicated end-of-file by itself, allows the stream to close.
// This does not cancel pending read operations, and if there is such an
// operation, the process may still not be able to exit successfully
// until it finishes.
stream.push(null);
stream.read(0);
}, 100);
If autoClose
is false, then the file descriptor won't be closed, even if
there's an error. It is the application's responsibility to close it and make
sure there's no file descriptor leak. If autoClose
is set to true (default
behavior), on 'error'
or 'end'
the file descriptor will be closed
automatically.
mode
sets the file mode (permission and sticky bits), but only if the
file was created.
An example to read the last 10 bytes of a file which is 100 bytes long:
import { createReadStream } from 'node:fs';
createReadStream('sample.txt', { start: 90, end: 99 });
If options
is a string, then it specifies the encoding.
Parameters:
path
: PathLike
options
: BufferEncoding | ReadStreamOptions | undefined
Returns: ReadStream
Unlike the 16 KiB default `highWaterMark` for a `stream.Readable`, the stream returned by this method has a default `highWaterMark` of 64 KiB. `options` can include `start` and `end` values to read a range of bytes from the file instead of the entire file. Both `start` and `end` are inclusive and start counting at 0, allowed values are in the \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `fd` is specified and `start` is omitted or `undefined`, `fs.createReadStream()` reads sequentially from the current file position. The `encoding` can be any one of those accepted by `Buffer`. If `fd` is specified, `ReadStream` will ignore the `path` argument and will use the specified file descriptor. This means that no `'open'` event will be emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`. If `fd` points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally. By default, the stream will emit a `'close'` event after it has been destroyed. Set the `emitClose` option to `false` to change this behavior. By providing the `fs` option, it is possible to override the corresponding `fs` implementations for `open`, `read`, and `close`. When providing the `fs` option, an override for `read` is required. If no `fd` is provided, an override for `open` is also required. If `autoClose` is `true`, an override for `close` is also required. ```js import { createReadStream } from 'node:fs'; // Create a stream from some character device. const stream = createReadStream('/dev/input/event0'); setTimeout(() => { stream.close(); // This may not close the stream. // Artificially marking end-of-stream, as if the underlying resource had // indicated end-of-file by itself, allows the stream to close. // This does not cancel pending read operations, and if there is such an // operation, the process may still not be able to exit successfully // until it finishes. stream.push(null); stream.read(0); }, 100); ``` If `autoClose` is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. If `autoClose` is set to true (default behavior), on `'error'` or `'end'` the file descriptor will be closed automatically. `mode` sets the file mode (permission and sticky bits), but only if the file was created. An example to read the last 10 bytes of a file which is 100 bytes long: ```js import { createReadStream } from 'node:fs'; createReadStream('sample.txt', { start: 90, end: 99 }); ``` If `options` is a string, then it specifies the encoding. **Parameters:** - `path`: `PathLike` - `options`: `BufferEncoding | ReadStreamOptions | undefined` **Returns:** `ReadStream`
(create-write-stream path)
(create-write-stream path options)
options
may also include a start
option to allow writing data at some
position past the beginning of the file, allowed values are in the
[0, Number.MAX_SAFE_INTEGER
] range. Modifying a file rather than
replacing it may require the flags
option to be set to r+
rather than the
default w
. The encoding
can be any one of those accepted by Buffer
.
If autoClose
is set to true (default behavior) on 'error'
or 'finish'
the file descriptor will be closed automatically. If autoClose
is false,
then the file descriptor won't be closed, even if there's an error.
It is the application's responsibility to close it and make sure there's no
file descriptor leak.
By default, the stream will emit a 'close'
event after it has been
destroyed. Set the emitClose
option to false
to change this behavior.
By providing the fs
option it is possible to override the corresponding fs
implementations for open
, write
, writev
, and close
. Overriding write()
without writev()
can reduce
performance as some optimizations (_writev()
)
will be disabled. When providing the fs
option, overrides for at least one of write
and writev
are required. If no fd
option is supplied, an override
for open
is also required. If autoClose
is true
, an override for close
is also required.
Like fs.ReadStream
, if fd
is specified, fs.WriteStream
will ignore the path
argument and will use the specified file descriptor. This means that no 'open'
event will be
emitted. fd
should be blocking; non-blocking fd
s
should be passed to net.Socket
.
If options
is a string, then it specifies the encoding.
Parameters:
path
: PathLike
options
: BufferEncoding | WriteStreamOptions | undefined
Returns: WriteStream
`options` may also include a `start` option to allow writing data at some position past the beginning of the file, allowed values are in the \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than replacing it may require the `flags` option to be set to `r+` rather than the default `w`. The `encoding` can be any one of those accepted by `Buffer`. If `autoClose` is set to true (default behavior) on `'error'` or `'finish'` the file descriptor will be closed automatically. If `autoClose` is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. By default, the stream will emit a `'close'` event after it has been destroyed. Set the `emitClose` option to `false` to change this behavior. By providing the `fs` option it is possible to override the corresponding `fs` implementations for `open`, `write`, `writev`, and `close`. Overriding `write()` without `writev()` can reduce performance as some optimizations (`_writev()`) will be disabled. When providing the `fs` option, overrides for at least one of `write` and `writev` are required. If no `fd` option is supplied, an override for `open` is also required. If `autoClose` is `true`, an override for `close` is also required. Like `fs.ReadStream`, if `fd` is specified, `fs.WriteStream` will ignore the `path` argument and will use the specified file descriptor. This means that no `'open'` event will be emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`. If `options` is a string, then it specifies the encoding. **Parameters:** - `path`: `PathLike` - `options`: `BufferEncoding | WriteStreamOptions | undefined` **Returns:** `WriteStream`
(exists path callback)
Test whether or not the given path exists by checking with the file system.
Then call the callback
argument with either true or false:
import { exists } from 'node:fs';
exists('/etc/passwd', (e) => {
console.log(e ? 'it exists' : 'no passwd!');
});
The parameters for this callback are not consistent with other Node.js
callbacks. Normally, the first parameter to a Node.js callback is an err
parameter, optionally followed by other parameters. The fs.exists()
callback
has only one boolean parameter. This is one reason fs.access()
is recommended
instead of fs.exists()
.
Using fs.exists()
to check for the existence of a file before calling fs.open()
, fs.readFile()
, or fs.writeFile()
is not recommended. Doing
so introduces a race condition, since other processes may change the file's
state between the two calls. Instead, user code should open/read/write the
file directly and handle the error raised if the file does not exist.
write (NOT RECOMMENDED)
import { exists, open, close } from 'node:fs';
exists('myfile', (e) => {
if (e) {
console.error('myfile already exists');
} else {
open('myfile', 'wx', (err, fd) => {
if (err) throw err;
try {
writeMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
}
});
write (RECOMMENDED)
import { open, close } from 'node:fs';
open('myfile', 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
console.error('myfile already exists');
return;
}
throw err;
}
try {
writeMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
read (NOT RECOMMENDED)
import { open, close, exists } from 'node:fs';
exists('myfile', (e) => {
if (e) {
open('myfile', 'r', (err, fd) => {
if (err) throw err;
try {
readMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
} else {
console.error('myfile does not exist');
}
});
read (RECOMMENDED)
import { open, close } from 'node:fs';
open('myfile', 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
}
throw err;
}
try {
readMyData(fd);
} finally {
close(fd, (err) => {
if (err) throw err;
});
}
});
The "not recommended" examples above check for existence and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.
In general, check for the existence of a file only if the file won't be used directly, for example when its existence is a signal from another process.
Parameters:
path
: PathLike
callback
: (exists: boolean) => void
Returns: void
Test whether or not the given path exists by checking with the file system. Then call the `callback` argument with either true or false: ```js import { exists } from 'node:fs'; exists('/etc/passwd', (e) => { console.log(e ? 'it exists' : 'no passwd!'); }); ``` **The parameters for this callback are not consistent with other Node.js** **callbacks.** Normally, the first parameter to a Node.js callback is an `err` parameter, optionally followed by other parameters. The `fs.exists()` callback has only one boolean parameter. This is one reason `fs.access()` is recommended instead of `fs.exists()`. Using `fs.exists()` to check for the existence of a file before calling `fs.open()`, `fs.readFile()`, or `fs.writeFile()` is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist. **write (NOT RECOMMENDED)** ```js import { exists, open, close } from 'node:fs'; exists('myfile', (e) => { if (e) { console.error('myfile already exists'); } else { open('myfile', 'wx', (err, fd) => { if (err) throw err; try { writeMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); } }); ``` **write (RECOMMENDED)** ```js import { open, close } from 'node:fs'; open('myfile', 'wx', (err, fd) => { if (err) { if (err.code === 'EEXIST') { console.error('myfile already exists'); return; } throw err; } try { writeMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); ``` **read (NOT RECOMMENDED)** ```js import { open, close, exists } from 'node:fs'; exists('myfile', (e) => { if (e) { open('myfile', 'r', (err, fd) => { if (err) throw err; try { readMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); } else { console.error('myfile does not exist'); } }); ``` **read (RECOMMENDED)** ```js import { open, close } from 'node:fs'; open('myfile', 'r', (err, fd) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } try { readMyData(fd); } finally { close(fd, (err) => { if (err) throw err; }); } }); ``` The "not recommended" examples above check for existence and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any. In general, check for the existence of a file only if the file won't be used directly, for example when its existence is a signal from another process. **Parameters:** - `path`: `PathLike` - `callback`: `(exists: boolean) => void` **Returns:** `void`
(exists-sync? path)
Returns true
if the path exists, false
otherwise.
For detailed information, see the documentation of the asynchronous version of this API: {@link exists }.
fs.exists()
is deprecated, but fs.existsSync()
is not. The callback
parameter to fs.exists()
accepts parameters that are inconsistent with other
Node.js callbacks. fs.existsSync()
does not use a callback.
import { existsSync } from 'node:fs';
if (existsSync('/etc/passwd'))
console.log('The path exists.');
Parameters:
path
: PathLike
Returns: boolean
Returns `true` if the path exists, `false` otherwise. For detailed information, see the documentation of the asynchronous version of this API: {@link exists }. `fs.exists()` is deprecated, but `fs.existsSync()` is not. The `callback` parameter to `fs.exists()` accepts parameters that are inconsistent with other Node.js callbacks. `fs.existsSync()` does not use a callback. ```js import { existsSync } from 'node:fs'; if (existsSync('/etc/passwd')) console.log('The path exists.'); ``` **Parameters:** - `path`: `PathLike` **Returns:** `boolean`
(fchmod fd mode callback)
Sets the permissions on the file. No arguments other than a possible exception are given to the completion callback.
See the POSIX fchmod(2)
documentation for more detail.
Parameters:
fd
: number
mode
: Mode
callback
: NoParamCallback
Returns: void
Sets the permissions on the file. No arguments other than a possible exception are given to the completion callback. See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `mode`: `Mode` - `callback`: `NoParamCallback` **Returns:** `void`
(fchmod-sync fd mode)
Sets the permissions on the file. Returns undefined
.
See the POSIX fchmod(2)
documentation for more detail.
Parameters:
fd
: number
mode
: Mode
Returns: void
Sets the permissions on the file. Returns `undefined`. See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `mode`: `Mode` **Returns:** `void`
(fchown fd uid gid callback)
Sets the owner of the file. No arguments other than a possible exception are given to the completion callback.
See the POSIX fchown(2)
documentation for more detail.
Parameters:
fd
: number
uid
: number
gid
: number
callback
: NoParamCallback
Returns: void
Sets the owner of the file. No arguments other than a possible exception are given to the completion callback. See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `uid`: `number` - `gid`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(fchown-sync fd uid gid)
Sets the owner of the file. Returns undefined
.
See the POSIX fchown(2)
documentation for more detail.
Parameters:
fd
: number
uid
: number
- The file's new owner's user id.gid
: number
- The file's new group's group id.Returns: void
Sets the owner of the file. Returns `undefined`. See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `uid`: `number` - The file's new owner's user id. - `gid`: `number` - The file's new group's group id. **Returns:** `void`
(fdatasync fd callback)
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. No arguments other
than a possible
exception are given to the completion callback.
Parameters:
fd
: number
callback
: NoParamCallback
Returns: void
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)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. No arguments other than a possible exception are given to the completion callback. **Parameters:** - `fd`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(fdatasync-sync fd)
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. Returns undefined
.
Parameters:
fd
: number
Returns: void
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)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. Returns `undefined`. **Parameters:** - `fd`: `number` **Returns:** `void`
(fstat fd)
(fstat fd callback)
(fstat fd options)
(fstat fd options callback)
Invokes the callback with the fs.Stats
for the file descriptor.
See the POSIX fstat(2)
documentation for more detail.
Parameters:
fd
: number
options
: StatOptions | undefined
callback
: (err: ErrnoException | null, stats: Stats | BigIntStats) => void
Returns: void
Invokes the callback with the `fs.Stats` for the file descriptor. See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `options`: `StatOptions | undefined` - `callback`: `(err: ErrnoException | null, stats: Stats | BigIntStats) => void` **Returns:** `void`
(fstat-sync fd)
(fstat-sync fd options)
Retrieves the fs.Stats
for the file descriptor.
See the POSIX fstat(2)
documentation for more detail.
Parameters:
fd
: number
options
: StatOptions | undefined
Returns: Stats | BigIntStats
Retrieves the `fs.Stats` for the file descriptor. See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail. **Parameters:** - `fd`: `number` - `options`: `StatOptions | undefined` **Returns:** `Stats | BigIntStats`
(fsync fd callback)
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. No arguments other
than a possible exception are given to the completion callback.
Parameters:
fd
: number
callback
: NoParamCallback
Returns: void
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)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. No arguments other than a possible exception are given to the completion callback. **Parameters:** - `fd`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(fsync-sync fd)
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. Returns undefined
.
Parameters:
fd
: number
Returns: void
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)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. Returns `undefined`. **Parameters:** - `fd`: `number` **Returns:** `void`
(ftruncate fd)
(ftruncate fd callback)
(ftruncate fd len)
(ftruncate fd len callback)
Truncates the file descriptor. No arguments other than a possible exception are given to the completion callback.
See the POSIX ftruncate(2)
documentation for more detail.
If the file referred to by the file descriptor was larger than len
bytes, only
the first len
bytes will be retained in the file.
For example, the following program retains only the first four bytes of the file:
import { open, close, ftruncate } from 'node:fs';
function closeFd(fd) {
close(fd, (err) => {
if (err) throw err;
});
}
open('temp.txt', 'r+', (err, fd) => {
if (err) throw err;
try {
ftruncate(fd, 4, (err) => {
closeFd(fd);
if (err) throw err;
});
} catch (err) {
closeFd(fd);
if (err) throw err;
}
});
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.
Asynchronous ftruncate(2) - Truncate a file to a specified length.
Parameters:
fd
: number
- A file descriptor.callback
: NoParamCallback
len
: number | null | undefined
Returns: void
Truncates the file descriptor. No arguments other than a possible exception are given to the completion callback. See the POSIX [`ftruncate(2)`](http://man7.org/linux/man-pages/man2/ftruncate.2.html) documentation for more detail. If the file referred to by the file descriptor was larger than `len` bytes, only the first `len` bytes will be retained in the file. For example, the following program retains only the first four bytes of the file: ```js import { open, close, ftruncate } from 'node:fs'; function closeFd(fd) { close(fd, (err) => { if (err) throw err; }); } open('temp.txt', 'r+', (err, fd) => { if (err) throw err; try { ftruncate(fd, 4, (err) => { closeFd(fd); if (err) throw err; }); } catch (err) { closeFd(fd); if (err) throw err; } }); ``` 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. Asynchronous ftruncate(2) - Truncate a file to a specified length. **Parameters:** - `fd`: `number` - A file descriptor. - `callback`: `NoParamCallback` - `len`: `number | null | undefined` **Returns:** `void`
(ftruncate-sync fd)
(ftruncate-sync fd len)
Truncates the file descriptor. Returns undefined
.
For detailed information, see the documentation of the asynchronous version of this API: {@link ftruncate }.
Parameters:
fd
: number
len
: number | null | undefined
Returns: void
Truncates the file descriptor. Returns `undefined`. For detailed information, see the documentation of the asynchronous version of this API: {@link ftruncate }. **Parameters:** - `fd`: `number` - `len`: `number | null | undefined` **Returns:** `void`
(futimes fd atime mtime callback)
Change the file system timestamps of the object referenced by the supplied file descriptor. See {@link utimes }.
Parameters:
fd
: number
atime
: TimeLike
mtime
: TimeLike
callback
: NoParamCallback
Returns: void
Change the file system timestamps of the object referenced by the supplied file descriptor. See {@link utimes }. **Parameters:** - `fd`: `number` - `atime`: `TimeLike` - `mtime`: `TimeLike` - `callback`: `NoParamCallback` **Returns:** `void`
(futimes-sync fd atime mtime)
Synchronous version of {@link futimes }. Returns undefined
.
Parameters:
fd
: number
atime
: TimeLike
mtime
: TimeLike
Returns: void
Synchronous version of {@link futimes }. Returns `undefined`. **Parameters:** - `fd`: `number` - `atime`: `TimeLike` - `mtime`: `TimeLike` **Returns:** `void`
(lchmod path mode callback)
Changes the permissions on a symbolic link. No arguments other than a possible exception are given to the completion callback.
This method is only implemented on macOS.
See the POSIX lchmod(2)
documentation for more detail.
Parameters:
path
: PathLike
mode
: Mode
callback
: NoParamCallback
Returns: void
Changes the permissions on a symbolic link. No arguments other than a possible exception are given to the completion callback. This method is only implemented on macOS. See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail. **Parameters:** - `path`: `PathLike` - `mode`: `Mode` - `callback`: `NoParamCallback` **Returns:** `void`
(lchmod-sync path mode)
Changes the permissions on a symbolic link. Returns undefined
.
This method is only implemented on macOS.
See the POSIX lchmod(2)
documentation for more detail.
Parameters:
path
: PathLike
mode
: Mode
Returns: void
Changes the permissions on a symbolic link. Returns `undefined`. This method is only implemented on macOS. See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail. **Parameters:** - `path`: `PathLike` - `mode`: `Mode` **Returns:** `void`
(lchown path uid gid callback)
Set the owner of the symbolic link. No arguments other than a possible exception are given to the completion callback.
See the POSIX lchown(2)
documentation for more detail.
Parameters:
path
: PathLike
uid
: number
gid
: number
callback
: NoParamCallback
Returns: void
Set the owner of the symbolic link. No arguments other than a possible exception are given to the completion callback. See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more detail. **Parameters:** - `path`: `PathLike` - `uid`: `number` - `gid`: `number` - `callback`: `NoParamCallback` **Returns:** `void`
(lchown-sync path uid gid)
Set the owner for the path. Returns undefined
.
See the POSIX lchown(2)
documentation for more details.
Parameters:
path
: PathLike
uid
: number
- The file's new owner's user id.gid
: number
- The file's new group's group id.Returns: void
Set the owner for the path. Returns `undefined`. See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more details. **Parameters:** - `path`: `PathLike` - `uid`: `number` - The file's new owner's user id. - `gid`: `number` - The file's new group's group id. **Returns:** `void`
(link existing-path new-path callback)
Creates a new link from the existingPath
to the newPath
. See the POSIX link(2)
documentation for more detail. No arguments other than
a possible
exception are given to the completion callback.
Parameters:
existing-path
: PathLike
new-path
: PathLike
callback
: NoParamCallback
Returns: void
Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. No arguments other than a possible exception are given to the completion callback. **Parameters:** - `existing-path`: `PathLike` - `new-path`: `PathLike` - `callback`: `NoParamCallback` **Returns:** `void`
(link-sync existing-path new-path)
Creates a new link from the existingPath
to the newPath
. See the POSIX link(2)
documentation for more detail. Returns undefined
.
Parameters:
existing-path
: PathLike
new-path
: PathLike
Returns: void
Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. Returns `undefined`. **Parameters:** - `existing-path`: `PathLike` - `new-path`: `PathLike` **Returns:** `void`
(lstat path)
(lstat path callback)
(lstat path options)
(lstat path options callback)
Retrieves the fs.Stats
for the symbolic link referred to by the path.
The callback gets two arguments (err, stats)
where stats
is a fs.Stats
object. lstat()
is identical to stat()
, except that if path
is a symbolic
link, then the link itself is stat-ed, not the file that it refers to.
See the POSIX lstat(2)
documentation for more details.
Parameters:
path
: PathLike
options
: StatOptions | undefined
callback
: (err: ErrnoException | null, stats: Stats | BigIntStats) => void
Returns: void
Retrieves the `fs.Stats` for the symbolic link referred to by the path. The callback gets two arguments `(err, stats)` where `stats` is a `fs.Stats` object. `lstat()` is identical to `stat()`, except that if `path` is a symbolic link, then the link itself is stat-ed, not the file that it refers to. See the POSIX [`lstat(2)`](http://man7.org/linux/man-pages/man2/lstat.2.html) documentation for more details. **Parameters:** - `path`: `PathLike` - `options`: `StatOptions | undefined` - `callback`: `(err: ErrnoException | null, stats: Stats | BigIntStats) => void` **Returns:** `void`
(lstat-sync)
(lstat-sync path)
(lstat-sync path options)
Synchronous lstat(2) - Get file status. Does not dereference symbolic links.
Parameters:
path
: PathLike
options
: StatSyncOptions | undefined
Returns: Stats | BigIntStats | undefined
Synchronous lstat(2) - Get file status. Does not dereference symbolic links. **Parameters:** - `path`: `PathLike` - `options`: `StatSyncOptions | undefined` **Returns:** `Stats | BigIntStats | undefined`
(lutimes path atime mtime callback)
Changes the access and modification times of a file in the same way as {@link utimes }, with the difference that if the path refers to a symbolic link, then the link is not dereferenced: instead, the timestamps of the symbolic link itself are changed.
No arguments other than a possible exception are given to the completion callback.
Parameters:
path
: PathLike
atime
: TimeLike
mtime
: TimeLike
callback
: NoParamCallback
Returns: void
Changes the access and modification times of a file in the same way as {@link utimes }, with the difference that if the path refers to a symbolic link, then the link is not dereferenced: instead, the timestamps of the symbolic link itself are changed. No arguments other than a possible exception are given to the completion callback. **Parameters:** - `path`: `PathLike` - `atime`: `TimeLike` - `mtime`: `TimeLike` - `callback`: `NoParamCallback` **Returns:** `void`
(lutimes-sync path atime mtime)
Change the file system timestamps of the symbolic link referenced by path
.
Returns undefined
, or throws an exception when parameters are incorrect or
the operation fails. This is the synchronous version of {@link lutimes }.
Parameters:
path
: PathLike
atime
: TimeLike
mtime
: TimeLike
Returns: void
Change the file system timestamps of the symbolic link referenced by `path`. Returns `undefined`, or throws an exception when parameters are incorrect or the operation fails. This is the synchronous version of {@link lutimes }. **Parameters:** - `path`: `PathLike` - `atime`: `TimeLike` - `mtime`: `TimeLike` **Returns:** `void`
(mkdir path)
(mkdir path callback)
(mkdir path options)
(mkdir path options callback)
Asynchronously creates a directory.
The callback is given a possible exception and, if recursive
is true
, the
first directory path created, (err[, path])
.path
can still be undefined
when recursive
is true
, if no directory was
created (for instance, if it was previously created).
The optional options
argument can be an integer specifying mode
(permission
and sticky bits), or an object with a mode
property and a recursive
property indicating whether parent directories should be created. Calling fs.mkdir()
when path
is a directory that
exists results in an error only
when recursive
is false. If recursive
is false and the directory exists,
an EEXIST
error occurs.
import { mkdir } from 'node:fs';
// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.
mkdir('./tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});
On Windows, using fs.mkdir()
on the root directory even with recursion will
result in an error:
import { mkdir } from 'node:fs';
mkdir('/', { recursive: true }, (err) => {
// => [Error: EPERM: operation not permitted, mkdir 'C:\']
});
See the POSIX mkdir(2)
documentation for more details.
Asynchronous mkdir(2) - create a directory.
Asynchronous mkdir(2) - create a directory with a mode of 0o777
.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.callback
: NoParamCallback
options
: Mode | MakeDirectoryOptions | null | undefined
- Either the file mode, or an object optionally specifying the file mode and whether parent folders
should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to 0o777
.Returns: void
Asynchronously creates a directory. The callback is given a possible exception and, if `recursive` is `true`, the first directory path created, `(err[, path])`.`path` can still be `undefined` when `recursive` is `true`, if no directory was created (for instance, if it was previously created). The optional `options` argument can be an integer specifying `mode` (permission and sticky bits), or an object with a `mode` property and a `recursive` property indicating whether parent directories should be created. Calling `fs.mkdir()` when `path` is a directory that exists results in an error only when `recursive` is false. If `recursive` is false and the directory exists, an `EEXIST` error occurs. ```js import { mkdir } from 'node:fs'; // Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist. mkdir('./tmp/a/apple', { recursive: true }, (err) => { if (err) throw err; }); ``` On Windows, using `fs.mkdir()` on the root directory even with recursion will result in an error: ```js import { mkdir } from 'node:fs'; mkdir('/', { recursive: true }, (err) => { // => [Error: EPERM: operation not permitted, mkdir 'C:\'] }); ``` See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details. Asynchronous mkdir(2) - create a directory. Asynchronous mkdir(2) - create a directory with a mode of `0o777`. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `callback`: `NoParamCallback` - `options`: `Mode | MakeDirectoryOptions | null | undefined` - Either the file mode, or an object optionally specifying the file mode and whether parent folders should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. **Returns:** `void`
(mkdir-sync path)
(mkdir-sync path options)
Synchronously creates a directory. Returns undefined
, or if recursive
is true
, the first directory path created.
This is the synchronous version of {@link mkdir }.
See the POSIX mkdir(2)
documentation for more details.
Synchronous mkdir(2) - create a directory.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.options
: Mode | MakeDirectoryOptions | null | undefined
- Either the file mode, or an object optionally specifying the file mode and whether parent folders
should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to 0o777
.Returns: string | undefined
Synchronously creates a directory. Returns `undefined`, or if `recursive` is `true`, the first directory path created. This is the synchronous version of {@link mkdir }. See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details. Synchronous mkdir(2) - create a directory. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `options`: `Mode | MakeDirectoryOptions | null | undefined` - Either the file mode, or an object optionally specifying the file mode and whether parent folders should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. **Returns:** `string | undefined`
(mkdtemp prefix)
(mkdtemp prefix callback)
(mkdtemp prefix options)
(mkdtemp prefix options callback)
Creates a unique temporary directory.
Generates six random characters to be appended behind a required prefix
to create a unique temporary directory. Due to platform
inconsistencies, avoid trailing X
characters in prefix
. Some platforms,
notably the BSDs, can return more than six random characters, and replace
trailing X
characters in prefix
with random characters.
The created directory path is passed as a string to the callback's second parameter.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use.
import { mkdtemp } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => {
if (err) throw err;
console.log(directory);
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
});
The fs.mkdtemp()
method will append the six randomly selected characters
directly to the prefix
string. For instance, given a directory /tmp
, if the
intention is to create a temporary directory within/tmp
, the prefix
must end with a trailing platform-specific path separator
(require('node:path').sep
).
import { tmpdir } from 'node:os';
import { mkdtemp } from 'node:fs';
// The parent directory for the new temporary directory
const tmpDir = tmpdir();
// This method is *INCORRECT*:
mkdtemp(tmpDir, (err, directory) => {
if (err) throw err;
console.log(directory);
// Will print something similar to `/tmpabc123`.
// A new temporary directory is created at the file system root
// rather than *within* the /tmp directory.
});
// This method is *CORRECT*:
import { sep } from 'node:path';
mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
if (err) throw err;
console.log(directory);
// Will print something similar to `/tmp/abc123`.
// A new temporary directory is created within
// the /tmp directory.
});
Asynchronously creates a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
Parameters:
prefix
: string
callback
: (err: ErrnoException | null, folder: string) => void
options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: void
Creates a unique temporary directory. Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory. Due to platform inconsistencies, avoid trailing `X` characters in `prefix`. Some platforms, notably the BSDs, can return more than six random characters, and replace trailing `X` characters in `prefix` with random characters. The created directory path is passed as a string to the callback's second parameter. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use. ```js import { mkdtemp } from 'node:fs'; import { join } from 'node:path'; import { tmpdir } from 'node:os'; mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => { if (err) throw err; console.log(directory); // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 }); ``` The `fs.mkdtemp()` method will append the six randomly selected characters directly to the `prefix` string. For instance, given a directory `/tmp`, if the intention is to create a temporary directory _within_`/tmp`, the `prefix`must end with a trailing platform-specific path separator (`require('node:path').sep`). ```js import { tmpdir } from 'node:os'; import { mkdtemp } from 'node:fs'; // The parent directory for the new temporary directory const tmpDir = tmpdir(); // This method is *INCORRECT*: mkdtemp(tmpDir, (err, directory) => { if (err) throw err; console.log(directory); // Will print something similar to `/tmpabc123`. // A new temporary directory is created at the file system root // rather than *within* the /tmp directory. }); // This method is *CORRECT*: import { sep } from 'node:path'; mkdtemp(`${tmpDir}${sep}`, (err, directory) => { if (err) throw err; console.log(directory); // Will print something similar to `/tmp/abc123`. // A new temporary directory is created within // the /tmp directory. }); ``` Asynchronously creates a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory. **Parameters:** - `prefix`: `string` - `callback`: `(err: ErrnoException | null, folder: string) => void` - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `void`
(mkdtemp-sync prefix)
(mkdtemp-sync prefix options)
Returns the created directory path.
For detailed information, see the documentation of the asynchronous version of this API: {@link mkdtemp }.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use.
Synchronously creates a unique temporary directory.
Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
Parameters:
prefix
: string
options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: string | Buffer
Returns the created directory path. For detailed information, see the documentation of the asynchronous version of this API: {@link mkdtemp }. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use. Synchronously creates a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory. **Parameters:** - `prefix`: `string` - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `string | Buffer`
(open path)
(open path callback)
(open path flags)
(open path flags callback)
(open path flags mode)
(open path flags mode callback)
Asynchronous file open. See the POSIX open(2)
documentation for more details.
mode
sets the file mode (permission and sticky bits), but only if the file was
created. On Windows, only the write permission can be manipulated; see {@link chmod }.
The callback gets two arguments (err, fd)
.
Some characters (< > : " / \ | ? *
) are reserved under Windows as documented
by Naming Files, Paths, and Namespaces. Under NTFS, if the filename contains
a colon, Node.js will open a file system stream, as described by this MSDN page.
Functions based on fs.open()
exhibit this behavior as well:fs.writeFile()
, fs.readFile()
, etc.
Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be 0o666
.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.callback
: (err: ErrnoException | null, fd: number) => void
flags
: OpenMode | undefined
- See support of file system
flags``.mode
: Mode | null | undefined
Returns: void
Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details. `mode` sets the file mode (permission and sticky bits), but only if the file was created. On Windows, only the write permission can be manipulated; see {@link chmod }. The callback gets two arguments `(err, fd)`. Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams). Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc. Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `callback`: `(err: ErrnoException | null, fd: number) => void` - `flags`: `OpenMode | undefined` - See `support of file system `flags``. - `mode`: `Mode | null | undefined` **Returns:** `void`
(open-as-blob path)
(open-as-blob path options)
Returns a Blob
whose data is backed by the given file.
The file must not be modified after the Blob
is created. Any modifications
will cause reading the Blob
data to fail with a DOMException
error.
Synchronous stat operations on the file when the Blob
is created, and before
each read in order to detect whether the file data has been modified on disk.
import { openAsBlob } from 'node:fs';
const blob = await openAsBlob('the.file.txt');
const ab = await blob.arrayBuffer();
blob.stream();
Parameters:
path
: PathLike
options
: OpenAsBlobOptions | undefined
Returns: Promise<Blob>
Returns a `Blob` whose data is backed by the given file. The file must not be modified after the `Blob` is created. Any modifications will cause reading the `Blob` data to fail with a `DOMException` error. Synchronous stat operations on the file when the `Blob` is created, and before each read in order to detect whether the file data has been modified on disk. ```js import { openAsBlob } from 'node:fs'; const blob = await openAsBlob('the.file.txt'); const ab = await blob.arrayBuffer(); blob.stream(); ``` **Parameters:** - `path`: `PathLike` - `options`: `OpenAsBlobOptions | undefined` **Returns:** `Promise<Blob>`
(open-sync path flags)
(open-sync path flags mode)
Returns an integer representing the file descriptor.
For detailed information, see the documentation of the asynchronous version of this API: {@link open }.
Parameters:
path
: PathLike
flags
: OpenMode
mode
: Mode | null | undefined
Returns: number
Returns an integer representing the file descriptor. For detailed information, see the documentation of the asynchronous version of this API: {@link open }. **Parameters:** - `path`: `PathLike` - `flags`: `OpenMode` - `mode`: `Mode | null | undefined` **Returns:** `number`
(opendir path cb)
(opendir path options cb)
Asynchronously open a directory. See the POSIX opendir(3)
documentation for
more details.
Creates an fs.Dir
, which contains all further functions for reading from
and cleaning up the directory.
The encoding
option sets the encoding for the path
while opening the
directory and subsequent read operations.
Parameters:
path
: PathLike
options
: OpenDirOptions
cb
: (err: ErrnoException | null, dir: Dir) => void
Returns: void
Asynchronously open a directory. See the POSIX [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for more details. Creates an `fs.Dir`, which contains all further functions for reading from and cleaning up the directory. The `encoding` option sets the encoding for the `path` while opening the directory and subsequent read operations. **Parameters:** - `path`: `PathLike` - `options`: `OpenDirOptions` - `cb`: `(err: ErrnoException | null, dir: Dir) => void` **Returns:** `void`
(opendir-sync path)
(opendir-sync path options)
Synchronously open a directory. See opendir(3)
.
Creates an fs.Dir
, which contains all further functions for reading from
and cleaning up the directory.
The encoding
option sets the encoding for the path
while opening the
directory and subsequent read operations.
Parameters:
path
: PathLike
options
: OpenDirOptions | undefined
Returns: Dir
Synchronously open a directory. See [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html). Creates an `fs.Dir`, which contains all further functions for reading from and cleaning up the directory. The `encoding` option sets the encoding for the `path` while opening the directory and subsequent read operations. **Parameters:** - `path`: `PathLike` - `options`: `OpenDirOptions | undefined` **Returns:** `Dir`
(read fd callback)
(read fd options callback)
(read fd buffer offset length position callback)
Read data from the file specified by fd
.
The callback is given the three arguments, (err, bytesRead, buffer)
.
If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
If this method is invoked as its util.promisify()
ed version, it returns
a promise for an Object
with bytesRead
and buffer
properties.
Similar to the above fs.read
function, this version takes an optional options
object.
If not otherwise specified in an options
object,
buffer
defaults to Buffer.alloc(16384)
,
offset
defaults to 0
,
length
defaults to buffer.byteLength
, - offset
as of Node 17.6.0
position
defaults to null
Parameters:
fd
: number
buffer
: TBuffer
- The buffer that the data will be written to.callback
: (err: ErrnoException | null, bytesRead: number, buffer: ArrayBufferView) => void
options
: ReadAsyncOptions<TBuffer>
offset
: number
- The position in buffer
to write the data to.length
: number
- The number of bytes to read.position
: ReadPosition | null
- Specifies where to begin reading from in the file. If position
is null
or -1
, data will be read from the current file position, and the file position will be updated. If
position
is an integer, the file position will be unchanged.Returns: void
Read data from the file specified by `fd`. The callback is given the three arguments, `(err, bytesRead, buffer)`. If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero. If this method is invoked as its `util.promisify()` ed version, it returns a promise for an `Object` with `bytesRead` and `buffer` properties. Similar to the above `fs.read` function, this version takes an optional `options` object. If not otherwise specified in an `options` object, `buffer` defaults to `Buffer.alloc(16384)`, `offset` defaults to `0`, `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0 `position` defaults to `null` **Parameters:** - `fd`: `number` - `buffer`: `TBuffer` - The buffer that the data will be written to. - `callback`: `(err: ErrnoException | null, bytesRead: number, buffer: ArrayBufferView) => void` - `options`: `ReadAsyncOptions<TBuffer>` - `offset`: `number` - The position in `buffer` to write the data to. - `length`: `number` - The number of bytes to read. - `position`: `ReadPosition | null` - Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If `position` is an integer, the file position will be unchanged. **Returns:** `void`
(read-file path)
(read-file path callback)
(read-file path options)
(read-file path options callback)
Asynchronously reads the entire contents of a file.
import { readFile } from 'node:fs';
readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
The callback is passed two arguments (err, data)
, where data
is the
contents of the file.
If no encoding is specified, then the raw buffer is returned.
If options
is a string, then it specifies the encoding:
import { readFile } from 'node:fs';
readFile('/etc/passwd', 'utf8', callback);
When the path is a directory, the behavior of fs.readFile()
and {@link readFileSync } is platform-specific. On macOS, Linux, and Windows, an
error will be returned. On FreeBSD, a representation of the directory's contents
will be returned.
import { readFile } from 'node:fs';
// macOS, Linux, and Windows
readFile('<directory>', (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
});
// FreeBSD
readFile('<directory>', (err, data) => {
// => null, <data>
});
It is possible to abort an ongoing request using an AbortSignal
. If a
request is aborted the callback is called with an AbortError
:
import { readFile } from 'node:fs';
const controller = new AbortController();
const signal = controller.signal;
readFile(fileInfo[0].name, { signal }, (err, buf) => {
// ...
});
// When you want to abort the request
controller.abort();
The fs.readFile()
function buffers the entire file. To minimize memory costs,
when possible prefer streaming via fs.createReadStream()
.
Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.readFile
performs.
Asynchronously reads the entire contents of a file.
Parameters:
path
: PathOrFileDescriptor
- 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.callback
: (err: ErrnoException | null, data: Buffer) => void
options
: BufferEncoding | (ObjectEncodingOptions & { flag?: string | undefined; } & Abortable) | null | undefined
- Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'
.Returns: void
Asynchronously reads the entire contents of a file. ```js import { readFile } from 'node:fs'; readFile('/etc/passwd', (err, data) => { if (err) throw err; console.log(data); }); ``` The callback is passed two arguments `(err, data)`, where `data` is the contents of the file. If no encoding is specified, then the raw buffer is returned. If `options` is a string, then it specifies the encoding: ```js import { readFile } from 'node:fs'; readFile('/etc/passwd', 'utf8', callback); ``` When the path is a directory, the behavior of `fs.readFile()` and {@link readFileSync } is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned. ```js import { readFile } from 'node:fs'; // macOS, Linux, and Windows readFile('<directory>', (err, data) => { // => [Error: EISDIR: illegal operation on a directory, read <directory>] }); // FreeBSD readFile('<directory>', (err, data) => { // => null, <data> }); ``` It is possible to abort an ongoing request using an `AbortSignal`. If a request is aborted the callback is called with an `AbortError`: ```js import { readFile } from 'node:fs'; const controller = new AbortController(); const signal = controller.signal; readFile(fileInfo[0].name, { signal }, (err, buf) => { // ... }); // When you want to abort the request controller.abort(); ``` The `fs.readFile()` function buffers the entire file. To minimize memory costs, when possible prefer streaming via `fs.createReadStream()`. Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering `fs.readFile` performs. Asynchronously reads the entire contents of a file. **Parameters:** - `path`: `PathOrFileDescriptor` - 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. - `callback`: `(err: ErrnoException | null, data: Buffer) => void` - `options`: `BufferEncoding | (ObjectEncodingOptions & { flag?: string | undefined; } & Abortable) | null | undefined` - Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to `'r'`. **Returns:** `void`
(read-file-sync path)
(read-file-sync path options)
Returns the contents of the path
.
For detailed information, see the documentation of the asynchronous version of this API: {@link readFile }.
If the encoding
option is specified then this function returns a
string. Otherwise it returns a buffer.
Similar to {@link readFile }, when the path is a directory, the behavior of fs.readFileSync()
is platform-specific.
import { readFileSync } from 'node:fs';
// macOS, Linux, and Windows
readFileSync('<directory>');
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
// FreeBSD
readFileSync('<directory>'); // => <data>
Synchronously reads the entire contents of a file.
Parameters:
path
: PathOrFileDescriptor
- 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.options
: BufferEncoding | (ObjectEncodingOptions & { flag?: string | undefined; }) | null | undefined
- Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'
.Returns: string | Buffer
Returns the contents of the `path`. For detailed information, see the documentation of the asynchronous version of this API: {@link readFile }. If the `encoding` option is specified then this function returns a string. Otherwise it returns a buffer. Similar to {@link readFile }, when the path is a directory, the behavior of `fs.readFileSync()` is platform-specific. ```js import { readFileSync } from 'node:fs'; // macOS, Linux, and Windows readFileSync('<directory>'); // => [Error: EISDIR: illegal operation on a directory, read <directory>] // FreeBSD readFileSync('<directory>'); // => <data> ``` Synchronously reads the entire contents of a file. **Parameters:** - `path`: `PathOrFileDescriptor` - 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. - `options`: `BufferEncoding | (ObjectEncodingOptions & { flag?: string | undefined; }) | null | undefined` - Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to `'r'`. **Returns:** `string | Buffer`
(read-sync fd buffer)
(read-sync fd buffer opts)
(read-sync fd buffer offset length position)
Returns the number of bytesRead
.
For detailed information, see the documentation of the asynchronous version of
this API: {@link read }.
Similar to the above fs.readSync
function, this version takes an optional options
object.
If no options
object is specified, it will default with the above values.
Parameters:
fd
: number
buffer
: ArrayBufferView
offset
: number
opts
: ReadSyncOptions | undefined
length
: number
position
: ReadPosition | null
Returns: number
Returns the number of `bytesRead`. For detailed information, see the documentation of the asynchronous version of this API: {@link read }. Similar to the above `fs.readSync` function, this version takes an optional `options` object. If no `options` object is specified, it will default with the above values. **Parameters:** - `fd`: `number` - `buffer`: `ArrayBufferView` - `offset`: `number` - `opts`: `ReadSyncOptions | undefined` - `length`: `number` - `position`: `ReadPosition | null` **Returns:** `number`
(readdir path)
(readdir path callback)
(readdir path options)
(readdir path options callback)
Reads the contents of a directory. The callback gets two arguments (err, files)
where files
is an array of the names of the files in the directory excluding '.'
and '..'
.
See the POSIX readdir(3)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use for
the filenames passed to the callback. If the encoding
is set to 'buffer'
,
the filenames returned will be passed as Buffer
objects.
If options.withFileTypes
is set to true
, the files
array will contain fs.Dirent
objects.
Asynchronous readdir(3) - read a directory.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.options
: ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; }
- If called with withFileTypes: true
the result data will be an array of Dirent.callback
: (err: ErrnoException | null, files: Dirent[]) => void
Returns: void
Reads the contents of a directory. The callback gets two arguments `(err, files)` where `files` is an array of the names of the files in the directory excluding `'.'` and `'..'`. See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use for the filenames passed to the callback. If the `encoding` is set to `'buffer'`, the filenames returned will be passed as `Buffer` objects. If `options.withFileTypes` is set to `true`, the `files` array will contain `fs.Dirent` objects. Asynchronous readdir(3) - read a directory. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `options`: `ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; }` - If called with `withFileTypes: true` the result data will be an array of Dirent. - `callback`: `(err: ErrnoException | null, files: Dirent[]) => void` **Returns:** `void`
(readdir-sync path)
(readdir-sync path options)
Reads the contents of the directory.
See the POSIX readdir(3)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use for
the filenames returned. If the encoding
is set to 'buffer'
,
the filenames returned will be passed as Buffer
objects.
If options.withFileTypes
is set to true
, the result will contain fs.Dirent
objects.
Synchronous readdir(3) - read a directory.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.options
: ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; }
- If called with withFileTypes: true
the result data will be an array of Dirent.Returns: Dirent[]
Reads the contents of the directory. See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use for the filenames returned. If the `encoding` is set to `'buffer'`, the filenames returned will be passed as `Buffer` objects. If `options.withFileTypes` is set to `true`, the result will contain `fs.Dirent` objects. Synchronous readdir(3) - read a directory. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `options`: `ObjectEncodingOptions & { withFileTypes: true; recursive?: boolean | undefined; }` - If called with `withFileTypes: true` the result data will be an array of Dirent. **Returns:** `Dirent[]`
(readlink path)
(readlink path callback)
(readlink path options)
(readlink path options callback)
Reads the contents of the symbolic link referred to by path
. The callback gets
two arguments (err, linkString)
.
See the POSIX readlink(2)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use for
the link path passed to the callback. If the encoding
is set to 'buffer'
,
the link path returned will be passed as a Buffer
object.
Asynchronous readlink(2) - read value of a symbolic link.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.callback
: (err: ErrnoException | null, linkString: string) => void
options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: void
Reads the contents of the symbolic link referred to by `path`. The callback gets two arguments `(err, linkString)`. See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use for the link path passed to the callback. If the `encoding` is set to `'buffer'`, the link path returned will be passed as a `Buffer` object. Asynchronous readlink(2) - read value of a symbolic link. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `callback`: `(err: ErrnoException | null, linkString: string) => void` - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `void`
(readlink-sync path)
(readlink-sync path options)
Returns the symbolic link's string value.
See the POSIX readlink(2)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use for
the link path returned. If the encoding
is set to 'buffer'
,
the link path returned will be passed as a Buffer
object.
Synchronous readlink(2) - read value of a symbolic link.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: string | Buffer
Returns the symbolic link's string value. See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use for the link path returned. If the `encoding` is set to `'buffer'`, the link path returned will be passed as a `Buffer` object. Synchronous readlink(2) - read value of a symbolic link. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `string | Buffer`
(readv fd buffers cb)
(readv fd buffers position cb)
Read from a file specified by fd
and write to an array of ArrayBufferView
s
using readv()
.
position
is the offset from the beginning of the file from where data
should be read. If typeof position !== 'number'
, the data will be read
from the current position.
The callback will be given three arguments: err
, bytesRead
, and buffers
. bytesRead
is how many bytes were read from the file.
If this method is invoked as its util.promisify()
ed version, it returns
a promise for an Object
with bytesRead
and buffers
properties.
Parameters:
fd
: number
buffers
: readonly ArrayBufferView[]
position
: number
cb
: (err: ErrnoException | null, bytesRead: number, buffers: ArrayBufferView[]) => void
Returns: void
Read from a file specified by `fd` and write to an array of `ArrayBufferView`s using `readv()`. `position` is the offset from the beginning of the file from where data should be read. If `typeof position !== 'number'`, the data will be read from the current position. The callback will be given three arguments: `err`, `bytesRead`, and `buffers`. `bytesRead` is how many bytes were read from the file. If this method is invoked as its `util.promisify()` ed version, it returns a promise for an `Object` with `bytesRead` and `buffers` properties. **Parameters:** - `fd`: `number` - `buffers`: `readonly ArrayBufferView[]` - `position`: `number` - `cb`: `(err: ErrnoException | null, bytesRead: number, buffers: ArrayBufferView[]) => void` **Returns:** `void`
(readv-sync fd buffers)
(readv-sync fd buffers position)
For detailed information, see the documentation of the asynchronous version of this API: {@link readv }.
Parameters:
fd
: number
buffers
: readonly ArrayBufferView[]
position
: number | undefined
Returns: number
For detailed information, see the documentation of the asynchronous version of this API: {@link readv }. **Parameters:** - `fd`: `number` - `buffers`: `readonly ArrayBufferView[]` - `position`: `number | undefined` **Returns:** `number`
(realpath path)
(realpath path callback)
(realpath path options)
(realpath path options callback)
Asynchronously computes the canonical pathname by resolving .
, ..
, and
symbolic links.
A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames.
This function behaves like realpath(3)
, with some exceptions:
realpath(3)
implementation supports.The callback
gets two arguments (err, resolvedPath)
. May use process.cwd
to resolve relative paths.
Only paths that can be converted to UTF8 strings are supported.
The optional options
argument can be a string specifying an encoding, or an
object with an encoding
property specifying the character encoding to use for
the path passed to the callback. If the encoding
is set to 'buffer'
,
the path returned will be passed as a Buffer
object.
If path
resolves to a socket or a pipe, the function will return a system
dependent name for that object.
Asynchronous realpath(3) - return the canonicalized absolute pathname.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.callback
: (err: ErrnoException | null, resolvedPath: string) => void
options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: void
Asynchronously computes the canonical pathname by resolving `.`, `..`, and symbolic links. A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames. This function behaves like [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html), with some exceptions: 1. No case conversion is performed on case-insensitive file systems. 2. The maximum number of symbolic links is platform-independent and generally (much) higher than what the native [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html) implementation supports. The `callback` gets two arguments `(err, resolvedPath)`. May use `process.cwd` to resolve relative paths. Only paths that can be converted to UTF8 strings are supported. The optional `options` argument can be a string specifying an encoding, or an object with an `encoding` property specifying the character encoding to use for the path passed to the callback. If the `encoding` is set to `'buffer'`, the path returned will be passed as a `Buffer` object. If `path` resolves to a socket or a pipe, the function will return a system dependent name for that object. Asynchronous realpath(3) - return the canonicalized absolute pathname. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `callback`: `(err: ErrnoException | null, resolvedPath: string) => void` - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `void`
(realpath-sync path)
(realpath-sync path options)
Returns the resolved pathname.
For detailed information, see the documentation of the asynchronous version of this API: {@link realpath }. Synchronous realpath(3) - return the canonicalized absolute pathname.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.options
: EncodingOption
- The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8'
is used.Returns: string | Buffer
Returns the resolved pathname. For detailed information, see the documentation of the asynchronous version of this API: {@link realpath }. Synchronous realpath(3) - return the canonicalized absolute pathname. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `options`: `EncodingOption` - The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. **Returns:** `string | Buffer`
(rename old-path new-path callback)
Asynchronously rename file at oldPath
to the pathname provided
as newPath
. In the case that newPath
already exists, it will
be overwritten. If there is a directory at newPath
, an error will
be raised instead. No arguments other than a possible exception are
given to the completion callback.
See also: rename(2)
.
import { rename } from 'node:fs';
rename('oldFile.txt', 'newFile.txt', (err) => {
if (err) throw err;
console.log('Rename complete!');
});
Parameters:
old-path
: PathLike
new-path
: PathLike
callback
: NoParamCallback
Returns: void
Asynchronously rename file at `oldPath` to the pathname provided as `newPath`. In the case that `newPath` already exists, it will be overwritten. If there is a directory at `newPath`, an error will be raised instead. No arguments other than a possible exception are given to the completion callback. See also: [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html). ```js import { rename } from 'node:fs'; rename('oldFile.txt', 'newFile.txt', (err) => { if (err) throw err; console.log('Rename complete!'); }); ``` **Parameters:** - `old-path`: `PathLike` - `new-path`: `PathLike` - `callback`: `NoParamCallback` **Returns:** `void`
(rename-sync old-path new-path)
Renames the file from oldPath
to newPath
. Returns undefined
.
See the POSIX rename(2)
documentation for more details.
Parameters:
old-path
: PathLike
new-path
: PathLike
Returns: void
Renames the file from `oldPath` to `newPath`. Returns `undefined`. See the POSIX [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html) documentation for more details. **Parameters:** - `old-path`: `PathLike` - `new-path`: `PathLike` **Returns:** `void`
(rm path callback)
(rm path options callback)
Asynchronously removes files and directories (modeled on the standard POSIX rm
utility). No arguments other than a possible exception are given to the
completion callback.
Parameters:
path
: PathLike
options
: RmOptions
callback
: NoParamCallback
Returns: void
Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility). No arguments other than a possible exception are given to the completion callback. **Parameters:** - `path`: `PathLike` - `options`: `RmOptions` - `callback`: `NoParamCallback` **Returns:** `void`
(rm-sync path)
(rm-sync path options)
Synchronously removes files and directories (modeled on the standard POSIX rm
utility). Returns undefined
.
Parameters:
path
: PathLike
options
: RmOptions | undefined
Returns: void
Synchronously removes files and directories (modeled on the standard POSIX `rm` utility). Returns `undefined`. **Parameters:** - `path`: `PathLike` - `options`: `RmOptions | undefined` **Returns:** `void`
(rmdir path callback)
(rmdir path options callback)
Asynchronous rmdir(2)
. No arguments other than a possible exception are given
to the completion callback.
Using fs.rmdir()
on a file (not a directory) results in an ENOENT
error on
Windows and an ENOTDIR
error on POSIX.
To get a behavior similar to the rm -rf
Unix command, use {@link rm } with options { recursive: true, force: true }
.
Parameters:
path
: PathLike
options
: RmDirOptions
callback
: NoParamCallback
Returns: void
Asynchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). No arguments other than a possible exception are given to the completion callback. Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX. To get a behavior similar to the `rm -rf` Unix command, use {@link rm } with options `{ recursive: true, force: true }`. **Parameters:** - `path`: `PathLike` - `options`: `RmDirOptions` - `callback`: `NoParamCallback` **Returns:** `void`
(rmdir-sync path)
(rmdir-sync path options)
Synchronous rmdir(2)
. Returns undefined
.
Using fs.rmdirSync()
on a file (not a directory) results in an ENOENT
error
on Windows and an ENOTDIR
error on POSIX.
To get a behavior similar to the rm -rf
Unix command, use {@link rmSync } with options { recursive: true, force: true }
.
Parameters:
path
: PathLike
options
: RmDirOptions | undefined
Returns: void
Synchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). Returns `undefined`. Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX. To get a behavior similar to the `rm -rf` Unix command, use {@link rmSync } with options `{ recursive: true, force: true }`. **Parameters:** - `path`: `PathLike` - `options`: `RmDirOptions | undefined` **Returns:** `void`
(stat path)
(stat path callback)
(stat path options)
(stat path options callback)
Asynchronous stat(2)
. The callback gets two arguments (err, stats)
wherestats
is an fs.Stats
object.
In case of an error, the err.code
will be one of Common System Errors
.
{@link stat } follows symbolic links. Use {@link lstat } to look at the links themselves.
Using fs.stat()
to check for the existence of a file before callingfs.open()
, fs.readFile()
, or fs.writeFile()
is not recommended.
Instead, user code should open/read/write the file directly and handle the
error raised if the file is not available.
To check if a file exists without manipulating it afterwards, {@link access } is recommended.
For example, given the following directory structure:
- txtDir
-- file.txt
- app.js
The next program will check for the stats of the given paths:
import { stat } from 'node:fs';
const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
for (let i = 0; i < pathsToCheck.length; i++) {
stat(pathsToCheck[i], (err, stats) => {
console.log(stats.isDirectory());
console.log(stats);
});
}
The resulting output will resemble:
true
Stats {
dev: 16777220,
mode: 16877,
nlink: 3,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214262,
size: 96,
blocks: 0,
atimeMs: 1561174653071.963,
mtimeMs: 1561174614583.3518,
ctimeMs: 1561174626623.5366,
birthtimeMs: 1561174126937.2893,
atime: 2019-06-22T03:37:33.072Z,
mtime: 2019-06-22T03:36:54.583Z,
ctime: 2019-06-22T03:37:06.624Z,
birthtime: 2019-06-22T03:28:46.937Z
}
false
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214074,
size: 8,
blocks: 8,
atimeMs: 1561174616618.8555,
mtimeMs: 1561174614584,
ctimeMs: 1561174614583.8145,
birthtimeMs: 1561174007710.7478,
atime: 2019-06-22T03:36:56.619Z,
mtime: 2019-06-22T03:36:54.584Z,
ctime: 2019-06-22T03:36:54.584Z,
birthtime: 2019-06-22T03:26:47.711Z
}
Parameters:
path
: PathLike
options
: StatOptions | undefined
callback
: (err: ErrnoException | null, stats: Stats | BigIntStats) => void
Returns: void
Asynchronous [`stat(2)`](http://man7.org/linux/man-pages/man2/stat.2.html). The callback gets two arguments `(err, stats)` where`stats` is an `fs.Stats` object. In case of an error, the `err.code` will be one of `Common System Errors`. {@link stat } follows symbolic links. Use {@link lstat } to look at the links themselves. Using `fs.stat()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()`, or `fs.writeFile()` is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available. To check if a file exists without manipulating it afterwards, {@link access } is recommended. For example, given the following directory structure: ```text - txtDir -- file.txt - app.js ``` The next program will check for the stats of the given paths: ```js import { stat } from 'node:fs'; const pathsToCheck = ['./txtDir', './txtDir/file.txt']; for (let i = 0; i < pathsToCheck.length; i++) { stat(pathsToCheck[i], (err, stats) => { console.log(stats.isDirectory()); console.log(stats); }); } ``` The resulting output will resemble: ```console true Stats { dev: 16777220, mode: 16877, nlink: 3, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 14214262, size: 96, blocks: 0, atimeMs: 1561174653071.963, mtimeMs: 1561174614583.3518, ctimeMs: 1561174626623.5366, birthtimeMs: 1561174126937.2893, atime: 2019-06-22T03:37:33.072Z, mtime: 2019-06-22T03:36:54.583Z, ctime: 2019-06-22T03:37:06.624Z, birthtime: 2019-06-22T03:28:46.937Z } false Stats { dev: 16777220, mode: 33188, nlink: 1, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 14214074, size: 8, blocks: 8, atimeMs: 1561174616618.8555, mtimeMs: 1561174614584, ctimeMs: 1561174614583.8145, birthtimeMs: 1561174007710.7478, atime: 2019-06-22T03:36:56.619Z, mtime: 2019-06-22T03:36:54.584Z, ctime: 2019-06-22T03:36:54.584Z, birthtime: 2019-06-22T03:26:47.711Z } ``` **Parameters:** - `path`: `PathLike` - `options`: `StatOptions | undefined` - `callback`: `(err: ErrnoException | null, stats: Stats | BigIntStats) => void` **Returns:** `void`
(stat-sync)
(stat-sync path)
(stat-sync path options)
Synchronous stat(2) - Get file status.
Parameters:
path
: PathLike
options
: StatSyncOptions | undefined
Returns: Stats | BigIntStats | undefined
Synchronous stat(2) - Get file status. **Parameters:** - `path`: `PathLike` - `options`: `StatSyncOptions | undefined` **Returns:** `Stats | BigIntStats | undefined`
(statfs path)
(statfs path callback)
(statfs path options)
(statfs path options callback)
Asynchronous statfs(2)
. Returns information about the mounted file system which
contains path
. The callback gets two arguments (err, stats)
where stats
is an fs.StatFs
object.
In case of an error, the err.code
will be one of Common System Errors
.
Parameters:
path
: PathLike
options
: StatFsOptions | undefined
callback
: (err: ErrnoException | null, stats: StatsFs | BigIntStatsFs) => void
Returns: void
Asynchronous [`statfs(2)`](http://man7.org/linux/man-pages/man2/statfs.2.html). Returns information about the mounted file system which contains `path`. The callback gets two arguments `(err, stats)` where `stats`is an `fs.StatFs` object. In case of an error, the `err.code` will be one of `Common System Errors`. **Parameters:** - `path`: `PathLike` - `options`: `StatFsOptions | undefined` - `callback`: `(err: ErrnoException | null, stats: StatsFs | BigIntStatsFs) => void` **Returns:** `void`
(statfs-sync path)
(statfs-sync path options)
Synchronous statfs(2)
. Returns information about the mounted file system which
contains path
.
In case of an error, the err.code
will be one of Common System Errors
.
Parameters:
path
: PathLike
options
: StatFsOptions | undefined
Returns: StatsFs | BigIntStatsFs
Synchronous [`statfs(2)`](http://man7.org/linux/man-pages/man2/statfs.2.html). Returns information about the mounted file system which contains `path`. In case of an error, the `err.code` will be one of `Common System Errors`. **Parameters:** - `path`: `PathLike` - `options`: `StatFsOptions | undefined` **Returns:** `StatsFs | BigIntStatsFs`
(symlink target path)
(symlink target path callback)
(symlink target path type)
(symlink target path type callback)
Creates the link called path
pointing to target
. No arguments other than a
possible exception are given to the completion callback.
See the POSIX symlink(2)
documentation for more details.
The type
argument is only available on Windows and ignored on other platforms.
It can be set to 'dir'
, 'file'
, or 'junction'
. If the type
argument is
not a string, Node.js will autodetect target
type and use 'file'
or 'dir'
.
If the target
does not exist, 'file'
will be used. Windows junction points
require the destination path to be absolute. When using 'junction'
, thetarget
argument will automatically be normalized to absolute path. Junction
points on NTFS volumes can only point to directories.
Relative targets are relative to the link's parent directory.
import { symlink } from 'node:fs';
symlink('./mew', './mewtwo', callback);
The above example creates a symbolic link mewtwo
which points to mew
in the
same directory:
$ tree .
.
├── mew
└── mewtwo -> ./mew
Asynchronous symlink(2) - Create a new symbolic link to an existing file.
Parameters:
target
: PathLike
- A path to an existing file. If a URL is provided, it must use the file:
protocol.path
: PathLike
- A path to the new symlink. If a URL is provided, it must use the file:
protocol.callback
: NoParamCallback
type
: Type | null | undefined
Returns: void
Creates the link called `path` pointing to `target`. No arguments other than a possible exception are given to the completion callback. See the POSIX [`symlink(2)`](http://man7.org/linux/man-pages/man2/symlink.2.html) documentation for more details. The `type` argument is only available on Windows and ignored on other platforms. It can be set to `'dir'`, `'file'`, or `'junction'`. If the `type` argument is not a string, Node.js will autodetect `target` type and use `'file'` or `'dir'`. If the `target` does not exist, `'file'` will be used. Windows junction points require the destination path to be absolute. When using `'junction'`, the`target` argument will automatically be normalized to absolute path. Junction points on NTFS volumes can only point to directories. Relative targets are relative to the link's parent directory. ```js import { symlink } from 'node:fs'; symlink('./mew', './mewtwo', callback); ``` The above example creates a symbolic link `mewtwo` which points to `mew` in the same directory: ```bash $ tree . . ├── mew └── mewtwo -> ./mew ``` Asynchronous symlink(2) - Create a new symbolic link to an existing file. **Parameters:** - `target`: `PathLike` - A path to an existing file. If a URL is provided, it must use the `file:` protocol. - `path`: `PathLike` - A path to the new symlink. If a URL is provided, it must use the `file:` protocol. - `callback`: `NoParamCallback` - `type`: `Type | null | undefined` **Returns:** `void`
(symlink-sync target path)
(symlink-sync target path type)
Returns undefined
.
For detailed information, see the documentation of the asynchronous version of this API: {@link symlink }.
Parameters:
target
: PathLike
path
: PathLike
type
: Type | null | undefined
Returns: void
Returns `undefined`. For detailed information, see the documentation of the asynchronous version of this API: {@link symlink }. **Parameters:** - `target`: `PathLike` - `path`: `PathLike` - `type`: `Type | null | undefined` **Returns:** `void`
(truncate path)
(truncate path callback)
(truncate path len)
(truncate path len callback)
Truncates the file. No arguments other than a possible exception are
given to the completion callback. A file descriptor can also be passed as the
first argument. In this case, fs.ftruncate()
is called.
import { truncate } from 'node:fs';
// Assuming that 'path/file.txt' is a regular file.
truncate('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was truncated');
});
Passing a file descriptor is deprecated and may result in an error being thrown in the future.
See the POSIX truncate(2)
documentation for more details.
Asynchronous truncate(2) - Truncate a file to a specified length.
Parameters:
path
: PathLike
- A path to a file. If a URL is provided, it must use the file:
protocol.callback
: NoParamCallback
len
: number | null | undefined
Returns: void
Truncates the file. No arguments other than a possible exception are given to the completion callback. A file descriptor can also be passed as the first argument. In this case, `fs.ftruncate()` is called. ```js import { truncate } from 'node:fs'; // Assuming that 'path/file.txt' is a regular file. truncate('path/file.txt', (err) => { if (err) throw err; console.log('path/file.txt was truncated'); }); ``` Passing a file descriptor is deprecated and may result in an error being thrown in the future. See the POSIX [`truncate(2)`](http://man7.org/linux/man-pages/man2/truncate.2.html) documentation for more details. Asynchronous truncate(2) - Truncate a file to a specified length. **Parameters:** - `path`: `PathLike` - A path to a file. If a URL is provided, it must use the `file:` protocol. - `callback`: `NoParamCallback` - `len`: `number | null | undefined` **Returns:** `void`
(truncate-sync path)
(truncate-sync path len)
Truncates the file. Returns undefined
. A file descriptor can also be
passed as the first argument. In this case, fs.ftruncateSync()
is called.
Passing a file descriptor is deprecated and may result in an error being thrown in the future.
Parameters:
path
: PathLike
len
: number | null | undefined
Returns: void
Truncates the file. Returns `undefined`. A file descriptor can also be passed as the first argument. In this case, `fs.ftruncateSync()` is called. Passing a file descriptor is deprecated and may result in an error being thrown in the future. **Parameters:** - `path`: `PathLike` - `len`: `number | null | undefined` **Returns:** `void`
(unlink path callback)
Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback.
import { unlink } from 'node:fs';
// Assuming that 'path/file.txt' is a regular file.
unlink('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was deleted');
});
fs.unlink()
will not work on a directory, empty or otherwise. To remove a
directory, use {@link rmdir }.
See the POSIX unlink(2)
documentation for more details.
Parameters:
path
: PathLike
callback
: NoParamCallback
Returns: void
Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback. ```js import { unlink } from 'node:fs'; // Assuming that 'path/file.txt' is a regular file. unlink('path/file.txt', (err) => { if (err) throw err; console.log('path/file.txt was deleted'); }); ``` `fs.unlink()` will not work on a directory, empty or otherwise. To remove a directory, use {@link rmdir }. See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more details. **Parameters:** - `path`: `PathLike` - `callback`: `NoParamCallback` **Returns:** `void`
(unlink-sync path)
Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`. **Parameters:** - `path`: `PathLike` **Returns:** `void`
(unwatch-file filename)
(unwatch-file filename listener)
Stop watching for changes on filename
. If listener
is specified, only that
particular listener is removed. Otherwise, all listeners are removed,
effectively stopping watching of filename
.
Calling fs.unwatchFile()
with a filename that is not being watched is a
no-op, not an error.
Using {@link watch } is more efficient than fs.watchFile()
and fs.unwatchFile()
. fs.watch()
should be used instead of fs.watchFile()
and fs.unwatchFile()
when possible.
Parameters:
filename
: PathLike
listener
: BigIntStatsListener | undefined
Returns: void
Stop watching for changes on `filename`. If `listener` is specified, only that particular listener is removed. Otherwise, _all_ listeners are removed, effectively stopping watching of `filename`. Calling `fs.unwatchFile()` with a filename that is not being watched is a no-op, not an error. Using {@link watch } is more efficient than `fs.watchFile()` and `fs.unwatchFile()`. `fs.watch()` should be used instead of `fs.watchFile()` and `fs.unwatchFile()` when possible. **Parameters:** - `filename`: `PathLike` - `listener`: `BigIntStatsListener | undefined` **Returns:** `void`
(utimes path atime mtime callback)
Change the file system timestamps of the object referenced by path
.
The atime
and mtime
arguments follow these rules:
Date
s, or a numeric string like '123456789.0'
.NaN
, Infinity
, or -Infinity
, an Error
will be thrown.Parameters:
path
: PathLike
atime
: TimeLike
mtime
: TimeLike
callback
: NoParamCallback
Returns: void
Change the file system timestamps of the object referenced by `path`. The `atime` and `mtime` arguments follow these rules: * Values can be either numbers representing Unix epoch time in seconds, `Date`s, or a numeric string like `'123456789.0'`. * If the value can not be converted to a number, or is `NaN`, `Infinity`, or `-Infinity`, an `Error` will be thrown. **Parameters:** - `path`: `PathLike` - `atime`: `TimeLike` - `mtime`: `TimeLike` - `callback`: `NoParamCallback` **Returns:** `void`
(utimes-sync path atime mtime)
Returns undefined
.
For detailed information, see the documentation of the asynchronous version of this API: {@link utimes }.
Parameters:
path
: PathLike
atime
: TimeLike
mtime
: TimeLike
Returns: void
Returns `undefined`. For detailed information, see the documentation of the asynchronous version of this API: {@link utimes }. **Parameters:** - `path`: `PathLike` - `atime`: `TimeLike` - `mtime`: `TimeLike` **Returns:** `void`
(watch filename)
(watch filename listener)
(watch filename options)
(watch filename options listener)
Watch for changes on filename
, where filename
is either a file or a
directory.
The second argument is optional. If options
is provided as a string, it
specifies the encoding
. Otherwise options
should be passed as an object.
The listener callback gets two arguments (eventType, filename)
. eventType
is either 'rename'
or 'change'
, and filename
is the name of the file
which triggered the event.
On most platforms, 'rename'
is emitted whenever a filename appears or
disappears in the directory.
The listener callback is attached to the 'change'
event fired by fs.FSWatcher
, but it is not the same thing as the 'change'
value of eventType
.
If a signal
is passed, aborting the corresponding AbortController will close
the returned fs.FSWatcher
.
Watch for changes on filename
, where filename
is either a file or a directory, returning an FSWatcher
.
Parameters:
filename
: PathLike
- A path to a file or directory. If a URL is provided, it must use the file:
protocol.listener
: WatchListener<string> | undefined
options
: string | WatchOptions
- Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
If encoding
is not supplied, the default of 'utf8'
is used.
If persistent
is not supplied, the default of true
is used.
If recursive
is not supplied, the default of false
is used.Returns: FSWatcher
Watch for changes on `filename`, where `filename` is either a file or a directory. The second argument is optional. If `options` is provided as a string, it specifies the `encoding`. Otherwise `options` should be passed as an object. The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file which triggered the event. On most platforms, `'rename'` is emitted whenever a filename appears or disappears in the directory. The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of `eventType`. If a `signal` is passed, aborting the corresponding AbortController will close the returned `fs.FSWatcher`. Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`. **Parameters:** - `filename`: `PathLike` - A path to a file or directory. If a URL is provided, it must use the `file:` protocol. - `listener`: `WatchListener<string> | undefined` - `options`: `string | WatchOptions` - Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options. If `encoding` is not supplied, the default of `'utf8'` is used. If `persistent` is not supplied, the default of `true` is used. If `recursive` is not supplied, the default of `false` is used. **Returns:** `FSWatcher`
(watch-file filename)
(watch-file filename listener)
(watch-file filename options)
(watch-file filename options listener)
Watch for changes on filename
. The callback listener
will be called each
time the file is accessed.
The options
argument may be omitted. If provided, it should be an object. The options
object may contain a boolean named persistent
that indicates
whether the process should continue to run as long as files are being watched.
The options
object may specify an interval
property indicating how often the
target should be polled in milliseconds.
The listener
gets two arguments the current stat object and the previous
stat object:
import { watchFile } from 'node:fs';
watchFile('message.text', (curr, prev) => {
console.log(`the current mtime is: ${curr.mtime}`);
console.log(`the previous mtime was: ${prev.mtime}`);
});
These stat objects are instances of fs.Stat
. If the bigint
option is true
,
the numeric values in these objects are specified as BigInt
s.
To be notified when the file was modified, not just accessed, it is necessary
to compare curr.mtimeMs
and prev.mtimeMs
.
When an fs.watchFile
operation results in an ENOENT
error, it
will invoke the listener once, with all the fields zeroed (or, for dates, the
Unix Epoch). If the file is created later on, the listener will be called
again, with the latest stat objects. This is a change in functionality since
v0.10.
Using {@link watch } is more efficient than fs.watchFile
and fs.unwatchFile
. fs.watch
should be used instead of fs.watchFile
and fs.unwatchFile
when possible.
When a file being watched by fs.watchFile()
disappears and reappears,
then the contents of previous
in the second callback event (the file's
reappearance) will be the same as the contents of previous
in the first
callback event (its disappearance).
This happens when:
filename
. The callback listener
will be called each time the file is accessed.Parameters:
filename
: PathLike
- A path to a file or directory. If a URL is provided, it must use the file:
protocol.listener
: StatsListener
options
: (WatchFileOptions & { bigint: true; }) | undefined
Returns: StatWatcher
Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed. The `options` argument may be omitted. If provided, it should be an object. The `options` object may contain a boolean named `persistent` that indicates whether the process should continue to run as long as files are being watched. The `options` object may specify an `interval` property indicating how often the target should be polled in milliseconds. The `listener` gets two arguments the current stat object and the previous stat object: ```js import { watchFile } from 'node:fs'; watchFile('message.text', (curr, prev) => { console.log(`the current mtime is: ${curr.mtime}`); console.log(`the previous mtime was: ${prev.mtime}`); }); ``` These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`, the numeric values in these objects are specified as `BigInt`s. To be notified when the file was modified, not just accessed, it is necessary to compare `curr.mtimeMs` and `prev.mtimeMs`. When an `fs.watchFile` operation results in an `ENOENT` error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). If the file is created later on, the listener will be called again, with the latest stat objects. This is a change in functionality since v0.10. Using {@link watch } is more efficient than `fs.watchFile` and `fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile` when possible. When a file being watched by `fs.watchFile()` disappears and reappears, then the contents of `previous` in the second callback event (the file's reappearance) will be the same as the contents of `previous` in the first callback event (its disappearance). This happens when: * the file is deleted, followed by a restore * the file is renamed and then renamed a second time back to its original name Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed. **Parameters:** - `filename`: `PathLike` - A path to a file or directory. If a URL is provided, it must use the `file:` protocol. - `listener`: `StatsListener` - `options`: `(WatchFileOptions & { bigint: true; }) | undefined` **Returns:** `StatWatcher`
(write fd buffer)
(write fd string)
(write fd buffer callback)
(write fd buffer offset)
(write fd string callback)
(write fd string position)
(write fd buffer offset callback)
(write fd buffer offset length)
(write fd string position callback)
(write fd string position encoding)
(write fd buffer offset length callback)
(write fd buffer offset length position)
(write fd string position encoding callback)
(write fd buffer offset length position callback)
Write buffer
to the file specified by fd
.
offset
determines the part of the buffer to be written, and length
is
an integer specifying the number of bytes to write.
position
refers to the offset from the beginning of the file where this data
should be written. If typeof position !== 'number'
, the data will be written
at the current position. See pwrite(2)
.
The callback will be given three arguments (err, bytesWritten, buffer)
where bytesWritten
specifies how many bytes were written from buffer
.
If this method is invoked as its util.promisify()
ed version, it returns
a promise for an Object
with bytesWritten
and buffer
properties.
It is unsafe to use fs.write()
multiple times on the same file without waiting
for the callback. For this scenario, {@link createWriteStream } is
recommended.
On Linux, positional writes don't 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.
Asynchronously writes buffer
to the file referenced by the supplied file descriptor.
Asynchronously writes string
to the file referenced by the supplied file descriptor.
Parameters:
fd
: number
- A file descriptor.buffer
: TBuffer
string
: string
- A string to write.callback
: (err: ErrnoException | null, written: number, str: string) => void
offset
: number | null | undefined
- The part of the buffer to be written. If not supplied, defaults to 0
.position
: number | null | undefined
- The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.encoding
: BufferEncoding | null | undefined
- The expected string encoding.length
: number | null | undefined
- The number of bytes to write. If not supplied, defaults to buffer.length - offset
.Returns: void
Write `buffer` to the file specified by `fd`. `offset` determines the part of the buffer to be written, and `length` is an integer specifying the number of bytes to write. `position` refers to the offset from the beginning of the file where this data should be written. If `typeof position !== 'number'`, the data will be written at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html). The callback will be given three arguments `(err, bytesWritten, buffer)` where `bytesWritten` specifies how many _bytes_ were written from `buffer`. If this method is invoked as its `util.promisify()` ed version, it returns a promise for an `Object` with `bytesWritten` and `buffer` properties. It is unsafe to use `fs.write()` multiple times on the same file without waiting for the callback. For this scenario, {@link createWriteStream } is recommended. On Linux, positional writes don't 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. Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. Asynchronously writes `string` to the file referenced by the supplied file descriptor. **Parameters:** - `fd`: `number` - A file descriptor. - `buffer`: `TBuffer` - `string`: `string` - A string to write. - `callback`: `(err: ErrnoException | null, written: number, str: string) => void` - `offset`: `number | null | undefined` - The part of the buffer to be written. If not supplied, defaults to `0`. - `position`: `number | null | undefined` - The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. - `encoding`: `BufferEncoding | null | undefined` - The expected string encoding. - `length`: `number | null | undefined` - The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. **Returns:** `void`
(write-file path data callback)
(write-file file data options callback)
When file
is a filename, asynchronously writes data to the file, replacing the
file if it already exists. data
can be a string or a buffer.
When file
is a file descriptor, the behavior is similar to calling fs.write()
directly (which is recommended). See the notes below on using
a file descriptor.
The encoding
option is ignored if data
is a buffer.
The mode
option only affects the newly created file. See {@link open } for more details.
import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
If options
is a string, then it specifies the encoding:
import { writeFile } from 'node:fs';
writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
It is unsafe to use fs.writeFile()
multiple times on the same file without
waiting for the callback. For this scenario, {@link createWriteStream } is
recommended.
Similarly to fs.readFile
- fs.writeFile
is a convenience method that
performs multiple write
calls internally to write the buffer passed to it.
For performance sensitive code consider using {@link createWriteStream }.
It is possible to use an AbortSignal
to cancel an fs.writeFile()
.
Cancelation is "best effort", and some amount of data is likely still
to be written.
import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';
const controller = new AbortController();
const { signal } = controller;
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, { signal }, (err) => {
// When a request is aborted - the callback is called with an AbortError
});
// When the request should be aborted
controller.abort();
Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering fs.writeFile
performs.
Asynchronously writes data to a file, replacing the file if it already exists.
Parameters:
file
: PathOrFileDescriptor
- filename or file descriptorpath
: PathOrFileDescriptor
- 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.data
: string | ArrayBufferView
- The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.callback
: NoParamCallback
options
: WriteFileOptions
Returns: void
When `file` is a filename, asynchronously writes data to the file, replacing the file if it already exists. `data` can be a string or a buffer. When `file` is a file descriptor, the behavior is similar to calling `fs.write()` directly (which is recommended). See the notes below on using a file descriptor. The `encoding` option is ignored if `data` is a buffer. The `mode` option only affects the newly created file. See {@link open } for more details. ```js import { writeFile } from 'node:fs'; import { Buffer } from 'node:buffer'; const data = new Uint8Array(Buffer.from('Hello Node.js')); writeFile('message.txt', data, (err) => { if (err) throw err; console.log('The file has been saved!'); }); ``` If `options` is a string, then it specifies the encoding: ```js import { writeFile } from 'node:fs'; writeFile('message.txt', 'Hello Node.js', 'utf8', callback); ``` It is unsafe to use `fs.writeFile()` multiple times on the same file without waiting for the callback. For this scenario, {@link createWriteStream } is recommended. Similarly to `fs.readFile` \- `fs.writeFile` is a convenience method that performs multiple `write` calls internally to write the buffer passed to it. For performance sensitive code consider using {@link createWriteStream }. It is possible to use an `AbortSignal` to cancel an `fs.writeFile()`. Cancelation is "best effort", and some amount of data is likely still to be written. ```js import { writeFile } from 'node:fs'; import { Buffer } from 'node:buffer'; const controller = new AbortController(); const { signal } = controller; const data = new Uint8Array(Buffer.from('Hello Node.js')); writeFile('message.txt', data, { signal }, (err) => { // When a request is aborted - the callback is called with an AbortError }); // When the request should be aborted controller.abort(); ``` Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering `fs.writeFile` performs. Asynchronously writes data to a file, replacing the file if it already exists. **Parameters:** - `file`: `PathOrFileDescriptor` - filename or file descriptor - `path`: `PathOrFileDescriptor` - 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. - `data`: `string | ArrayBufferView` - The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. - `callback`: `NoParamCallback` - `options`: `WriteFileOptions` **Returns:** `void`
(write-file-sync file data)
(write-file-sync file data options)
Returns undefined
.
The mode
option only affects the newly created file. See {@link open } for more details.
For detailed information, see the documentation of the asynchronous version of this API: {@link writeFile }.
Parameters:
file
: PathOrFileDescriptor
- filename or file descriptordata
: string | ArrayBufferView
options
: WriteFileOptions | undefined
Returns: void
Returns `undefined`. The `mode` option only affects the newly created file. See {@link open } for more details. For detailed information, see the documentation of the asynchronous version of this API: {@link writeFile }. **Parameters:** - `file`: `PathOrFileDescriptor` - filename or file descriptor - `data`: `string | ArrayBufferView` - `options`: `WriteFileOptions | undefined` **Returns:** `void`
(write-sync fd buffer)
(write-sync fd string)
(write-sync fd buffer offset)
(write-sync fd string position)
(write-sync fd buffer offset length)
(write-sync fd string position encoding)
(write-sync fd buffer offset length position)
For detailed information, see the documentation of the asynchronous version of
this API: {@link write }.
Synchronously writes string
to the file referenced by the supplied file descriptor, returning the number of bytes written.
Parameters:
fd
: number
- A file descriptor.buffer
: ArrayBufferView
string
: string
- A string to write.offset
: number | null | undefined
position
: number | null | undefined
- The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.encoding
: BufferEncoding | null | undefined
- The expected string encoding.length
: number | null | undefined
Returns: number
For detailed information, see the documentation of the asynchronous version of this API: {@link write }. Synchronously writes `string` to the file referenced by the supplied file descriptor, returning the number of bytes written. **Parameters:** - `fd`: `number` - A file descriptor. - `buffer`: `ArrayBufferView` - `string`: `string` - A string to write. - `offset`: `number | null | undefined` - `position`: `number | null | undefined` - The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. - `encoding`: `BufferEncoding | null | undefined` - The expected string encoding. - `length`: `number | null | undefined` **Returns:** `number`
(writev fd buffers cb)
(writev fd buffers position cb)
Write an array of ArrayBufferView
s to the file specified by fd
using writev()
.
position
is the offset from the beginning of the file where this data
should be written. If typeof position !== 'number'
, the data will be written
at the current position.
The callback will be given three arguments: err
, bytesWritten
, and buffers
. bytesWritten
is how many bytes were written from buffers
.
If this method is util.promisify()
ed, it returns a promise for an Object
with bytesWritten
and buffers
properties.
It is unsafe to use fs.writev()
multiple times on the same file without
waiting for the callback. For this scenario, use {@link createWriteStream }.
On Linux, positional writes don't 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.
Parameters:
fd
: number
buffers
: readonly ArrayBufferView[]
position
: number
cb
: (err: ErrnoException | null, bytesWritten: number, buffers: ArrayBufferView[]) => void
Returns: void
Write an array of `ArrayBufferView`s to the file specified by `fd` using `writev()`. `position` is the offset from the beginning of the file where this data should be written. If `typeof position !== 'number'`, the data will be written at the current position. The callback will be given three arguments: `err`, `bytesWritten`, and `buffers`. `bytesWritten` is how many bytes were written from `buffers`. If this method is `util.promisify()` ed, it returns a promise for an `Object` with `bytesWritten` and `buffers` properties. It is unsafe to use `fs.writev()` multiple times on the same file without waiting for the callback. For this scenario, use {@link createWriteStream }. On Linux, positional writes don't 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. **Parameters:** - `fd`: `number` - `buffers`: `readonly ArrayBufferView[]` - `position`: `number` - `cb`: `(err: ErrnoException | null, bytesWritten: number, buffers: ArrayBufferView[]) => void` **Returns:** `void`
(writev-sync fd buffers)
(writev-sync fd buffers position)
For detailed information, see the documentation of the asynchronous version of this API: {@link writev }.
Parameters:
fd
: number
buffers
: readonly ArrayBufferView[]
position
: number | undefined
Returns: number
For detailed information, see the documentation of the asynchronous version of this API: {@link writev }. **Parameters:** - `fd`: `number` - `buffers`: `readonly ArrayBufferView[]` - `position`: `number | undefined` **Returns:** `number`
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close