2020-06-14 23:49:34 +02:00
|
|
|
|
# File system
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
|
2012-03-04 02:14:06 +01:00
|
|
|
|
<!--name=fs-->
|
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
The `fs` module provides an API for interacting with the file system in a
|
|
|
|
|
manner closely modeled around standard POSIX functions.
|
|
|
|
|
|
|
|
|
|
To use this module:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
All file system operations have synchronous and asynchronous forms.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-08-14 21:56:26 +02:00
|
|
|
|
The asynchronous form always takes a completion callback as its last argument.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
The arguments passed to the completion callback depend on the method, but the
|
|
|
|
|
first argument is always reserved for an exception. If the operation was
|
|
|
|
|
completed successfully, then the first argument will be `null` or `undefined`.
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
fs.unlink('/tmp/hello', (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('successfully deleted /tmp/hello');
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
Exceptions that occur using synchronous operations are thrown immediately and
|
2019-06-30 17:42:52 +02:00
|
|
|
|
may be handled using `try…catch`, or may be allowed to bubble up.
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
try {
|
|
|
|
|
fs.unlinkSync('/tmp/hello');
|
|
|
|
|
console.log('successfully deleted /tmp/hello');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// handle the error
|
|
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
There is no guaranteed ordering when using asynchronous methods. So the
|
|
|
|
|
following is prone to error because the `fs.stat()` operation may complete
|
|
|
|
|
before the `fs.rename()` operation:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.rename('/tmp/hello', '/tmp/world', (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('renamed complete');
|
|
|
|
|
});
|
|
|
|
|
fs.stat('/tmp/world', (err, stats) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log(`stats: ${JSON.stringify(stats)}`);
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
To correctly order the operations, move the `fs.stat()` call into the callback
|
|
|
|
|
of the `fs.rename()` operation:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.rename('/tmp/hello', '/tmp/world', (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.stat('/tmp/world', (err, stats) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log(`stats: ${JSON.stringify(stats)}`);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-06-02 17:08:53 +02:00
|
|
|
|
In busy processes, use the asynchronous versions of these calls. The synchronous
|
|
|
|
|
versions will block the entire process until they complete, halting all
|
|
|
|
|
connections.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
While it is not recommended, most fs functions allow the callback argument to
|
|
|
|
|
be omitted, in which case a default callback is used that rethrows errors. To
|
|
|
|
|
get a trace to the original call site, set the `NODE_DEBUG` environment
|
|
|
|
|
variable:
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Omitting the callback function on asynchronous fs functions is deprecated and
|
|
|
|
|
may result in an error being thrown in the future.
|
2013-03-13 23:36:52 +01:00
|
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
|
```console
|
2016-01-17 18:39:07 +01:00
|
|
|
|
$ cat script.js
|
|
|
|
|
function bad() {
|
|
|
|
|
require('fs').readFile('/');
|
|
|
|
|
}
|
|
|
|
|
bad();
|
|
|
|
|
|
|
|
|
|
$ env NODE_DEBUG=fs node script.js
|
2016-05-06 15:57:30 +02:00
|
|
|
|
fs.js:88
|
|
|
|
|
throw backtrace;
|
|
|
|
|
^
|
|
|
|
|
Error: EISDIR: illegal operation on a directory, read
|
|
|
|
|
<stack trace.>
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2012-12-04 03:17:52 +01:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
## File paths
|
|
|
|
|
|
|
|
|
|
Most `fs` operations accept filepaths that may be specified in the form of
|
|
|
|
|
a string, a [`Buffer`][], or a [`URL`][] object using the `file:` protocol.
|
|
|
|
|
|
|
|
|
|
String form paths are interpreted as UTF-8 character sequences identifying
|
|
|
|
|
the absolute or relative filename. Relative paths will be resolved relative
|
|
|
|
|
to the current working directory as specified by `process.cwd()`.
|
|
|
|
|
|
|
|
|
|
Example using an absolute path on POSIX:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
fs.open('/open/some/file.txt', 'r', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.close(fd, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Example using a relative path on POSIX (relative to `process.cwd()`):
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('file.txt', 'r', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.close(fd, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Paths specified using a [`Buffer`][] are useful primarily on certain POSIX
|
|
|
|
|
operating systems that treat file paths as opaque byte sequences. On such
|
|
|
|
|
systems, it is possible for a single file path to contain sub-sequences that
|
|
|
|
|
use multiple character encodings. As with string paths, `Buffer` paths may
|
|
|
|
|
be relative or absolute:
|
|
|
|
|
|
|
|
|
|
Example using an absolute path on POSIX:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open(Buffer.from('/open/some/file.txt'), 'r', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.close(fd, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
On Windows, Node.js follows the concept of per-drive working directory. This
|
|
|
|
|
behavior can be observed when using a drive path without a backslash. For
|
2020-03-16 07:48:00 +01:00
|
|
|
|
example `fs.readdirSync('C:\\')` can potentially return a different result than
|
|
|
|
|
`fs.readdirSync('C:')`. For more information, see
|
2017-06-06 11:19:23 +02:00
|
|
|
|
[this MSDN page][MSDN-Rel-Path].
|
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
### URL object support
|
2017-04-25 16:20:58 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v7.6.0
|
|
|
|
|
-->
|
|
|
|
|
For most `fs` module functions, the `path` or `filename` argument may be passed
|
|
|
|
|
as a WHATWG [`URL`][] object. Only [`URL`][] objects using the `file:` protocol
|
|
|
|
|
are supported.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const fileUrl = new URL('file:///tmp/hello');
|
|
|
|
|
|
|
|
|
|
fs.readFileSync(fileUrl);
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
`file:` URLs are always absolute paths.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
|
|
|
|
|
Using WHATWG [`URL`][] objects might introduce platform-specific behaviors.
|
|
|
|
|
|
2020-01-12 16:25:50 +01:00
|
|
|
|
On Windows, `file:` URLs with a host name convert to UNC paths, while `file:`
|
2017-04-25 16:20:58 +02:00
|
|
|
|
URLs with drive letters convert to local absolute paths. `file:` URLs without a
|
2020-01-12 16:25:50 +01:00
|
|
|
|
host name nor a drive letter will result in a throw:
|
2017-04-25 16:20:58 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// On Windows :
|
|
|
|
|
|
|
|
|
|
// - WHATWG file URLs with hostname convert to UNC path
|
|
|
|
|
// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file
|
|
|
|
|
fs.readFileSync(new URL('file://hostname/p/a/t/h/file'));
|
|
|
|
|
|
|
|
|
|
// - WHATWG file URLs with drive letters convert to absolute path
|
|
|
|
|
// file:///C:/tmp/hello => C:\tmp\hello
|
|
|
|
|
fs.readFileSync(new URL('file:///C:/tmp/hello'));
|
|
|
|
|
|
|
|
|
|
// - WHATWG file URLs without hostname must have a drive letters
|
|
|
|
|
fs.readFileSync(new URL('file:///notdriveletter/p/a/t/h/file'));
|
|
|
|
|
fs.readFileSync(new URL('file:///c/p/a/t/h/file'));
|
|
|
|
|
// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
`file:` URLs with drive letters must use `:` as a separator just after
|
2017-04-25 16:20:58 +02:00
|
|
|
|
the drive letter. Using another separator will result in a throw.
|
|
|
|
|
|
2020-01-12 16:25:50 +01:00
|
|
|
|
On all other platforms, `file:` URLs with a host name are unsupported and will
|
2017-04-25 16:20:58 +02:00
|
|
|
|
result in a throw:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// On other platforms:
|
|
|
|
|
|
|
|
|
|
// - WHATWG file URLs with hostname are unsupported
|
|
|
|
|
// file://hostname/p/a/t/h/file => throw!
|
|
|
|
|
fs.readFileSync(new URL('file://hostname/p/a/t/h/file'));
|
|
|
|
|
// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute
|
|
|
|
|
|
|
|
|
|
// - WHATWG file URLs convert to absolute path
|
|
|
|
|
// file:///tmp/hello => /tmp/hello
|
|
|
|
|
fs.readFileSync(new URL('file:///tmp/hello'));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
A `file:` URL having encoded slash characters will result in a throw on all
|
|
|
|
|
platforms:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// On Windows
|
|
|
|
|
fs.readFileSync(new URL('file:///C:/p/a/t/h/%2F'));
|
|
|
|
|
fs.readFileSync(new URL('file:///C:/p/a/t/h/%2f'));
|
|
|
|
|
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
|
|
|
|
|
\ or / characters */
|
|
|
|
|
|
|
|
|
|
// On POSIX
|
|
|
|
|
fs.readFileSync(new URL('file:///p/a/t/h/%2F'));
|
|
|
|
|
fs.readFileSync(new URL('file:///p/a/t/h/%2f'));
|
|
|
|
|
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
|
|
|
|
|
/ characters */
|
|
|
|
|
```
|
2019-08-29 15:28:03 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
On Windows, `file:` URLs having encoded backslash will result in a throw:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// On Windows
|
|
|
|
|
fs.readFileSync(new URL('file:///C:/path/%5C'));
|
|
|
|
|
fs.readFileSync(new URL('file:///C:/path/%5c'));
|
|
|
|
|
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
|
|
|
|
|
\ or / characters */
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
## File descriptors
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
On POSIX systems, for every process, the kernel maintains a table of currently
|
|
|
|
|
open files and resources. Each open file is assigned a simple numeric
|
|
|
|
|
identifier called a *file descriptor*. At the system-level, all file system
|
|
|
|
|
operations use these file descriptors to identify and track each specific
|
|
|
|
|
file. Windows systems use a different but conceptually similar mechanism for
|
|
|
|
|
tracking resources. To simplify things for users, Node.js abstracts away the
|
|
|
|
|
specific differences between operating systems and assigns all open files a
|
|
|
|
|
numeric file descriptor.
|
|
|
|
|
|
|
|
|
|
The `fs.open()` method is used to allocate a new file descriptor. Once
|
|
|
|
|
allocated, the file descriptor may be used to read data from, write data to,
|
|
|
|
|
or request information about the file.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('/open/some/file.txt', 'r', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.fstat(fd, (err, stat) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
// use stat
|
2016-04-02 15:44:18 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
// always close the file descriptor!
|
|
|
|
|
fs.close(fd, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
2016-04-02 15:44:18 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
Most operating systems limit the number of file descriptors that may be open
|
|
|
|
|
at any given time so it is critical to close the descriptor when operations
|
|
|
|
|
are completed. Failure to do so will result in a memory leak that will
|
|
|
|
|
eventually cause an application to crash.
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
## Threadpool usage
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
All file system APIs except `fs.FSWatcher()` and those that are explicitly
|
|
|
|
|
synchronous use libuv's threadpool, which can have surprising and negative
|
|
|
|
|
performance implications for some applications. See the
|
2017-12-15 01:34:57 +01:00
|
|
|
|
[`UV_THREADPOOL_SIZE`][] documentation for more information.
|
2016-04-02 15:44:18 +02:00
|
|
|
|
|
2020-06-10 02:15:31 +02:00
|
|
|
|
## Class: `fs.Dir`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
A class representing a directory stream.
|
|
|
|
|
|
2019-10-09 15:10:19 +02:00
|
|
|
|
Created by [`fs.opendir()`][], [`fs.opendirSync()`][], or
|
|
|
|
|
[`fsPromises.opendir()`][].
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
async function print(path) {
|
|
|
|
|
const dir = await fs.promises.opendir(path);
|
|
|
|
|
for await (const dirent of dir) {
|
|
|
|
|
console.log(dirent.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print('./').catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.close()`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously close the directory's underlying resource handle.
|
|
|
|
|
Subsequent reads will result in errors.
|
|
|
|
|
|
|
|
|
|
A `Promise` is returned that will be resolved after the resource has been
|
|
|
|
|
closed.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.close(callback)`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
|
|
|
|
|
Asynchronously close the directory's underlying resource handle.
|
|
|
|
|
Subsequent reads will result in errors.
|
|
|
|
|
|
|
|
|
|
The `callback` will be called after the resource handle has been closed.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.closeSync()`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Synchronously close the directory's underlying resource handle.
|
|
|
|
|
Subsequent reads will result in errors.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.path`
|
2019-10-09 15:10:19 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v12.12.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {string}
|
|
|
|
|
|
|
|
|
|
The read-only path of this directory as was provided to [`fs.opendir()`][],
|
|
|
|
|
[`fs.opendirSync()`][], or [`fsPromises.opendir()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.read()`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
* Returns: {Promise} containing {fs.Dirent|null}
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
Asynchronously read the next directory entry via readdir(3) as an
|
|
|
|
|
[`fs.Dirent`][].
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
After the read is completed, a `Promise` is returned that will be resolved with
|
|
|
|
|
an [`fs.Dirent`][], or `null` if there are no more directory entries to read.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-10-10 21:49:35 +02:00
|
|
|
|
Directory entries returned by this function are in no particular order as
|
|
|
|
|
provided by the operating system's underlying directory mechanisms.
|
|
|
|
|
Entries added or removed while iterating over the directory may or may not be
|
|
|
|
|
included in the iteration results.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.read(callback)`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
2019-10-08 22:18:08 +02:00
|
|
|
|
* `dirent` {fs.Dirent|null}
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
Asynchronously read the next directory entry via readdir(3) as an
|
|
|
|
|
[`fs.Dirent`][].
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
After the read is completed, the `callback` will be called with an
|
|
|
|
|
[`fs.Dirent`][], or `null` if there are no more directory entries to read.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-10-10 21:49:35 +02:00
|
|
|
|
Directory entries returned by this function are in no particular order as
|
|
|
|
|
provided by the operating system's underlying directory mechanisms.
|
|
|
|
|
Entries added or removed while iterating over the directory may or may not be
|
|
|
|
|
included in the iteration results.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir.readSync()`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
* Returns: {fs.Dirent|null}
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
Synchronously read the next directory entry via readdir(3) as an
|
|
|
|
|
[`fs.Dirent`][].
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
If there are no more directory entries to read, `null` will be returned.
|
|
|
|
|
|
2019-10-10 21:49:35 +02:00
|
|
|
|
Directory entries returned by this function are in no particular order as
|
|
|
|
|
provided by the operating system's underlying directory mechanisms.
|
|
|
|
|
Entries added or removed while iterating over the directory may or may not be
|
|
|
|
|
included in the iteration results.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dir[Symbol.asyncIterator]()`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
* Returns: {AsyncIterator} of {fs.Dirent}
|
|
|
|
|
|
|
|
|
|
Asynchronously iterates over the directory via readdir(3) until all entries have
|
|
|
|
|
been read.
|
|
|
|
|
|
|
|
|
|
Entries returned by the async iterator are always an [`fs.Dirent`][].
|
|
|
|
|
The `null` case from `dir.read()` is handled internally.
|
|
|
|
|
|
|
|
|
|
See [`fs.Dir`][] for an example.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-10-10 21:49:35 +02:00
|
|
|
|
Directory entries returned by this iterator are in no particular order as
|
|
|
|
|
provided by the operating system's underlying directory mechanisms.
|
|
|
|
|
Entries added or removed while iterating over the directory may or may not be
|
|
|
|
|
included in the iteration results.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## Class: `fs.Dirent`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2020-04-12 03:11:44 +02:00
|
|
|
|
A representation of a directory entry, which can be a file or a subdirectory
|
|
|
|
|
within the directory, as returned by reading from an [`fs.Dir`][]. The
|
|
|
|
|
directory entry is a combination of the file name and file type pairs.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
Additionally, when [`fs.readdir()`][] or [`fs.readdirSync()`][] is called with
|
|
|
|
|
the `withFileTypes` option set to `true`, the resulting array is filled with
|
2018-07-28 04:29:32 +02:00
|
|
|
|
`fs.Dirent` objects, rather than strings or `Buffers`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isBlockDevice()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a block device.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isCharacterDevice()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a character device.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isDirectory()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a file system
|
|
|
|
|
directory.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isFIFO()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a first-in-first-out
|
|
|
|
|
(FIFO) pipe.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isFile()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a regular file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isSocket()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a socket.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.isSymbolicLink()`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Dirent` object describes a symbolic link.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `dirent.name`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
<!-- YAML
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
added: v10.10.0
|
2018-07-28 04:29:32 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {string|Buffer}
|
|
|
|
|
|
|
|
|
|
The file name that this `fs.Dirent` object refers to. The type of this
|
|
|
|
|
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
|
|
|
|
|
[`fs.readdirSync()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## Class: `fs.FSWatcher`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.8
|
|
|
|
|
-->
|
2011-07-14 13:17:40 +02:00
|
|
|
|
|
2019-08-25 03:59:14 +02:00
|
|
|
|
* Extends {EventEmitter}
|
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
A successful call to [`fs.watch()`][] method will return a new `fs.FSWatcher`
|
|
|
|
|
object.
|
2016-07-01 10:33:20 +02:00
|
|
|
|
|
2019-08-25 03:59:14 +02:00
|
|
|
|
All `fs.FSWatcher` objects emit a `'change'` event whenever a specific watched
|
|
|
|
|
file is modified.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'change'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.8
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
* `eventType` {string} The type of change event that has occurred
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `filename` {string|Buffer} The filename that changed (if relevant/available)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Emitted when something changes in a watched directory or file.
|
2015-11-28 00:30:32 +01:00
|
|
|
|
See more details in [`fs.watch()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
The `filename` argument may not be provided depending on operating system
|
|
|
|
|
support. If `filename` is provided, it will be provided as a `Buffer` if
|
2016-10-14 00:41:53 +02:00
|
|
|
|
`fs.watch()` is called with its `encoding` option set to `'buffer'`, otherwise
|
2017-12-15 01:34:57 +01:00
|
|
|
|
`filename` will be a UTF-8 string.
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
|
|
|
|
```js
|
2018-04-09 18:30:22 +02:00
|
|
|
|
// Example when handled through fs.watch() listener
|
2017-06-01 01:07:25 +02:00
|
|
|
|
fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
|
2017-06-27 22:32:32 +02:00
|
|
|
|
if (filename) {
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
console.log(filename);
|
|
|
|
|
// Prints: <Buffer ...>
|
2017-06-27 22:32:32 +02:00
|
|
|
|
}
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'close'`
|
2018-04-16 17:03:31 +02:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-04-16 17:03:31 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2018-05-27 00:07:29 +02:00
|
|
|
|
Emitted when the watcher stops watching for changes. The closed
|
|
|
|
|
`fs.FSWatcher` object is no longer usable in the event handler.
|
2018-04-16 17:03:31 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'error'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.8
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `error` {Error}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-05-27 00:07:29 +02:00
|
|
|
|
Emitted when an error occurs while watching the file. The errored
|
|
|
|
|
`fs.FSWatcher` object is no longer usable in the event handler.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `watcher.close()`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.8
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the
|
|
|
|
|
`fs.FSWatcher` object is no longer usable.
|
2012-08-04 21:39:11 +02:00
|
|
|
|
|
2020-04-28 20:00:35 +02:00
|
|
|
|
### `watcher.ref()`
|
|
|
|
|
<!-- YAML
|
2020-05-18 03:45:37 +02:00
|
|
|
|
added: v14.3.0
|
2020-04-28 20:00:35 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {fs.FSWatcher}
|
|
|
|
|
|
|
|
|
|
When called, requests that the Node.js event loop *not* exit so long as the
|
|
|
|
|
`FSWatcher` is active. Calling `watcher.ref()` multiple times will have
|
|
|
|
|
no effect.
|
|
|
|
|
|
|
|
|
|
By default, all `FSWatcher` objects are "ref'ed", making it normally
|
|
|
|
|
unnecessary to call `watcher.ref()` unless `watcher.unref()` had been
|
|
|
|
|
called previously.
|
|
|
|
|
|
|
|
|
|
### `watcher.unref()`
|
|
|
|
|
<!-- YAML
|
2020-05-18 03:45:37 +02:00
|
|
|
|
added: v14.3.0
|
2020-04-28 20:00:35 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {fs.FSWatcher}
|
|
|
|
|
|
|
|
|
|
When called, the active `FSWatcher` object will not require the Node.js
|
|
|
|
|
event loop to remain active. If there is no other activity keeping the
|
|
|
|
|
event loop running, the process may exit before the `FSWatcher` object's
|
|
|
|
|
callback is invoked. Calling `watcher.unref()` multiple times will have
|
|
|
|
|
no effect.
|
|
|
|
|
|
|
|
|
|
## Class: `fs.StatWatcher`
|
|
|
|
|
<!-- YAML
|
2020-05-18 03:45:37 +02:00
|
|
|
|
added: v14.3.0
|
2020-04-28 20:00:35 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Extends {EventEmitter}
|
|
|
|
|
|
|
|
|
|
A successful call to `fs.watchFile()` method will return a new `fs.StatWatcher`
|
|
|
|
|
object.
|
|
|
|
|
|
|
|
|
|
### `watcher.ref()`
|
|
|
|
|
<!-- YAML
|
2020-05-18 03:45:37 +02:00
|
|
|
|
added: v14.3.0
|
2020-04-28 20:00:35 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {fs.StatWatcher}
|
|
|
|
|
|
|
|
|
|
When called, requests that the Node.js event loop *not* exit so long as the
|
|
|
|
|
`StatWatcher` is active. Calling `watcher.ref()` multiple times will have
|
|
|
|
|
no effect.
|
|
|
|
|
|
|
|
|
|
By default, all `StatWatcher` objects are "ref'ed", making it normally
|
|
|
|
|
unnecessary to call `watcher.ref()` unless `watcher.unref()` had been
|
|
|
|
|
called previously.
|
|
|
|
|
|
|
|
|
|
### `watcher.unref()`
|
|
|
|
|
<!-- YAML
|
2020-05-18 03:45:37 +02:00
|
|
|
|
added: v14.3.0
|
2020-04-28 20:00:35 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {fs.StatWatcher}
|
|
|
|
|
|
|
|
|
|
When called, the active `StatWatcher` object will not require the Node.js
|
|
|
|
|
event loop to remain active. If there is no other activity keeping the
|
|
|
|
|
event loop running, the process may exit before the `StatWatcher` object's
|
|
|
|
|
callback is invoked. Calling `watcher.unref()` multiple times will have
|
|
|
|
|
no effect.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## Class: `fs.ReadStream`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2012-08-04 21:39:11 +02:00
|
|
|
|
|
2019-08-25 03:59:14 +02:00
|
|
|
|
* Extends: {stream.Readable}
|
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
A successful call to `fs.createReadStream()` will return a new `fs.ReadStream`
|
|
|
|
|
object.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'close'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2012-08-04 21:39:11 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
Emitted when the `fs.ReadStream`'s underlying file descriptor has been closed.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'open'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2016-05-01 09:58:16 +02:00
|
|
|
|
|
2018-04-11 13:23:27 +02:00
|
|
|
|
* `fd` {integer} Integer file descriptor used by the `ReadStream`.
|
2017-02-13 03:49:35 +01:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
Emitted when the `fs.ReadStream`'s file descriptor has been opened.
|
2016-05-01 09:58:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'ready'`
|
2018-04-12 10:11:51 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v9.11.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Emitted when the `fs.ReadStream` is ready to be used.
|
|
|
|
|
|
|
|
|
|
Fires immediately after `'open'`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `readStream.bytesRead`
|
2016-08-01 21:31:16 +02:00
|
|
|
|
<!-- YAML
|
2018-03-25 12:01:33 +02:00
|
|
|
|
added: v6.4.0
|
2016-08-01 21:31:16 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {number}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The number of bytes that have been read so far.
|
2016-08-01 21:31:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `readStream.path`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2015-12-20 18:15:54 +01:00
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {string|Buffer}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
The path to the file the stream is reading from as specified in the first
|
|
|
|
|
argument to `fs.createReadStream()`. If `path` is passed as a string, then
|
|
|
|
|
`readStream.path` will be a string. If `path` is passed as a `Buffer`, then
|
|
|
|
|
`readStream.path` will be a `Buffer`.
|
2015-12-20 18:15:54 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `readStream.pending`
|
2018-11-03 19:53:06 +01:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v11.2.0
|
|
|
|
|
- v10.16.0
|
2018-11-03 19:53:06 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
This property is `true` if the underlying file has not been opened yet,
|
|
|
|
|
i.e. before the `'ready'` event is emitted.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## Class: `fs.Stats`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-05-23 18:15:56 +02:00
|
|
|
|
changes:
|
2017-11-16 15:32:56 +01:00
|
|
|
|
- version: v8.1.0
|
2017-05-23 18:15:56 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/13173
|
|
|
|
|
description: Added times as numbers.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2017-12-15 01:34:57 +01:00
|
|
|
|
A `fs.Stats` object provides information about a file.
|
|
|
|
|
|
2017-05-23 18:15:56 +02:00
|
|
|
|
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and
|
|
|
|
|
their synchronous counterparts are of this type.
|
2018-04-23 11:14:56 +02:00
|
|
|
|
If `bigint` in the `options` passed to those methods is true, the numeric values
|
2018-06-18 20:58:49 +02:00
|
|
|
|
will be `bigint` instead of `number`, and the object will contain additional
|
|
|
|
|
nanosecond-precision properties suffixed with `Ns`.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2017-05-26 11:19:20 +02:00
|
|
|
|
```console
|
2017-03-25 14:33:03 +01:00
|
|
|
|
Stats {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
dev: 2114,
|
|
|
|
|
ino: 48064969,
|
|
|
|
|
mode: 33188,
|
|
|
|
|
nlink: 1,
|
|
|
|
|
uid: 85,
|
|
|
|
|
gid: 100,
|
|
|
|
|
rdev: 0,
|
|
|
|
|
size: 527,
|
|
|
|
|
blksize: 4096,
|
|
|
|
|
blocks: 8,
|
2017-05-23 18:15:56 +02:00
|
|
|
|
atimeMs: 1318289051000.1,
|
|
|
|
|
mtimeMs: 1318289051000.1,
|
|
|
|
|
ctimeMs: 1318289051000.1,
|
|
|
|
|
birthtimeMs: 1318289051000.1,
|
2016-01-17 18:39:07 +01:00
|
|
|
|
atime: Mon, 10 Oct 2011 23:24:11 GMT,
|
|
|
|
|
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
|
|
|
|
|
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
|
2017-03-25 14:33:03 +01:00
|
|
|
|
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
`bigint` version:
|
|
|
|
|
|
|
|
|
|
```console
|
2018-06-18 20:58:49 +02:00
|
|
|
|
BigIntStats {
|
2018-04-23 11:14:56 +02:00
|
|
|
|
dev: 2114n,
|
|
|
|
|
ino: 48064969n,
|
|
|
|
|
mode: 33188n,
|
|
|
|
|
nlink: 1n,
|
|
|
|
|
uid: 85n,
|
|
|
|
|
gid: 100n,
|
|
|
|
|
rdev: 0n,
|
|
|
|
|
size: 527n,
|
|
|
|
|
blksize: 4096n,
|
|
|
|
|
blocks: 8n,
|
|
|
|
|
atimeMs: 1318289051000n,
|
|
|
|
|
mtimeMs: 1318289051000n,
|
|
|
|
|
ctimeMs: 1318289051000n,
|
|
|
|
|
birthtimeMs: 1318289051000n,
|
2018-06-18 20:58:49 +02:00
|
|
|
|
atimeNs: 1318289051000000000n,
|
|
|
|
|
mtimeNs: 1318289051000000000n,
|
|
|
|
|
ctimeNs: 1318289051000000000n,
|
|
|
|
|
birthtimeNs: 1318289051000000000n,
|
2018-04-23 11:14:56 +02:00
|
|
|
|
atime: Mon, 10 Oct 2011 23:24:11 GMT,
|
|
|
|
|
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
|
|
|
|
|
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
|
|
|
|
|
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isBlockDevice()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a block device.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isCharacterDevice()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a character device.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isDirectory()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a file system directory.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isFIFO()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a first-in-first-out (FIFO)
|
|
|
|
|
pipe.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isFile()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a regular file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isSocket()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a socket.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.isSymbolicLink()`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.10
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the `fs.Stats` object describes a symbolic link.
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
This method is only valid when using [`fs.lstat()`][].
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.dev`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The numeric identifier of the device containing the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.ino`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The file system specific "Inode" number for the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.mode`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
A bit-field describing the file type and mode.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.nlink`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The number of hard-links that exist for the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.uid`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The numeric user identifier of the user that owns the file (POSIX).
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.gid`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The numeric group identifier of the group that owns the file (POSIX).
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.rdev`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
A numeric device identifier if the file is considered "special".
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.size`
|
2018-03-25 03:03:11 +02:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2018-03-25 03:03:11 +02:00
|
|
|
|
|
|
|
|
|
The size of the file in bytes.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.blksize`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The file system block size for i/o operations.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.blocks`
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The number of blocks allocated for this file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.atimeMs`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v8.1.0
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time this file was accessed expressed in
|
|
|
|
|
milliseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.mtimeMs`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v8.1.0
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time this file was modified expressed in
|
|
|
|
|
milliseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.ctimeMs`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v8.1.0
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time the file status was changed expressed
|
|
|
|
|
in milliseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.birthtimeMs`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v8.1.0
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* {number|bigint}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the creation time of this file expressed in
|
|
|
|
|
milliseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.atimeNs`
|
2018-06-18 20:58:49 +02:00
|
|
|
|
<!-- YAML
|
2019-09-04 00:10:04 +02:00
|
|
|
|
added: v12.10.0
|
2018-06-18 20:58:49 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {bigint}
|
|
|
|
|
|
|
|
|
|
Only present when `bigint: true` is passed into the method that generates
|
|
|
|
|
the object.
|
|
|
|
|
The timestamp indicating the last time this file was accessed expressed in
|
|
|
|
|
nanoseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.mtimeNs`
|
2018-06-18 20:58:49 +02:00
|
|
|
|
<!-- YAML
|
2019-09-04 00:10:04 +02:00
|
|
|
|
added: v12.10.0
|
2018-06-18 20:58:49 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {bigint}
|
|
|
|
|
|
|
|
|
|
Only present when `bigint: true` is passed into the method that generates
|
|
|
|
|
the object.
|
|
|
|
|
The timestamp indicating the last time this file was modified expressed in
|
|
|
|
|
nanoseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.ctimeNs`
|
2018-06-18 20:58:49 +02:00
|
|
|
|
<!-- YAML
|
2019-09-04 00:10:04 +02:00
|
|
|
|
added: v12.10.0
|
2018-06-18 20:58:49 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {bigint}
|
|
|
|
|
|
|
|
|
|
Only present when `bigint: true` is passed into the method that generates
|
|
|
|
|
the object.
|
|
|
|
|
The timestamp indicating the last time the file status was changed expressed
|
|
|
|
|
in nanoseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.birthtimeNs`
|
2018-06-18 20:58:49 +02:00
|
|
|
|
<!-- YAML
|
2019-09-04 00:10:04 +02:00
|
|
|
|
added: v12.10.0
|
2018-06-18 20:58:49 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {bigint}
|
|
|
|
|
|
|
|
|
|
Only present when `bigint: true` is passed into the method that generates
|
|
|
|
|
the object.
|
|
|
|
|
The timestamp indicating the creation time of this file expressed in
|
|
|
|
|
nanoseconds since the POSIX Epoch.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.atime`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.13
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {Date}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time this file was accessed.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.mtime`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.13
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {Date}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time this file was modified.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.ctime`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.13
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {Date}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the last time the file status was changed.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `stats.birthtime`
|
2018-03-09 15:56:58 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.13
|
|
|
|
|
-->
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
|
* {Date}
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
|
|
|
|
The timestamp indicating the creation time of this file.
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### Stat time values
|
2017-12-15 01:34:57 +01:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
The `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs` properties are
|
2018-06-18 20:58:49 +02:00
|
|
|
|
numeric values that hold the corresponding times in milliseconds. Their
|
|
|
|
|
precision is platform specific. When `bigint: true` is passed into the
|
|
|
|
|
method that generates the object, the properties will be [bigints][],
|
|
|
|
|
otherwise they will be [numbers][MDN-Number].
|
|
|
|
|
|
|
|
|
|
The `atimeNs`, `mtimeNs`, `ctimeNs`, `birthtimeNs` properties are
|
|
|
|
|
[bigints][] that hold the corresponding times in nanoseconds. They are
|
|
|
|
|
only present when `bigint: true` is passed into the method that generates
|
|
|
|
|
the object. Their precision is platform specific.
|
|
|
|
|
|
|
|
|
|
`atime`, `mtime`, `ctime`, and `birthtime` are
|
2018-02-06 06:55:16 +01:00
|
|
|
|
[`Date`][MDN-Date] object alternate representations of the various times. The
|
|
|
|
|
`Date` and number values are not connected. Assigning a new number value, or
|
|
|
|
|
mutating the `Date` value, will not be reflected in the corresponding alternate
|
|
|
|
|
representation.
|
2017-05-23 18:15:56 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The times in the stat object have the following semantics:
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `atime` "Access Time": Time when file data last accessed. Changed
|
2016-11-16 03:00:30 +01:00
|
|
|
|
by the mknod(2), utimes(2), and read(2) system calls.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `mtime` "Modified Time": Time when file data last modified.
|
2016-11-16 03:00:30 +01:00
|
|
|
|
Changed by the mknod(2), utimes(2), and write(2) system calls.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `ctime` "Change Time": Time when file status was last changed
|
2018-04-02 07:38:48 +02:00
|
|
|
|
(inode data modification). Changed by the chmod(2), chown(2),
|
2016-11-16 03:00:30 +01:00
|
|
|
|
link(2), mknod(2), rename(2), unlink(2), utimes(2),
|
|
|
|
|
read(2), and write(2) system calls.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `birthtime` "Birth Time": Time of file creation. Set once when the
|
2018-04-02 07:38:48 +02:00
|
|
|
|
file is created. On filesystems where birthtime is not available,
|
2015-11-04 18:07:07 +01:00
|
|
|
|
this field may instead hold either the `ctime` or
|
2019-07-06 15:49:58 +02:00
|
|
|
|
`1970-01-01T00:00Z` (ie, Unix epoch timestamp `0`). This value may be greater
|
2018-07-04 01:51:28 +02:00
|
|
|
|
than `atime` or `mtime` in this case. On Darwin and other FreeBSD variants,
|
|
|
|
|
also set if the `atime` is explicitly set to an earlier value than the current
|
|
|
|
|
`birthtime` using the utimes(2) system call.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
Prior to Node.js 0.12, the `ctime` held the `birthtime` on Windows systems. As
|
|
|
|
|
of 0.12, `ctime` is not "creation time", and on Unix systems, it never was.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## Class: `fs.WriteStream`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-08-25 03:59:14 +02:00
|
|
|
|
* Extends {stream.Writable}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'close'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2010-11-21 23:22:34 +01:00
|
|
|
|
|
2017-10-06 17:50:35 +02:00
|
|
|
|
Emitted when the `WriteStream`'s underlying file descriptor has been closed.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'open'`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2016-05-01 09:58:16 +02:00
|
|
|
|
|
2018-04-11 13:23:27 +02:00
|
|
|
|
* `fd` {integer} Integer file descriptor used by the `WriteStream`.
|
2017-02-13 03:49:35 +01:00
|
|
|
|
|
2018-04-11 13:23:27 +02:00
|
|
|
|
Emitted when the `WriteStream`'s file is opened.
|
2016-05-01 09:58:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### Event: `'ready'`
|
2018-04-12 10:11:51 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v9.11.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Emitted when the `fs.WriteStream` is ready to be used.
|
|
|
|
|
|
|
|
|
|
Fires immediately after `'open'`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `writeStream.bytesWritten`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.7
|
|
|
|
|
-->
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The number of bytes written so far. Does not include data that is still queued
|
|
|
|
|
for writing.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `writeStream.path`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.93
|
|
|
|
|
-->
|
2015-12-20 18:15:54 +01:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
The path to the file the stream is writing to as specified in the first
|
2018-09-26 15:15:54 +02:00
|
|
|
|
argument to [`fs.createWriteStream()`][]. If `path` is passed as a string, then
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then
|
|
|
|
|
`writeStream.path` will be a `Buffer`.
|
2015-12-20 18:15:54 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `writeStream.pending`
|
2018-11-03 19:53:06 +01:00
|
|
|
|
<!-- YAML
|
2018-11-14 01:57:14 +01:00
|
|
|
|
added: v11.2.0
|
2018-11-03 19:53:06 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
This property is `true` if the underlying file has not been opened yet,
|
|
|
|
|
i.e. before the `'ready'` event is emitted.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.access(path[, mode], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
2016-06-14 21:57:22 +02:00
|
|
|
|
added: v0.11.15
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-06-11 22:42:06 +02:00
|
|
|
|
- version: v6.3.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6534
|
|
|
|
|
description: The constants like `fs.R_OK`, etc which were present directly
|
|
|
|
|
on `fs` were moved into `fs.constants` as a soft deprecation.
|
2018-03-04 14:46:49 +01:00
|
|
|
|
Thus for Node.js `< v6.3.0` use `fs`
|
|
|
|
|
to access those constants, or
|
2017-06-11 22:42:06 +02:00
|
|
|
|
do something like `(fs.constants || fs).R_OK` to work with all
|
|
|
|
|
versions.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `mode` {integer} **Default:** `fs.constants.F_OK`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2016-06-02 19:39:39 +02:00
|
|
|
|
Tests a user's permissions for the file or directory specified by `path`.
|
|
|
|
|
The `mode` argument is an optional integer that specifies the accessibility
|
2020-06-14 23:49:34 +02:00
|
|
|
|
checks to be performed. Check [File access constants][] for possible values
|
2018-05-06 09:35:21 +02:00
|
|
|
|
of `mode`. It is possible to create a mask consisting of the bitwise OR of
|
|
|
|
|
two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
2012-06-30 02:23:03 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
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
|
2018-05-02 01:31:28 +02:00
|
|
|
|
argument will be an `Error` object. The following examples check if
|
|
|
|
|
`package.json` exists, and if it is readable or writable.
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2018-05-02 01:31:28 +02:00
|
|
|
|
const file = 'package.json';
|
|
|
|
|
|
|
|
|
|
// Check if the file exists in the current directory.
|
|
|
|
|
fs.access(file, fs.constants.F_OK, (err) => {
|
|
|
|
|
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check if the file is readable.
|
|
|
|
|
fs.access(file, fs.constants.R_OK, (err) => {
|
|
|
|
|
console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check if the file is writable.
|
|
|
|
|
fs.access(file, fs.constants.W_OK, (err) => {
|
|
|
|
|
console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check if the file exists in the current directory, and if it is writable.
|
|
|
|
|
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(
|
|
|
|
|
`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`${file} exists, and it is writable`);
|
|
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
```
|
2011-04-02 02:46:18 +02:00
|
|
|
|
|
2016-07-22 09:30:45 +02:00
|
|
|
|
Using `fs.access()` to check for the accessibility 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 is not accessible.
|
|
|
|
|
|
|
|
|
|
**write (NOT RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.access('myfile', (err) => {
|
|
|
|
|
if (!err) {
|
|
|
|
|
console.error('myfile already exists');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.open('myfile', 'wx', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
writeMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**write (RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('myfile', 'wx', (err, fd) => {
|
|
|
|
|
if (err) {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
if (err.code === 'EEXIST') {
|
2016-07-22 09:30:45 +02:00
|
|
|
|
console.error('myfile already exists');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**read (NOT RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.access('myfile', (err) => {
|
|
|
|
|
if (err) {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
if (err.code === 'ENOENT') {
|
2016-07-22 09:30:45 +02:00
|
|
|
|
console.error('myfile does not exist');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.open('myfile', 'r', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
readMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**read (RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('myfile', 'r', (err, fd) => {
|
|
|
|
|
if (err) {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
if (err.code === 'ENOENT') {
|
2016-07-22 09:30:45 +02:00
|
|
|
|
console.error('myfile does not exist');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2017-12-23 00:48:06 +01:00
|
|
|
|
In general, check for the accessibility of a file only if the file will not be
|
2016-07-22 09:30:45 +02:00
|
|
|
|
used directly, for example when its accessibility is a signal from another
|
|
|
|
|
process.
|
|
|
|
|
|
2018-10-20 00:25:34 +02:00
|
|
|
|
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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.accessSync(path[, mode])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
2016-06-14 21:57:22 +02:00
|
|
|
|
added: v0.11.15
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `mode` {integer} **Default:** `fs.constants.F_OK`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-05-06 09:35:21 +02:00
|
|
|
|
Synchronously tests a user's permissions for the file or directory specified
|
|
|
|
|
by `path`. The `mode` argument is an optional integer that specifies the
|
2020-06-14 23:49:34 +02:00
|
|
|
|
accessibility checks to be performed. Check [File access constants][] for
|
2018-05-06 09:35:21 +02:00
|
|
|
|
possible values of `mode`. It is possible to create a mask consisting of
|
|
|
|
|
the bitwise OR of two or more values
|
|
|
|
|
(e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
2017-12-23 00:48:06 +01:00
|
|
|
|
|
|
|
|
|
If any of the accessibility checks fail, an `Error` will be thrown. Otherwise,
|
|
|
|
|
the method will return `undefined`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
try {
|
|
|
|
|
fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
|
|
|
|
|
console.log('can read/write');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('no access!');
|
|
|
|
|
}
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.appendFile(path, data[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.7
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7831
|
|
|
|
|
description: The passed `options` object will never be modified.
|
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
|
|
|
|
description: The `file` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
* `path` {string|Buffer|URL|number} filename or file descriptor
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `data` {string|Buffer}
|
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'a'`.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-23 00:48:06 +01:00
|
|
|
|
Asynchronously append data to a file, creating the file if it does not yet
|
|
|
|
|
exist. `data` can be a string or a [`Buffer`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.appendFile('message.txt', 'data to append', (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('The "data to append" was appended to file!');
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
|
If `options` is a string, then it specifies the encoding:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.appendFile('message.txt', 'data to append', 'utf8', callback);
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
The `path` may be specified as a numeric file descriptor that has been opened
|
2017-12-23 00:48:06 +01:00
|
|
|
|
for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
|
|
|
|
|
not be closed automatically.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-12-23 00:48:06 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.open('message.txt', 'a', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
fs.appendFile(fd, 'data to append', 'utf8', (err) => {
|
|
|
|
|
fs.close(fd, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.appendFileSync(path, data[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.7
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7831
|
|
|
|
|
description: The passed `options` object will never be modified.
|
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
|
|
|
|
description: The `file` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
* `path` {string|Buffer|URL|number} filename or file descriptor
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `data` {string|Buffer}
|
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'a'`.
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
2017-12-23 00:48:06 +01:00
|
|
|
|
Synchronously append data to a file, creating the file if it does not yet
|
|
|
|
|
exist. `data` can be a string or a [`Buffer`][].
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
try {
|
|
|
|
|
fs.appendFileSync('message.txt', 'data to append');
|
|
|
|
|
console.log('The "data to append" was appended to file!');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
/* Handle the error */
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
|
If `options` is a string, then it specifies the encoding:
|
2017-12-23 00:48:06 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.appendFileSync('message.txt', 'data to append', 'utf8');
|
|
|
|
|
```
|
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
The `path` may be specified as a numeric file descriptor that has been opened
|
2017-12-23 00:48:06 +01:00
|
|
|
|
for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
|
|
|
|
|
not be closed automatically.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let fd;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fd = fs.openSync('message.txt', 'a');
|
|
|
|
|
fs.appendFileSync(fd, 'data to append', 'utf8');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
/* Handle the error */
|
|
|
|
|
} finally {
|
|
|
|
|
if (fd !== undefined)
|
|
|
|
|
fs.closeSync(fd);
|
|
|
|
|
}
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.chmod(path, mode, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.30
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-12-27 08:18:31 +01:00
|
|
|
|
* `mode` {string|integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-12-14 12:56:38 +01:00
|
|
|
|
Asynchronously changes the permissions of a file. No arguments other than a
|
|
|
|
|
possible exception are given to the completion callback.
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: chmod(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-06-21 23:56:56 +02:00
|
|
|
|
```js
|
|
|
|
|
fs.chmod('my_file.txt', 0o775, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('The permissions for file "my_file.txt" have been changed!');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2017-12-23 00:48:06 +01:00
|
|
|
|
### File modes
|
|
|
|
|
|
|
|
|
|
The `mode` argument used in both the `fs.chmod()` and `fs.chmodSync()`
|
|
|
|
|
methods is a numeric bitmask created using a logical OR of the following
|
|
|
|
|
constants:
|
|
|
|
|
|
|
|
|
|
| Constant | Octal | Description |
|
|
|
|
|
| ---------------------- | ------- | ------------------------ |
|
|
|
|
|
| `fs.constants.S_IRUSR` | `0o400` | read by owner |
|
|
|
|
|
| `fs.constants.S_IWUSR` | `0o200` | write by owner |
|
|
|
|
|
| `fs.constants.S_IXUSR` | `0o100` | execute/search by owner |
|
|
|
|
|
| `fs.constants.S_IRGRP` | `0o40` | read by group |
|
|
|
|
|
| `fs.constants.S_IWGRP` | `0o20` | write by group |
|
|
|
|
|
| `fs.constants.S_IXGRP` | `0o10` | execute/search by group |
|
|
|
|
|
| `fs.constants.S_IROTH` | `0o4` | read by others |
|
|
|
|
|
| `fs.constants.S_IWOTH` | `0o2` | write by others |
|
|
|
|
|
| `fs.constants.S_IXOTH` | `0o1` | execute/search by others |
|
|
|
|
|
|
|
|
|
|
An easier method of constructing the `mode` is to use a sequence of three
|
|
|
|
|
octal digits (e.g. `765`). The left-most digit (`7` in the example), specifies
|
|
|
|
|
the permissions for the file owner. The middle digit (`6` in the example),
|
|
|
|
|
specifies permissions for the group. The right-most digit (`5` in the example),
|
|
|
|
|
specifies the permissions for others.
|
|
|
|
|
|
|
|
|
|
| Number | Description |
|
|
|
|
|
| ------- | ------------------------ |
|
|
|
|
|
| `7` | read, write, and execute |
|
|
|
|
|
| `6` | read and write |
|
|
|
|
|
| `5` | read and execute |
|
|
|
|
|
| `4` | read only |
|
|
|
|
|
| `3` | write and execute |
|
|
|
|
|
| `2` | write only |
|
|
|
|
|
| `1` | execute only |
|
|
|
|
|
| `0` | no permission |
|
|
|
|
|
|
|
|
|
|
For example, the octal value `0o765` means:
|
|
|
|
|
|
|
|
|
|
* The owner may read, write and execute the file.
|
|
|
|
|
* The group may read and write the file.
|
|
|
|
|
* Others may read and execute the file.
|
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
When using raw numbers where file modes are expected, any value larger than
|
|
|
|
|
`0o777` may result in platform-specific behaviors that are not supported to work
|
|
|
|
|
consistently. Therefore constants like `S_ISVTX`, `S_ISGID` or `S_ISUID` are not
|
|
|
|
|
exposed in `fs.constants`.
|
2018-05-26 12:51:19 +02:00
|
|
|
|
|
2018-05-09 19:26:42 +02:00
|
|
|
|
Caveats: on Windows only the write permission can be changed, and the
|
|
|
|
|
distinction among the permissions of group, owner or others is not
|
|
|
|
|
implemented.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.chmodSync(path, mode)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.7
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-12-27 08:18:31 +01:00
|
|
|
|
* `mode` {string|integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.chmod()`][].
|
2017-12-14 12:56:38 +01:00
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: chmod(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.chown(path, uid, gid, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.97
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-12-14 12:56:38 +01:00
|
|
|
|
Asynchronously changes owner and group of a file. No arguments other than a
|
|
|
|
|
possible exception are given to the completion callback.
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: chown(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.chownSync(path, uid, gid)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.97
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-12-14 12:56:38 +01:00
|
|
|
|
Synchronously changes owner and group of a file. Returns `undefined`.
|
|
|
|
|
This is the synchronous version of [`fs.chown()`][].
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: chown(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.close(fd, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
|
Asynchronous close(2). No arguments other than a possible exception are given
|
2015-11-04 18:07:07 +01:00
|
|
|
|
to the completion callback.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-14 20:47:01 +01:00
|
|
|
|
Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
|
|
|
|
|
through any other `fs` operation may lead to undefined behavior.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.closeSync(fd)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous close(2). Returns `undefined`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-14 20:47:01 +01:00
|
|
|
|
Calling `fs.closeSync()` on any file descriptor (`fd`) that is currently in use
|
|
|
|
|
through any other `fs` operation may lead to undefined behavior.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.constants`
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
|
Returns an object containing commonly used constants for file system
|
|
|
|
|
operations. The specific constants currently defined are described in
|
2020-06-14 23:49:34 +02:00
|
|
|
|
[FS constants][].
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
## `fs.copyFile(src, dest[, mode], callback)`
|
2017-09-06 18:54:29 +02:00
|
|
|
|
<!-- YAML
|
2017-09-10 04:58:50 +02:00
|
|
|
|
added: v8.5.0
|
2017-09-06 18:54:29 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `src` {string|Buffer|URL} source filename to copy
|
|
|
|
|
* `dest` {string|Buffer|URL} destination filename of the copy operation
|
2019-09-26 00:34:05 +02:00
|
|
|
|
* `mode` {integer} modifiers for copy operation. **Default:** `0`.
|
2017-09-06 18:54:29 +02:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`mode` is an optional integer that specifies the behavior
|
2018-04-02 21:12:57 +02:00
|
|
|
|
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`).
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
|
2018-04-02 21:12:57 +02:00
|
|
|
|
exists.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
|
2018-04-02 21:12:57 +02:00
|
|
|
|
copy-on-write reflink. If the platform does not support copy-on-write, then a
|
|
|
|
|
fallback copy mechanism is used.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
|
2018-04-02 21:12:57 +02:00
|
|
|
|
create a copy-on-write reflink. If the platform does not support copy-on-write,
|
|
|
|
|
then the operation will fail.
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
2019-09-26 14:13:49 +02:00
|
|
|
|
const { COPYFILE_EXCL } = fs.constants;
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
2019-09-26 14:13:49 +02:00
|
|
|
|
function callback(err) {
|
2017-09-06 18:54:29 +02:00
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('source.txt was copied to destination.txt');
|
2019-09-26 14:13:49 +02:00
|
|
|
|
}
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
2019-09-26 14:13:49 +02:00
|
|
|
|
// destination.txt will be created or overwritten by default.
|
|
|
|
|
fs.copyFile('source.txt', 'destination.txt', callback);
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
|
|
|
|
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
|
|
|
|
|
fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback);
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
## `fs.copyFileSync(src, dest[, mode])`
|
2017-09-06 18:54:29 +02:00
|
|
|
|
<!-- YAML
|
2017-09-10 04:58:50 +02:00
|
|
|
|
added: v8.5.0
|
2017-09-06 18:54:29 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `src` {string|Buffer|URL} source filename to copy
|
|
|
|
|
* `dest` {string|Buffer|URL} destination filename of the copy operation
|
2019-09-26 00:34:05 +02:00
|
|
|
|
* `mode` {integer} modifiers for copy operation. **Default:** `0`.
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`mode` is an optional integer that specifies the behavior
|
2018-04-02 21:12:57 +02:00
|
|
|
|
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`).
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
|
2018-04-02 21:12:57 +02:00
|
|
|
|
exists.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
|
2018-04-02 21:12:57 +02:00
|
|
|
|
copy-on-write reflink. If the platform does not support copy-on-write, then a
|
|
|
|
|
fallback copy mechanism is used.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
|
2018-04-02 21:12:57 +02:00
|
|
|
|
create a copy-on-write reflink. If the platform does not support copy-on-write,
|
|
|
|
|
then the operation will fail.
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
2019-09-26 14:13:49 +02:00
|
|
|
|
const { COPYFILE_EXCL } = fs.constants;
|
2017-09-06 18:54:29 +02:00
|
|
|
|
|
|
|
|
|
// destination.txt will be created or overwritten by default.
|
|
|
|
|
fs.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.
|
|
|
|
|
fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.createReadStream(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.6.0
|
|
|
|
|
- v12.17.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29083
|
|
|
|
|
description: The `fs` options allow overriding the used `fs`
|
|
|
|
|
implementation.
|
2019-09-04 00:10:04 +02:00
|
|
|
|
- version: v12.10.0
|
2019-08-22 07:13:56 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29212
|
|
|
|
|
description: Enable `emitClose` option.
|
2018-10-03 01:01:19 +02:00
|
|
|
|
- version: v11.0.0
|
2018-04-09 19:32:08 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/19898
|
|
|
|
|
description: Impose new restrictions on `start` and `end`, throwing
|
|
|
|
|
more appropriate errors in cases when we cannot reasonably
|
|
|
|
|
handle the input values.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7831
|
|
|
|
|
description: The passed `options` object will never be modified.
|
|
|
|
|
- version: v2.3.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/1845
|
|
|
|
|
description: The passed `options` object can be a string now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flags` {string} See [support of file system `flags`][]. **Default:**
|
|
|
|
|
`'r'`.
|
2018-04-15 18:32:44 +02:00
|
|
|
|
* `encoding` {string} **Default:** `null`
|
|
|
|
|
* `fd` {integer} **Default:** `null`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
|
|
|
|
* `autoClose` {boolean} **Default:** `true`
|
2019-08-22 07:13:56 +02:00
|
|
|
|
* `emitClose` {boolean} **Default:** `false`
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `start` {integer}
|
2018-04-15 18:32:44 +02:00
|
|
|
|
* `end` {integer} **Default:** `Infinity`
|
|
|
|
|
* `highWaterMark` {integer} **Default:** `64 * 1024`
|
2019-08-11 15:29:30 +02:00
|
|
|
|
* `fs` {Object|null} **Default:** `null`
|
|
|
|
|
* Returns: {fs.ReadStream} See [Readable Stream][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-05-01 06:50:35 +02:00
|
|
|
|
Unlike the 16 kb default `highWaterMark` for a readable stream, the stream
|
|
|
|
|
returned by this method has a default `highWaterMark` of 64 kb.
|
2012-04-16 23:52:44 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`options` can include `start` and `end` values to read a range of bytes from
|
2018-04-02 07:38:48 +02:00
|
|
|
|
the file instead of the entire file. Both `start` and `end` are inclusive and
|
2019-02-10 05:16:53 +01:00
|
|
|
|
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`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
|
2016-06-02 19:39:39 +02:00
|
|
|
|
the specified file descriptor. This means that no `'open'` event will be
|
2018-07-04 01:51:28 +02:00
|
|
|
|
emitted. `fd` should be blocking; non-blocking `fd`s should be passed to
|
|
|
|
|
[`net.Socket`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-08-28 15:56:30 +02:00
|
|
|
|
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.
|
2018-06-08 16:23:25 +02:00
|
|
|
|
|
2019-08-22 07:13:56 +02:00
|
|
|
|
By default, the stream will not emit a `'close'` event after it has been
|
|
|
|
|
destroyed. This is the opposite of the default for other `Readable` streams.
|
|
|
|
|
Set the `emitClose` option to `true` to change this behavior.
|
|
|
|
|
|
2020-03-08 02:35:00 +01:00
|
|
|
|
By providing the `fs` option, it is possible to override the corresponding `fs`
|
|
|
|
|
implementations for `open`, `read`, and `close`. When providing the `fs` option,
|
|
|
|
|
overrides for `open`, `read`, and `close` are required.
|
2019-08-11 15:29:30 +02:00
|
|
|
|
|
2018-06-08 16:23:25 +02:00
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
2018-11-24 09:42:09 +01:00
|
|
|
|
// Create a stream from some character device.
|
2018-06-08 16:23:25 +02:00
|
|
|
|
const stream = fs.createReadStream('/dev/input/event0');
|
|
|
|
|
setTimeout(() => {
|
2018-08-28 15:56:30 +02:00
|
|
|
|
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.
|
2018-06-08 16:23:25 +02:00
|
|
|
|
stream.push(null);
|
2018-08-28 15:56:30 +02:00
|
|
|
|
stream.read(0);
|
2018-06-08 16:23:25 +02:00
|
|
|
|
}, 100);
|
|
|
|
|
```
|
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
If `autoClose` is false, then the file descriptor won't be closed, even if
|
2017-04-26 19:16:12 +02:00
|
|
|
|
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
|
2018-04-09 18:30:22 +02:00
|
|
|
|
behavior), on `'error'` or `'end'` the file descriptor will be closed
|
2015-11-04 18:07:07 +01:00
|
|
|
|
automatically.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`mode` sets the file mode (permission and sticky bits), but only if the
|
|
|
|
|
file was created.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
An example to read the last 10 bytes of a file which is 100 bytes long:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2017-06-01 01:07:25 +02:00
|
|
|
|
fs.createReadStream('sample.txt', { start: 90, end: 99 });
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.createWriteStream(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.6.0
|
|
|
|
|
- v12.17.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29083
|
|
|
|
|
description: The `fs` options allow overriding the used `fs`
|
|
|
|
|
implementation.
|
2019-09-04 00:10:04 +02:00
|
|
|
|
- version: v12.10.0
|
2019-08-22 07:13:56 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29212
|
|
|
|
|
description: Enable `emitClose` option.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7831
|
|
|
|
|
description: The passed `options` object will never be modified.
|
|
|
|
|
- version: v5.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3679
|
|
|
|
|
description: The `autoClose` option is supported now.
|
|
|
|
|
- version: v2.3.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/1845
|
|
|
|
|
description: The passed `options` object can be a string now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flags` {string} See [support of file system `flags`][]. **Default:**
|
|
|
|
|
`'w'`.
|
2018-04-15 18:32:44 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* `fd` {integer} **Default:** `null`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
|
|
|
|
* `autoClose` {boolean} **Default:** `true`
|
2019-08-22 07:13:56 +02:00
|
|
|
|
* `emitClose` {boolean} **Default:** `false`
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `start` {integer}
|
2019-08-11 15:29:30 +02:00
|
|
|
|
* `fs` {Object|null} **Default:** `null`
|
|
|
|
|
* Returns: {fs.WriteStream} See [Writable Stream][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`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`][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
|
If `autoClose` is set to true (default behavior) on `'error'` or `'finish'`
|
2015-11-29 16:11:19 +01:00
|
|
|
|
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.
|
2017-04-26 19:16:12 +02:00
|
|
|
|
It is the application's responsibility to close it and make sure there's no
|
|
|
|
|
file descriptor leak.
|
2015-11-29 16:11:19 +01:00
|
|
|
|
|
2019-08-22 07:13:56 +02:00
|
|
|
|
By default, the stream will not emit a `'close'` event after it has been
|
|
|
|
|
destroyed. This is the opposite of the default for other `Writable` streams.
|
|
|
|
|
Set the `emitClose` option to `true` to change this behavior.
|
|
|
|
|
|
2019-08-11 15:29:30 +02:00
|
|
|
|
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()`)
|
2020-03-08 02:35:00 +01:00
|
|
|
|
will be disabled. When providing the `fs` option, overrides for `open`,
|
|
|
|
|
`close`, and at least one of `write` and `writev` are required.
|
2019-08-11 15:29:30 +02:00
|
|
|
|
|
2018-04-15 18:32:44 +02:00
|
|
|
|
Like [`ReadStream`][], if `fd` is specified, [`WriteStream`][] will ignore the
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`path` argument and will use the specified file descriptor. This means that no
|
2018-07-04 01:51:28 +02:00
|
|
|
|
`'open'` event will be emitted. `fd` should be blocking; non-blocking `fd`s
|
|
|
|
|
should be passed to [`net.Socket`][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.exists(path, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
deprecated: v1.0.0
|
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
|
> Stability: 0 - Deprecated: Use [`fs.stat()`][] or [`fs.access()`][] instead.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2018-01-12 00:28:02 +01:00
|
|
|
|
* `exists` {boolean}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Test whether or not the given path exists by checking with the file system.
|
2018-08-26 18:02:27 +02:00
|
|
|
|
Then call the `callback` argument with either true or false:
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.exists('/etc/passwd', (exists) => {
|
|
|
|
|
console.log(exists ? 'it\'s there' : 'no passwd!');
|
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
**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()`.
|
2016-09-01 01:10:04 +02:00
|
|
|
|
|
2016-07-22 09:30:45 +02:00
|
|
|
|
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
|
|
|
|
|
fs.exists('myfile', (exists) => {
|
|
|
|
|
if (exists) {
|
|
|
|
|
console.error('myfile already exists');
|
|
|
|
|
} else {
|
|
|
|
|
fs.open('myfile', 'wx', (err, fd) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
writeMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**write (RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('myfile', 'wx', (err, fd) => {
|
|
|
|
|
if (err) {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
if (err.code === 'EEXIST') {
|
2016-07-22 09:30:45 +02:00
|
|
|
|
console.error('myfile already exists');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
2016-07-22 09:30:45 +02:00
|
|
|
|
writeMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**read (NOT RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.exists('myfile', (exists) => {
|
|
|
|
|
if (exists) {
|
|
|
|
|
fs.open('myfile', 'r', (err, fd) => {
|
2018-02-09 17:20:20 +01:00
|
|
|
|
if (err) throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
readMyData(fd);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.error('myfile does not exist');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**read (RECOMMENDED)**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.open('myfile', 'r', (err, fd) => {
|
|
|
|
|
if (err) {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
if (err.code === 'ENOENT') {
|
2016-07-22 09:30:45 +02:00
|
|
|
|
console.error('myfile does not exist');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
throw err;
|
2016-07-22 09:30:45 +02:00
|
|
|
|
}
|
2017-03-25 14:33:03 +01:00
|
|
|
|
|
|
|
|
|
readMyData(fd);
|
2016-07-22 09:30:45 +02:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
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.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.existsSync(path)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {boolean}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-12-15 22:50:08 +01:00
|
|
|
|
Returns `true` if the path exists, `false` otherwise.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.exists()`][].
|
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
`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.
|
2016-09-01 01:10:04 +02:00
|
|
|
|
|
2019-06-21 23:46:14 +02:00
|
|
|
|
```js
|
|
|
|
|
if (fs.existsSync('/etc/passwd')) {
|
2020-02-03 02:22:17 +01:00
|
|
|
|
console.log('The path exists.');
|
2019-06-21 23:46:14 +02:00
|
|
|
|
}
|
|
|
|
|
```
|
2018-06-10 15:28:00 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fchmod(fd, mode, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.7
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2019-12-27 16:12:35 +01:00
|
|
|
|
* `mode` {string|integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous fchmod(2). No arguments other than a possible exception
|
|
|
|
|
are given to the completion callback.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fchmodSync(fd, mode)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.7
|
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2019-12-27 16:12:35 +01:00
|
|
|
|
* `mode` {string|integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous fchmod(2). Returns `undefined`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fchown(fd, uid, gid, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.7
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous fchown(2). No arguments other than a possible exception are given
|
2010-11-17 17:04:21 +01:00
|
|
|
|
to the completion callback.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fchownSync(fd, uid, gid)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.7
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous fchown(2). Returns `undefined`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fdatasync(fd, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.96
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2016-02-24 04:41:05 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2016-02-24 04:41:05 +01:00
|
|
|
|
Asynchronous fdatasync(2). No arguments other than a possible exception are
|
|
|
|
|
given to the completion callback.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fdatasyncSync(fd)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.96
|
|
|
|
|
-->
|
2016-02-24 04:41:05 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2016-02-24 04:41:05 +01:00
|
|
|
|
Synchronous fdatasync(2). Returns `undefined`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fstat(fd[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.95
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `stats` {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous fstat(2). The callback gets two arguments `(err, stats)` where
|
2016-10-29 01:49:41 +02:00
|
|
|
|
`stats` is an [`fs.Stats`][] object. `fstat()` is identical to [`stat()`][],
|
2016-04-30 03:09:34 +02:00
|
|
|
|
except that the file to be stat-ed is specified by the file descriptor `fd`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fstatSync(fd[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.95
|
2018-04-23 11:14:56 +02:00
|
|
|
|
changes:
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-15 18:32:44 +02:00
|
|
|
|
Synchronous fstat(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fsync(fd, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.96
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous fsync(2). No arguments other than a possible exception are given
|
|
|
|
|
to the completion callback.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.fsyncSync(fd)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.96
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous fsync(2). Returns `undefined`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.ftruncate(fd[, len], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.6
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous ftruncate(2). No arguments other than a possible exception are
|
|
|
|
|
given to the completion callback.
|
|
|
|
|
|
2016-08-27 12:23:14 +02:00
|
|
|
|
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.
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
For example, the following program retains only the first four bytes of the
|
|
|
|
|
file:
|
2016-08-27 12:23:14 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8'));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: Node.js
|
2016-08-27 12:23:14 +02:00
|
|
|
|
|
|
|
|
|
// get the file descriptor of the file to be truncated
|
|
|
|
|
const fd = fs.openSync('temp.txt', 'r+');
|
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Truncate the file to first four bytes
|
2016-08-27 12:23:14 +02:00
|
|
|
|
fs.ftruncate(fd, 4, (err) => {
|
|
|
|
|
assert.ifError(err);
|
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8'));
|
|
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: Node
|
2016-08-27 12:23:14 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
If the file previously was shorter than `len` bytes, it is extended, and the
|
2018-08-26 18:02:27 +02:00
|
|
|
|
extended part is filled with null bytes (`'\0'`):
|
2016-08-27 12:23:14 +02:00
|
|
|
|
|
|
|
|
|
```js
|
2017-10-21 22:13:09 +02:00
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8'));
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: Node.js
|
2016-08-27 12:23:14 +02:00
|
|
|
|
|
|
|
|
|
// get the file descriptor of the file to be truncated
|
|
|
|
|
const fd = fs.openSync('temp.txt', 'r+');
|
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
|
// Truncate the file to 10 bytes, whereas the actual size is 7 bytes
|
2016-08-27 12:23:14 +02:00
|
|
|
|
fs.ftruncate(fd, 10, (err) => {
|
2017-03-25 14:33:03 +01:00
|
|
|
|
assert.ifError(err);
|
2016-08-27 12:23:14 +02:00
|
|
|
|
console.log(fs.readFileSync('temp.txt'));
|
|
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Prints: <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
|
|
|
|
|
// ('Node.js\0\0\0' in UTF8)
|
2016-08-27 12:23:14 +02:00
|
|
|
|
```
|
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
|
The last three bytes are null bytes (`'\0'`), to compensate the over-truncation.
|
2016-08-27 12:23:14 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.ftruncateSync(fd[, len])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.6
|
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns `undefined`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.ftruncate()`][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.futimes(fd, atime, mtime, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v4.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2387
|
|
|
|
|
description: Numeric strings, `NaN` and `Infinity` are now allowed
|
|
|
|
|
time specifiers.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2017-07-10 16:53:50 +02:00
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-07-10 16:53:50 +02:00
|
|
|
|
Change the file system timestamps of the object referenced by the supplied file
|
|
|
|
|
descriptor. See [`fs.utimes()`][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
This function does not work on AIX versions before 7.1, it will return the
|
|
|
|
|
error `UV_ENOSYS`.
|
2017-06-13 15:47:37 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.futimesSync(fd, atime, mtime)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v4.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2387
|
|
|
|
|
description: Numeric strings, `NaN` and `Infinity` are now allowed
|
|
|
|
|
time specifiers.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2019-09-23 12:25:15 +02:00
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lchmod(path, mode, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
deprecated: v0.4.7
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `mode` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous lchmod(2). No arguments other than a possible exception
|
|
|
|
|
are given to the completion callback.
|
|
|
|
|
|
2017-03-29 01:46:10 +02:00
|
|
|
|
Only available on macOS.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lchmodSync(path, mode)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
deprecated: v0.4.7
|
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `mode` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous lchmod(2). Returns `undefined`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lchown(path, uid, gid, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-07-03 09:44:13 +02:00
|
|
|
|
- version: v10.6.0
|
2018-06-23 22:26:29 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21498
|
|
|
|
|
description: This API is no longer deprecated.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous lchown(2). No arguments other than a possible exception are given
|
2010-11-17 17:04:21 +01:00
|
|
|
|
to the completion callback.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lchownSync(path, uid, gid)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
2018-06-23 22:26:29 +02:00
|
|
|
|
changes:
|
2018-07-03 09:44:13 +02:00
|
|
|
|
- version: v10.6.0
|
2018-06-23 22:26:29 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21498
|
|
|
|
|
description: This API is no longer deprecated.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous lchown(2). Returns `undefined`.
|
|
|
|
|
|
2020-05-14 11:54:00 +02:00
|
|
|
|
## `fs.lutimes(path, atime, mtime, callback)`
|
|
|
|
|
<!-- YAML
|
|
|
|
|
addded: REPLACEME
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
|
|
|
|
|
Changes the access and modification times of a file in the same way as
|
|
|
|
|
[`fs.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.
|
|
|
|
|
|
|
|
|
|
## `fs.lutimesSync(path, atime, mtime)`
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: REPLACEME
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
|
|
|
|
|
|
|
|
|
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 [`fs.lutimes()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.link(existingPath, newPath, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `existingPath` and `newPath` parameters can be WHATWG
|
|
|
|
|
`URL` objects using `file:` protocol. Support is currently
|
|
|
|
|
still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `existingPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous link(2). No arguments other than a possible exception are given to
|
|
|
|
|
the completion callback.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.linkSync(existingPath, newPath)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `existingPath` and `newPath` parameters can be WHATWG
|
|
|
|
|
`URL` objects using `file:` protocol. Support is currently
|
|
|
|
|
still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `existingPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous link(2). Returns `undefined`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lstat(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.30
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `stats` {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous lstat(2). The callback gets two arguments `(err, stats)` where
|
2016-04-30 03:09:34 +02:00
|
|
|
|
`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.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.lstatSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.30
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-15 18:32:44 +02:00
|
|
|
|
Synchronous lstat(2).
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.mkdir(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.8
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.11.0
|
|
|
|
|
- v12.17.0
|
2020-03-25 21:02:46 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31530
|
|
|
|
|
description: In `recursive` mode, the callback now receives the first
|
|
|
|
|
created path as an argument.
|
2018-10-07 14:09:45 +02:00
|
|
|
|
- version: v10.12.0
|
2018-09-19 17:13:01 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21875
|
|
|
|
|
description: The second argument can now be an `options` object with
|
|
|
|
|
`recursive` and `mode` properties.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-08-10 01:52:41 +02:00
|
|
|
|
* `options` {Object|integer}
|
|
|
|
|
* `recursive` {boolean} **Default:** `false`
|
2019-12-27 16:20:31 +01:00
|
|
|
|
* `mode` {string|integer} Not supported on Windows. **Default:** `0o777`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2020-01-29 08:06:44 +01:00
|
|
|
|
Asynchronously creates a directory.
|
|
|
|
|
|
|
|
|
|
The callback is given a possible exception and, if `recursive` is `true`, the
|
2020-04-18 22:02:54 +02:00
|
|
|
|
first directory path created, `(err, [path])`.
|
2017-12-14 12:56:38 +01:00
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
The optional `options` argument can be an integer specifying `mode` (permission
|
2018-08-10 01:52:41 +02:00
|
|
|
|
and sticky bits), or an object with a `mode` property and a `recursive`
|
2020-04-18 22:02:54 +02:00
|
|
|
|
property indicating whether parent directories should be created. Calling
|
2019-05-01 01:12:46 +02:00
|
|
|
|
`fs.mkdir()` when `path` is a directory that exists results in an error only
|
|
|
|
|
when `recursive` is false.
|
2018-08-10 01:52:41 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
|
|
|
|
|
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-01-04 01:26:10 +01:00
|
|
|
|
On Windows, using `fs.mkdir()` on the root directory even with recursion will
|
|
|
|
|
result in an error:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.mkdir('/', { recursive: true }, (err) => {
|
|
|
|
|
// => [Error: EPERM: operation not permitted, mkdir 'C:\']
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: mkdir(2).
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.mkdirSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.11.0
|
|
|
|
|
- v12.17.0
|
2020-03-25 21:02:46 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31530
|
|
|
|
|
description: In `recursive` mode, the first created path is returned now.
|
2018-10-07 14:09:45 +02:00
|
|
|
|
- version: v10.12.0
|
2018-09-19 17:13:01 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21875
|
|
|
|
|
description: The second argument can now be an `options` object with
|
|
|
|
|
`recursive` and `mode` properties.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-08-10 01:52:41 +02:00
|
|
|
|
* `options` {Object|integer}
|
|
|
|
|
* `recursive` {boolean} **Default:** `false`
|
2019-12-27 16:20:31 +01:00
|
|
|
|
* `mode` {string|integer} Not supported on Windows. **Default:** `0o777`.
|
2020-01-29 08:06:44 +01:00
|
|
|
|
* Returns: {string|undefined}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2020-01-29 08:06:44 +01:00
|
|
|
|
Synchronously creates a directory. Returns `undefined`, or if `recursive` is
|
2020-04-18 22:02:54 +02:00
|
|
|
|
`true`, the first directory path created.
|
2017-12-14 12:56:38 +01:00
|
|
|
|
This is the synchronous version of [`fs.mkdir()`][].
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: mkdir(2).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.mkdtemp(prefix[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v5.10.0
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v6.2.1
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6828
|
|
|
|
|
description: The `callback` parameter is optional now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2016-02-24 22:17:44 +01:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `prefix` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2016-07-22 20:33:24 +02:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2020-04-18 22:02:54 +02:00
|
|
|
|
* `directory` {string}
|
2016-07-22 20:33:24 +02:00
|
|
|
|
|
2016-02-24 22:17:44 +01:00
|
|
|
|
Creates a unique temporary directory.
|
|
|
|
|
|
|
|
|
|
Generates six random characters to be appended behind a required
|
2019-03-27 15:38:57 +01:00
|
|
|
|
`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.
|
2016-02-24 22:17:44 +01:00
|
|
|
|
|
2020-04-18 22:02:54 +02:00
|
|
|
|
The created directory path is passed as a string to the callback's second
|
2016-02-24 22:17:44 +01:00
|
|
|
|
parameter.
|
|
|
|
|
|
2016-07-22 20:33:24 +02:00
|
|
|
|
The optional `options` argument can be a string specifying an encoding, or an
|
|
|
|
|
object with an `encoding` property specifying the character encoding to use.
|
|
|
|
|
|
2016-02-24 22:17:44 +01:00
|
|
|
|
```js
|
2020-04-18 22:02:54 +02:00
|
|
|
|
fs.mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
|
2016-07-22 20:33:24 +02:00
|
|
|
|
if (err) throw err;
|
2020-04-18 22:02:54 +02:00
|
|
|
|
console.log(directory);
|
2017-09-14 14:26:42 +02:00
|
|
|
|
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
|
2016-02-24 22:17:44 +01:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
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`
|
2018-05-04 06:51:03 +02:00
|
|
|
|
must end with a trailing platform-specific path separator
|
2016-05-17 06:40:17 +02:00
|
|
|
|
(`require('path').sep`).
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// The parent directory for the new temporary directory
|
2017-09-14 14:26:42 +02:00
|
|
|
|
const tmpDir = os.tmpdir();
|
2016-05-17 06:40:17 +02:00
|
|
|
|
|
|
|
|
|
// This method is *INCORRECT*:
|
2020-04-18 22:02:54 +02:00
|
|
|
|
fs.mkdtemp(tmpDir, (err, directory) => {
|
2016-05-17 06:40:17 +02:00
|
|
|
|
if (err) throw err;
|
2020-04-18 22:02:54 +02:00
|
|
|
|
console.log(directory);
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Will print something similar to `/tmpabc123`.
|
2018-07-04 01:51:28 +02:00
|
|
|
|
// A new temporary directory is created at the file system root
|
|
|
|
|
// rather than *within* the /tmp directory.
|
2016-05-17 06:40:17 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// This method is *CORRECT*:
|
2017-03-25 14:33:03 +01:00
|
|
|
|
const { sep } = require('path');
|
2020-04-18 22:02:54 +02:00
|
|
|
|
fs.mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
|
2016-05-17 06:40:17 +02:00
|
|
|
|
if (err) throw err;
|
2020-04-18 22:02:54 +02:00
|
|
|
|
console.log(directory);
|
2016-11-08 21:04:57 +01:00
|
|
|
|
// Will print something similar to `/tmp/abc123`.
|
|
|
|
|
// A new temporary directory is created within
|
|
|
|
|
// the /tmp directory.
|
2016-05-17 06:40:17 +02:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.mkdtempSync(prefix[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v5.10.0
|
|
|
|
|
-->
|
2016-02-24 22:17:44 +01:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `prefix` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {string}
|
2016-07-22 20:33:24 +02:00
|
|
|
|
|
2020-04-18 22:02:54 +02:00
|
|
|
|
Returns the created directory path.
|
2018-06-10 15:28:00 +02:00
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.mkdtemp()`][].
|
2016-02-24 22:17:44 +01:00
|
|
|
|
|
2016-07-22 20:33:24 +02:00
|
|
|
|
The optional `options` argument can be a string specifying an encoding, or an
|
|
|
|
|
object with an `encoding` property specifying the character encoding to use.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.open(path[, flags[, mode]], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
2018-11-08 01:45:33 +01:00
|
|
|
|
- version: v11.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23767
|
|
|
|
|
description: The `flags` argument is now optional and defaults to `'r'`.
|
2018-03-25 03:51:46 +02:00
|
|
|
|
- version: v9.9.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/18801
|
2019-09-26 00:34:05 +02:00
|
|
|
|
description: The `as` and `as+` flags are supported now.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flags` {string|number} See [support of file system `flags`][].
|
2018-10-23 22:26:57 +02:00
|
|
|
|
**Default:** `'r'`.
|
2019-12-27 16:16:58 +01:00
|
|
|
|
* `mode` {string|integer} **Default:** `0o666` (readable and writable)
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `fd` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-15 03:50:48 +02:00
|
|
|
|
Asynchronous file open. See open(2).
|
2018-02-15 15:13:26 +01:00
|
|
|
|
|
2013-08-02 21:41:24 +02:00
|
|
|
|
`mode` sets the file mode (permission and sticky bits), but only if the file was
|
2018-07-04 01:51:28 +02:00
|
|
|
|
created. On Windows, only the write permission can be manipulated; see
|
|
|
|
|
[`fs.chmod()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2013-08-02 21:41:24 +02:00
|
|
|
|
The callback gets two arguments `(err, fd)`.
|
|
|
|
|
|
2017-07-04 12:53:27 +02:00
|
|
|
|
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][MSDN-Using-Streams].
|
|
|
|
|
|
2018-05-30 15:24:19 +02:00
|
|
|
|
Functions based on `fs.open()` exhibit this behavior as well:
|
2017-07-04 12:53:27 +02:00
|
|
|
|
`fs.writeFile()`, `fs.readFile()`, etc.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.opendir(path[, options], callback)`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.1.0
|
|
|
|
|
- v12.16.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30114
|
|
|
|
|
description: The `bufferSize` option was introduced.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
2019-10-25 16:17:07 +02:00
|
|
|
|
* `bufferSize` {number} Number of directory entries that are buffered
|
|
|
|
|
internally when reading from the directory. Higher values lead to better
|
|
|
|
|
performance but higher memory usage. **Default:** `32`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `dir` {fs.Dir}
|
|
|
|
|
|
|
|
|
|
Asynchronously 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
|
2019-10-09 21:10:06 +02:00
|
|
|
|
directory and subsequent read operations.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.opendirSync(path[, options])`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.1.0
|
|
|
|
|
- v12.16.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30114
|
|
|
|
|
description: The `bufferSize` option was introduced.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
2019-10-25 16:17:07 +02:00
|
|
|
|
* `bufferSize` {number} Number of directory entries that are buffered
|
|
|
|
|
internally when reading from the directory. Higher values lead to better
|
|
|
|
|
performance but higher memory usage. **Default:** `32`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
* Returns: {fs.Dir}
|
|
|
|
|
|
|
|
|
|
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
|
2019-10-09 21:10:06 +02:00
|
|
|
|
directory and subsequent read operations.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.openSync(path[, flags, mode])`
|
2019-10-09 15:10:19 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
|
|
|
|
changes:
|
|
|
|
|
- version: v11.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23767
|
|
|
|
|
description: The `flags` argument is now optional and defaults to `'r'`.
|
|
|
|
|
- version: v9.9.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/18801
|
2019-09-26 00:34:05 +02:00
|
|
|
|
description: The `as` and `as+` flags are supported now.
|
2019-10-09 15:10:19 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `flags` {string|number} **Default:** `'r'`.
|
|
|
|
|
See [support of file system `flags`][].
|
2019-12-27 16:16:58 +01:00
|
|
|
|
* `mode` {string|integer} **Default:** `0o666`
|
2019-10-09 15:10:19 +02:00
|
|
|
|
* Returns: {number}
|
|
|
|
|
|
|
|
|
|
Returns an integer representing the file descriptor.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.open()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.read(fd, buffer, offset, length, position, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `buffer` parameter can now be any `TypedArray`, or a
|
|
|
|
|
`DataView`.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10382
|
|
|
|
|
description: The `buffer` parameter can now be a `Uint8Array`.
|
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4518
|
|
|
|
|
description: The `length` parameter can now be `0`.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `bytesRead` {integer}
|
|
|
|
|
* `buffer` {Buffer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Read data from the file specified by `fd`.
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
2019-08-22 16:48:57 +02:00
|
|
|
|
`buffer` is the buffer that the data (read from the fd) will be written to.
|
2015-08-15 18:30:31 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`offset` is the offset in the buffer to start writing at.
|
2015-08-15 18:30:31 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`length` is an integer specifying the number of bytes to read.
|
2015-03-08 03:52:18 +01:00
|
|
|
|
|
2017-08-04 18:44:11 +02:00
|
|
|
|
`position` is an argument specifying where to begin reading from in the file.
|
|
|
|
|
If `position` is `null`, data will be read from the current file position,
|
|
|
|
|
and the file position will be updated.
|
2017-08-03 19:37:09 +02:00
|
|
|
|
If `position` is an integer, the file position will remain unchanged.
|
2015-03-08 03:52:18 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The callback is given the three arguments, `(err, bytesRead, buffer)`.
|
2015-03-08 03:52:18 +01:00
|
|
|
|
|
2017-04-16 21:19:36 +02:00
|
|
|
|
If this method is invoked as its [`util.promisify()`][]ed version, it returns
|
2018-04-29 19:46:41 +02:00
|
|
|
|
a `Promise` for an `Object` with `bytesRead` and `buffer` properties.
|
2017-04-16 21:19:36 +02:00
|
|
|
|
|
2020-01-16 15:46:28 +01:00
|
|
|
|
## `fs.read(fd, [options,] callback)`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.11.0
|
|
|
|
|
- v12.17.0
|
2020-01-16 15:46:28 +01:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.11.0
|
|
|
|
|
- v12.17.0
|
2020-01-16 15:46:28 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31402
|
|
|
|
|
description: Options object can be passed in
|
|
|
|
|
to make Buffer, offset, length and position optional
|
|
|
|
|
-->
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
|
|
|
|
|
* `offset` {integer} **Default:** `0`
|
|
|
|
|
* `length` {integer} **Default:** `buffer.length`
|
|
|
|
|
* `position` {integer} **Default:** `null`
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `bytesRead` {integer}
|
|
|
|
|
* `buffer` {Buffer}
|
|
|
|
|
|
|
|
|
|
Similar to the above `fs.read` function, this version takes an optional `options` object.
|
|
|
|
|
If no `options` object is specified, it will default with the above values.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readdir(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.8
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-09-10 16:20:42 +02:00
|
|
|
|
- version: v10.10.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22020
|
|
|
|
|
description: New option `withFileTypes` was added.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-04-10 20:35:07 +02:00
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5616
|
|
|
|
|
description: The `options` parameter was added.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
* `withFileTypes` {boolean} **Default:** `false`
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2018-07-28 04:29:32 +02:00
|
|
|
|
* `files` {string[]|Buffer[]|fs.Dirent[]}
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
|
Asynchronous readdir(3). Reads the contents of a directory.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The callback gets two arguments `(err, files)` where `files` is an array of
|
|
|
|
|
the names of the files in the directory excluding `'.'` and `'..'`.
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
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.
|
|
|
|
|
|
2018-07-28 04:29:32 +02:00
|
|
|
|
If `options.withFileTypes` is set to `true`, the `files` array will contain
|
|
|
|
|
[`fs.Dirent`][] objects.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readdirSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
2018-09-10 16:20:42 +02:00
|
|
|
|
- version: v10.10.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22020
|
|
|
|
|
description: New option `withFileTypes` was added.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-07-28 04:29:32 +02:00
|
|
|
|
* `withFileTypes` {boolean} **Default:** `false`
|
|
|
|
|
* Returns: {string[]|Buffer[]|fs.Dirent[]}
|
2015-03-08 03:52:18 +01:00
|
|
|
|
|
2018-04-15 18:32:44 +02:00
|
|
|
|
Synchronous readdir(3).
|
2015-03-08 03:52:18 +01:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
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
|
2018-07-11 11:10:42 +02:00
|
|
|
|
the filenames returned. If the `encoding` is set to `'buffer'`,
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
the filenames returned will be passed as `Buffer` objects.
|
|
|
|
|
|
2018-07-28 04:29:32 +02:00
|
|
|
|
If `options.withFileTypes` is set to `true`, the result will contain
|
|
|
|
|
[`fs.Dirent`][] objects.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readFile(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v5.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3740
|
|
|
|
|
description: The `callback` will always be called with `null` as the `error`
|
|
|
|
|
parameter in case of success.
|
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
2017-06-09 14:14:51 +02:00
|
|
|
|
description: The `path` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-05-03 23:56:04 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL|integer} filename or file descriptor
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `null`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'r'`.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `data` {string|Buffer}
|
2011-05-03 23:56:04 +02:00
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
|
Asynchronously reads the entire contents of a file.
|
2011-05-03 23:56:04 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.readFile('/etc/passwd', (err, data) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log(data);
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The callback is passed two arguments `(err, data)`, where `data` is the
|
|
|
|
|
contents of the file.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
If no encoding is specified, then the raw buffer is returned.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
|
If `options` is a string, then it specifies the encoding:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.readFile('/etc/passwd', 'utf8', callback);
|
|
|
|
|
```
|
2018-02-06 06:55:16 +01:00
|
|
|
|
|
|
|
|
|
When the path is a directory, the behavior of `fs.readFile()` and
|
|
|
|
|
[`fs.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.
|
2017-05-02 21:05:11 +02:00
|
|
|
|
|
|
|
|
|
```js
|
2017-11-29 03:53:24 +01:00
|
|
|
|
// macOS, Linux, and Windows
|
2017-05-02 21:05:11 +02:00
|
|
|
|
fs.readFile('<directory>', (err, data) => {
|
|
|
|
|
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// FreeBSD
|
|
|
|
|
fs.readFile('<directory>', (err, data) => {
|
|
|
|
|
// => null, <data>
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
The `fs.readFile()` function buffers the entire file. To minimize memory costs,
|
|
|
|
|
when possible prefer streaming via `fs.createReadStream()`.
|
2017-11-20 18:40:44 +01:00
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File descriptors
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-10-17 09:26:13 +02:00
|
|
|
|
1. Any specified file descriptor has to support reading.
|
|
|
|
|
2. If a file descriptor is specified as the `path`, it will not be closed
|
|
|
|
|
automatically.
|
|
|
|
|
3. The reading will begin at the current position. For example, if the file
|
|
|
|
|
already had `'Hello World`' and six bytes are read with the file descriptor,
|
|
|
|
|
the call to `fs.readFile()` with the same file descriptor, would give
|
|
|
|
|
`'World'`, rather than `'Hello World'`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readFileSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.8
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
2017-06-09 14:14:51 +02:00
|
|
|
|
description: The `path` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2013-04-08 00:41:33 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL|integer} filename or file descriptor
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `null`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'r'`.
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {string|Buffer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns the contents of the `path`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.readFile()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
If the `encoding` option is specified then this function returns a
|
|
|
|
|
string. Otherwise it returns a buffer.
|
2013-07-02 09:27:26 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Similar to [`fs.readFile()`][], when the path is a directory, the behavior of
|
|
|
|
|
`fs.readFileSync()` is platform-specific.
|
2017-05-02 21:05:11 +02:00
|
|
|
|
|
|
|
|
|
```js
|
2017-11-29 03:53:24 +01:00
|
|
|
|
// macOS, Linux, and Windows
|
2017-05-02 21:05:11 +02:00
|
|
|
|
fs.readFileSync('<directory>');
|
|
|
|
|
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
|
|
|
|
|
|
|
|
|
|
// FreeBSD
|
2018-05-23 01:54:30 +02:00
|
|
|
|
fs.readFileSync('<directory>'); // => <data>
|
2017-05-02 21:05:11 +02:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readlink(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `linkString` {string|Buffer}
|
2013-07-02 09:27:26 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous readlink(2). The callback gets two arguments `(err,
|
|
|
|
|
linkString)`.
|
2013-07-02 09:27:26 +02:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readlinkSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {string|Buffer}
|
2013-07-02 09:27:26 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous readlink(2). Returns the symbolic link's string value.
|
2013-07-02 09:27:26 +02:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
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
|
2018-07-11 11:10:42 +02:00
|
|
|
|
the link path returned. If the `encoding` is set to `'buffer'`,
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
the link path returned will be passed as a `Buffer` object.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.readSync(fd, buffer, offset, length, position)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `buffer` parameter can now be any `TypedArray` or a
|
|
|
|
|
`DataView`.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4518
|
|
|
|
|
description: The `length` parameter can now be `0`.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2016-04-26 16:27:18 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {number}
|
2016-04-26 16:27:18 +02:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns the number of `bytesRead`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.read()`][].
|
2016-04-26 16:27:18 +02:00
|
|
|
|
|
2020-03-24 16:23:33 +01:00
|
|
|
|
## `fs.readSync(fd, buffer, [options])`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.13.0
|
|
|
|
|
- v12.17.0
|
2020-03-24 16:23:33 +01:00
|
|
|
|
changes:
|
2020-05-01 14:43:14 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.13.0
|
|
|
|
|
- v12.17.0
|
2020-03-24 16:23:33 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/32460
|
|
|
|
|
description: Options object can be passed in
|
|
|
|
|
to make offset, length and position optional
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `offset` {integer} **Default:** `0`
|
|
|
|
|
* `length` {integer} **Default:** `buffer.length`
|
|
|
|
|
* `position` {integer} **Default:** `null`
|
|
|
|
|
* Returns: {number}
|
|
|
|
|
|
|
|
|
|
Returns the number of `bytesRead`.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.read()`][].
|
|
|
|
|
|
2020-03-16 14:50:27 +01:00
|
|
|
|
## `fs.readv(fd, buffers[, position], callback)`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.13.0
|
|
|
|
|
- v12.17.0
|
2020-03-16 14:50:27 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `bytesRead` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2020-05-27 18:55:15 +02:00
|
|
|
|
If this method is invoked as its [`util.promisify()`][]ed version, it returns
|
|
|
|
|
a `Promise` for an `Object` with `bytesRead` and `buffers` properties.
|
|
|
|
|
|
2020-03-16 14:50:27 +01:00
|
|
|
|
## `fs.readvSync(fd, buffers[, position])`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.13.0
|
|
|
|
|
- v12.17.0
|
2020-03-16 14:50:27 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {number} The number of bytes read.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.readv()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.realpath(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-03-16 04:26:14 +01:00
|
|
|
|
- version: v8.0.0
|
2017-05-15 00:57:54 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/13028
|
|
|
|
|
description: Pipe/Socket resolve support was added.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v6.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7899
|
|
|
|
|
description: Calling `realpath` now works again for various edge cases
|
|
|
|
|
on Windows.
|
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3594
|
|
|
|
|
description: The `cache` parameter was removed.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2013-03-01 18:10:26 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `resolvedPath` {string|Buffer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-11-07 21:16:36 +01:00
|
|
|
|
Asynchronously computes the canonical pathname by resolving `.`, `..` and
|
|
|
|
|
symbolic links.
|
|
|
|
|
|
2018-05-25 01:25:18 +02:00
|
|
|
|
A canonical pathname is not necessarily unique. Hard links and bind mounts can
|
2017-11-07 21:16:36 +01:00
|
|
|
|
expose a file system entity through many pathnames.
|
|
|
|
|
|
|
|
|
|
This function behaves like realpath(3), 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) implementation supports.
|
|
|
|
|
|
|
|
|
|
The `callback` gets two arguments `(err, resolvedPath)`. May use `process.cwd`
|
|
|
|
|
to resolve relative paths.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-09-18 21:47:27 +02:00
|
|
|
|
Only paths that can be converted to UTF8 strings are supported.
|
2016-07-27 00:18:35 +02:00
|
|
|
|
|
2016-04-11 16:48:34 +02:00
|
|
|
|
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.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
If `path` resolves to a socket or a pipe, the function will return a system
|
|
|
|
|
dependent name for that object.
|
2017-05-15 00:57:54 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.realpath.native(path[, options], callback)`
|
2017-11-07 21:16:36 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v9.2.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `resolvedPath` {string|Buffer}
|
|
|
|
|
|
|
|
|
|
Asynchronous realpath(3).
|
|
|
|
|
|
|
|
|
|
The `callback` gets two arguments `(err, resolvedPath)`.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
On Linux, when Node.js is linked against musl libc, the procfs file system must
|
2018-04-02 07:38:48 +02:00
|
|
|
|
be mounted on `/proc` in order for this function to work. Glibc does not have
|
2018-02-06 06:55:16 +01:00
|
|
|
|
this restriction.
|
2017-11-07 21:16:36 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.realpathSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2017-03-16 04:26:14 +01:00
|
|
|
|
- version: v8.0.0
|
2017-05-15 00:57:54 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/13028
|
|
|
|
|
description: Pipe/Socket resolve support was added.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v6.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7899
|
|
|
|
|
description: Calling `realpathSync` now works again for various edge cases
|
|
|
|
|
on Windows.
|
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3594
|
|
|
|
|
description: The `cache` parameter was removed.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-10-03 02:06:42 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {string|Buffer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns the resolved pathname.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.realpath()`][].
|
2017-05-15 00:57:54 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.realpathSync.native(path[, options])`
|
2017-11-07 21:16:36 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v9.2.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {string|Buffer}
|
2017-11-07 21:16:36 +01:00
|
|
|
|
|
|
|
|
|
Synchronous realpath(3).
|
|
|
|
|
|
|
|
|
|
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
|
2018-07-11 11:10:42 +02:00
|
|
|
|
the path returned. If the `encoding` is set to `'buffer'`,
|
2017-11-07 21:16:36 +01:00
|
|
|
|
the path returned will be passed as a `Buffer` object.
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
On Linux, when Node.js is linked against musl libc, the procfs file system must
|
2018-04-02 07:38:48 +02:00
|
|
|
|
be mounted on `/proc` in order for this function to work. Glibc does not have
|
2018-02-06 06:55:16 +01:00
|
|
|
|
this restriction.
|
2017-11-07 21:16:36 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.rename(oldPath, newPath, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `oldPath` and `newPath` parameters can be WHATWG `URL`
|
|
|
|
|
objects using `file:` protocol. Support is currently still
|
|
|
|
|
*experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-10-03 02:06:42 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `oldPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-02-16 09:44:04 +01:00
|
|
|
|
Asynchronously rename file at `oldPath` to the pathname provided
|
|
|
|
|
as `newPath`. In the case that `newPath` already exists, it will
|
2019-06-02 11:20:48 +02:00
|
|
|
|
be overwritten. If there is a directory at `newPath`, an error will
|
|
|
|
|
be raised instead. No arguments other than a possible exception are
|
2018-02-16 09:44:04 +01:00
|
|
|
|
given to the completion callback.
|
|
|
|
|
|
|
|
|
|
See also: rename(2).
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
fs.rename('oldFile.txt', 'newFile.txt', (err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
console.log('Rename complete!');
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.renameSync(oldPath, newPath)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `oldPath` and `newPath` parameters can be WHATWG `URL`
|
|
|
|
|
objects using `file:` protocol. Support is currently still
|
|
|
|
|
*experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `oldPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous rename(2). Returns `undefined`.
|
2013-03-01 18:10:26 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.rmdir(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.3.0
|
|
|
|
|
- v12.16.0
|
2019-11-25 21:04:18 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30644
|
2019-11-25 21:31:44 +01:00
|
|
|
|
description: The `maxBusyTries` option is renamed to `maxRetries`, and its
|
2019-11-25 22:13:27 +01:00
|
|
|
|
default is 0. The `emfileWait` option has been removed, and
|
2019-11-25 22:43:59 +01:00
|
|
|
|
`EMFILE` errors use the same retry logic as other errors. The
|
2019-11-27 16:16:36 +01:00
|
|
|
|
`retryDelay` option is now supported. `ENFILE` errors are now
|
|
|
|
|
retried.
|
2019-09-04 00:10:04 +02:00
|
|
|
|
- version: v12.10.0
|
2019-08-16 19:17:21 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29168
|
|
|
|
|
description: The `recursive`, `maxBusyTries`, and `emfileWait` options are
|
|
|
|
|
now supported.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameters can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2019-08-16 19:17:21 +02:00
|
|
|
|
> Stability: 1 - Recursive removal is experimental.
|
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `options` {Object}
|
2019-11-27 16:16:36 +01:00
|
|
|
|
* `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
|
|
|
backoff wait of `retryDelay` ms longer on each try. This option represents
|
|
|
|
|
the number of retries. This option is ignored if the `recursive` option is
|
|
|
|
|
not `true`. **Default:** `0`.
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `recursive` {boolean} If `true`, perform a recursive directory removal. In
|
2019-09-26 00:34:05 +02:00
|
|
|
|
recursive mode, errors are not reported if `path` does not exist, and
|
|
|
|
|
operations are retried on failure. **Default:** `false`.
|
2019-11-25 22:43:59 +01:00
|
|
|
|
* `retryDelay` {integer} The amount of time in milliseconds to wait between
|
2019-09-26 00:34:05 +02:00
|
|
|
|
retries. This option is ignored if the `recursive` option is not `true`.
|
|
|
|
|
**Default:** `100`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous rmdir(2). No arguments other than a possible exception are given
|
|
|
|
|
to the completion callback.
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on
|
|
|
|
|
Windows and an `ENOTDIR` error on POSIX.
|
2017-07-17 11:11:09 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.rmdirSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.3.0
|
|
|
|
|
- v12.16.0
|
2019-11-25 21:04:18 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30644
|
2019-11-25 21:31:44 +01:00
|
|
|
|
description: The `maxBusyTries` option is renamed to `maxRetries`, and its
|
2019-11-25 22:13:27 +01:00
|
|
|
|
default is 0. The `emfileWait` option has been removed, and
|
2019-11-25 22:43:59 +01:00
|
|
|
|
`EMFILE` errors use the same retry logic as other errors. The
|
2019-11-27 16:16:36 +01:00
|
|
|
|
`retryDelay` option is now supported. `ENFILE` errors are now
|
|
|
|
|
retried.
|
2019-09-04 00:10:04 +02:00
|
|
|
|
- version: v12.10.0
|
2019-08-16 19:17:21 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29168
|
|
|
|
|
description: The `recursive`, `maxBusyTries`, and `emfileWait` options are
|
|
|
|
|
now supported.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameters can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2019-08-16 19:17:21 +02:00
|
|
|
|
> Stability: 1 - Recursive removal is experimental.
|
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `options` {Object}
|
2019-11-27 16:16:36 +01:00
|
|
|
|
* `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
|
|
|
backoff wait of `retryDelay` ms longer on each try. This option represents
|
|
|
|
|
the number of retries. This option is ignored if the `recursive` option is
|
|
|
|
|
not `true`. **Default:** `0`.
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `recursive` {boolean} If `true`, perform a recursive directory removal. In
|
2019-09-26 00:34:05 +02:00
|
|
|
|
recursive mode, errors are not reported if `path` does not exist, and
|
|
|
|
|
operations are retried on failure. **Default:** `false`.
|
2019-11-25 22:43:59 +01:00
|
|
|
|
* `retryDelay` {integer} The amount of time in milliseconds to wait between
|
2019-09-26 00:34:05 +02:00
|
|
|
|
retries. This option is ignored if the `recursive` option is not `true`.
|
|
|
|
|
**Default:** `100`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous rmdir(2). Returns `undefined`.
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error
|
|
|
|
|
on Windows and an `ENOTDIR` error on POSIX.
|
2017-07-17 11:11:09 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.stat(path[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-05-27 05:11:38 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `stats` {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous stat(2). The callback gets two arguments `(err, stats)` where
|
2016-10-29 01:49:41 +02:00
|
|
|
|
`stats` is an [`fs.Stats`][] object.
|
2015-05-27 05:11:38 +02:00
|
|
|
|
|
2016-09-17 11:03:04 +02:00
|
|
|
|
In case of an error, the `err.code` will be one of [Common System Errors][].
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-10-02 06:31:57 +02:00
|
|
|
|
To check if a file exists without manipulating it afterwards, [`fs.access()`][]
|
2016-09-17 11:03:04 +02:00
|
|
|
|
is recommended.
|
|
|
|
|
|
2020-04-18 22:02:54 +02:00
|
|
|
|
For example, given the following directory structure:
|
2019-06-22 06:29:00 +02:00
|
|
|
|
|
2020-04-23 20:01:52 +02:00
|
|
|
|
```text
|
2019-06-22 06:29:00 +02:00
|
|
|
|
- txtDir
|
|
|
|
|
-- file.txt
|
|
|
|
|
- app.js
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The next program will check for the stats of the given paths:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < pathsToCheck.length; i++) {
|
|
|
|
|
fs.stat(pathsToCheck[i], function(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
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.statSync(path[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-10-03 02:06:42 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-02-14 08:17:14 +01:00
|
|
|
|
* Returns: {fs.Stats}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-15 18:32:44 +02:00
|
|
|
|
Synchronous stat(2).
|
2015-10-03 02:06:42 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.symlink(target, path[, type], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `target` and `path` parameters can be WHATWG `URL` objects
|
|
|
|
|
using `file:` protocol. Support is currently still
|
|
|
|
|
*experimental*.
|
2019-03-22 14:19:46 +01:00
|
|
|
|
- version: v12.0.0
|
2018-10-18 02:42:14 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23724
|
|
|
|
|
description: If the `type` argument is left undefined, Node will autodetect
|
|
|
|
|
`target` type and automatically select `dir` or `file`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `target` {string|Buffer|URL}
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-10-18 02:42:14 +02:00
|
|
|
|
* `type` {string}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2019-09-25 14:58:10 +02:00
|
|
|
|
Asynchronous symlink(2) which creates the link called `path` pointing to
|
2020-06-21 01:46:33 +02:00
|
|
|
|
`target`. No arguments other than a possible exception are given to the
|
2019-09-25 14:58:10 +02:00
|
|
|
|
completion callback.
|
2011-11-02 19:06:16 +01:00
|
|
|
|
|
2019-09-25 14:58:10 +02:00
|
|
|
|
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
|
2019-12-02 07:12:44 +01:00
|
|
|
|
not set, Node.js will autodetect `target` type and use `'file'` or `'dir'`. If
|
|
|
|
|
the `target` does not exist, `'file'` will be used. Windows junction points
|
2020-06-21 01:46:33 +02:00
|
|
|
|
require the destination path to be absolute. When using `'junction'`, the
|
2019-12-02 07:12:44 +01:00
|
|
|
|
`target` argument will automatically be normalized to absolute path.
|
2019-09-25 14:58:10 +02:00
|
|
|
|
|
|
|
|
|
Relative targets are relative to the link’s parent directory.
|
2015-11-25 05:40:42 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2019-09-25 14:58:10 +02:00
|
|
|
|
fs.symlink('./mew', './example/mewtwo', callback);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-11-25 05:40:42 +01:00
|
|
|
|
|
2019-09-25 14:58:10 +02:00
|
|
|
|
The above example creates a symbolic link `mewtwo` in the `example` which points
|
|
|
|
|
to `mew` in the same directory:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ tree example/
|
|
|
|
|
example/
|
|
|
|
|
├── mew
|
|
|
|
|
└── mewtwo -> ./mew
|
|
|
|
|
```
|
2015-11-25 05:40:42 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.symlinkSync(target, path[, type])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `target` and `path` parameters can be WHATWG `URL` objects
|
|
|
|
|
using `file:` protocol. Support is currently still
|
|
|
|
|
*experimental*.
|
2019-03-22 14:19:46 +01:00
|
|
|
|
- version: v12.0.0
|
2018-10-18 02:42:14 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23724
|
|
|
|
|
description: If the `type` argument is left undefined, Node will autodetect
|
|
|
|
|
`target` type and automatically select `dir` or `file`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `target` {string|Buffer|URL}
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-10-18 02:42:14 +02:00
|
|
|
|
* `type` {string}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns `undefined`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.symlink()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.truncate(path[, len], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.6
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Asynchronous truncate(2). 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.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Passing a file descriptor is deprecated and may result in an error being thrown
|
|
|
|
|
in the future.
|
2017-10-06 20:06:35 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.truncateSync(path[, len])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.6
|
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2016-08-27 12:23:14 +02:00
|
|
|
|
Synchronous truncate(2). Returns `undefined`. A file descriptor can also be
|
|
|
|
|
passed as the first argument. In this case, `fs.ftruncateSync()` is called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Passing a file descriptor is deprecated and may result in an error being thrown
|
|
|
|
|
in the future.
|
2017-10-06 20:06:35 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.unlink(path, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-02-07 22:11:03 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-02-18 01:09:05 +01:00
|
|
|
|
Asynchronously removes a file or symbolic link. No arguments other than a
|
|
|
|
|
possible exception are given to the completion callback.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Assuming that 'path/file.txt' is a regular file.
|
|
|
|
|
fs.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 [`fs.rmdir()`][].
|
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
|
See also: unlink(2).
|
2015-07-01 17:13:54 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.unlinkSync(path)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Synchronous unlink(2). Returns `undefined`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.unwatchFile(filename[, listener])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
|
|
|
|
-->
|
2012-03-04 01:23:31 +01:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `filename` {string|Buffer|URL}
|
2018-01-23 04:05:31 +01:00
|
|
|
|
* `listener` {Function} Optional, a listener previously attached using
|
|
|
|
|
`fs.watchFile()`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2012-07-08 16:31:07 +02:00
|
|
|
|
Stop watching for changes on `filename`. If `listener` is specified, only that
|
2017-04-26 19:16:12 +02:00
|
|
|
|
particular listener is removed. Otherwise, *all* listeners are removed,
|
|
|
|
|
effectively stopping watching of `filename`.
|
2012-07-08 16:31:07 +02:00
|
|
|
|
|
|
|
|
|
Calling `fs.unwatchFile()` with a filename that is not being watched is a
|
|
|
|
|
no-op, not an error.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Using [`fs.watch()`][] is more efficient than `fs.watchFile()` and
|
2018-04-02 07:38:48 +02:00
|
|
|
|
`fs.unwatchFile()`. `fs.watch()` should be used instead of `fs.watchFile()`
|
2017-05-20 22:15:58 +02:00
|
|
|
|
and `fs.unwatchFile()` when possible.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.utimes(path, atime, mtime, callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-10-04 18:24:23 +02:00
|
|
|
|
- version: v8.0.0
|
2017-09-29 12:24:10 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/11919
|
2017-10-04 18:24:23 +02:00
|
|
|
|
description: "`NaN`, `Infinity`, and `-Infinity` are no longer valid time
|
|
|
|
|
specifiers."
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v4.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2387
|
|
|
|
|
description: Numeric strings, `NaN` and `Infinity` are now allowed
|
|
|
|
|
time specifiers.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2017-07-10 16:53:50 +02:00
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2017-07-10 16:53:50 +02:00
|
|
|
|
Change the file system timestamps of the object referenced by `path`.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-07-10 16:53:50 +02:00
|
|
|
|
The `atime` and `mtime` arguments follow these rules:
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2020-05-04 13:33:21 +02:00
|
|
|
|
* Values can be either numbers representing Unix epoch time in seconds,
|
|
|
|
|
`Date`s, or a numeric string like `'123456789.0'`.
|
2019-09-13 06:22:29 +02:00
|
|
|
|
* If the value can not be converted to a number, or is `NaN`, `Infinity` or
|
2018-04-29 19:46:41 +02:00
|
|
|
|
`-Infinity`, an `Error` will be thrown.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.utimesSync(path, atime, mtime)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2017-09-29 12:24:10 +02:00
|
|
|
|
- version: v8.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/11919
|
2017-10-04 18:24:23 +02:00
|
|
|
|
description: "`NaN`, `Infinity`, and `-Infinity` are no longer valid time
|
|
|
|
|
specifiers."
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `path` parameter can be a WHATWG `URL` object using `file:`
|
|
|
|
|
protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v4.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2387
|
|
|
|
|
description: Numeric strings, `NaN` and `Infinity` are now allowed
|
|
|
|
|
time specifiers.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-09-23 12:25:15 +02:00
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns `undefined`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.utimes()`][].
|
2015-11-04 18:07:07 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.watch(filename[, options][, listener])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.10
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `filename` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7831
|
|
|
|
|
description: The passed `options` object will never be modified.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2012-03-04 01:23:31 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `filename` {string|Buffer|URL}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {string|Object}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `persistent` {boolean} Indicates whether the process should continue to run
|
2018-04-02 03:44:32 +02:00
|
|
|
|
as long as files are being watched. **Default:** `true`.
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `recursive` {boolean} Indicates whether all subdirectories should be
|
2017-04-20 20:55:45 +02:00
|
|
|
|
watched, or only the current directory. This applies when a directory is
|
2017-06-18 20:28:07 +02:00
|
|
|
|
specified, and only on supported platforms (See [Caveats][]). **Default:**
|
2018-04-02 03:44:32 +02:00
|
|
|
|
`false`.
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `encoding` {string} Specifies the character encoding to be used for the
|
2018-04-02 03:44:32 +02:00
|
|
|
|
filename passed to the listener. **Default:** `'utf8'`.
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `listener` {Function|undefined} **Default:** `undefined`
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `eventType` {string}
|
|
|
|
|
* `filename` {string|Buffer}
|
2018-04-15 18:32:44 +02:00
|
|
|
|
* Returns: {fs.FSWatcher}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2011-10-12 01:01:37 +02:00
|
|
|
|
Watch for changes on `filename`, where `filename` is either a file or a
|
2018-04-15 18:32:44 +02:00
|
|
|
|
directory.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2016-03-09 05:58:45 +01:00
|
|
|
|
The second argument is optional. If `options` is provided as a string, it
|
|
|
|
|
specifies the `encoding`. Otherwise `options` should be passed as an object.
|
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
|
The listener callback gets two arguments `(eventType, filename)`. `eventType`
|
2018-02-12 08:31:55 +01:00
|
|
|
|
is either `'rename'` or `'change'`, and `filename` is the name of the file
|
|
|
|
|
which triggered the event.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
On most platforms, `'rename'` is emitted whenever a filename appears or
|
|
|
|
|
disappears in the directory.
|
2016-10-27 19:49:21 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
The listener callback is attached to the `'change'` event fired by
|
2016-10-27 19:49:21 +02:00
|
|
|
|
[`fs.FSWatcher`][], but it is not the same thing as the `'change'` value of
|
|
|
|
|
`eventType`.
|
2016-07-01 10:33:20 +02:00
|
|
|
|
|
2012-03-04 01:23:31 +01:00
|
|
|
|
### Caveats
|
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
|
|
|
|
The `fs.watch` API is not 100% consistent across platforms, and is
|
|
|
|
|
unavailable in some situations.
|
|
|
|
|
|
2017-03-29 01:46:10 +02:00
|
|
|
|
The recursive option is only supported on macOS and Windows.
|
2019-10-12 11:23:07 +02:00
|
|
|
|
An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown
|
|
|
|
|
when the option is used on a platform that does not support it.
|
2013-10-22 04:08:28 +02:00
|
|
|
|
|
2020-03-10 16:02:19 +01:00
|
|
|
|
On Windows, no events will be emitted if the watched directory is moved or
|
|
|
|
|
renamed. An `EPERM` error is reported when the watched directory is deleted.
|
|
|
|
|
|
2012-03-04 01:23:31 +01:00
|
|
|
|
#### Availability
|
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
|
|
|
|
This feature depends on the underlying operating system providing a way
|
|
|
|
|
to be notified of filesystem changes.
|
|
|
|
|
|
2019-10-02 06:31:57 +02:00
|
|
|
|
* On Linux systems, this uses [`inotify(7)`][].
|
|
|
|
|
* On BSD systems, this uses [`kqueue(2)`][].
|
|
|
|
|
* On macOS, this uses [`kqueue(2)`][] for files and [`FSEvents`][] for
|
|
|
|
|
directories.
|
|
|
|
|
* On SunOS systems (including Solaris and SmartOS), this uses [`event ports`][].
|
|
|
|
|
* On Windows systems, this feature depends on [`ReadDirectoryChangesW`][].
|
|
|
|
|
* On Aix systems, this feature depends on [`AHAFS`][], which must be enabled.
|
2012-03-04 01:23:31 +01:00
|
|
|
|
|
|
|
|
|
If the underlying functionality is not available for some reason, then
|
2020-03-27 07:17:54 +01:00
|
|
|
|
`fs.watch()` will not be able to function and may thrown an exception.
|
|
|
|
|
For example, watching files or directories can be unreliable, and in some
|
|
|
|
|
cases impossible, on network file systems (NFS, SMB, etc) or host file systems
|
|
|
|
|
when using virtualization software such as Vagrant or Docker.
|
2012-09-12 17:04:31 +02:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
It is still possible to use `fs.watchFile()`, which uses stat polling, but
|
|
|
|
|
this method is slower and less reliable.
|
2012-03-04 01:23:31 +01:00
|
|
|
|
|
2016-04-07 14:36:39 +02:00
|
|
|
|
#### Inodes
|
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
2017-03-29 01:46:10 +02:00
|
|
|
|
On Linux and macOS systems, `fs.watch()` resolves the path to an [inode][] and
|
2016-04-07 14:36:39 +02:00
|
|
|
|
watches the inode. If the watched path is deleted and recreated, it is assigned
|
|
|
|
|
a new inode. The watch will emit an event for the delete but will continue
|
|
|
|
|
watching the *original* inode. Events for the new inode will not be emitted.
|
|
|
|
|
This is expected behavior.
|
|
|
|
|
|
2017-12-07 06:48:11 +01:00
|
|
|
|
AIX files retain the same inode for the lifetime of a file. Saving and closing a
|
|
|
|
|
watched file on AIX will result in two notifications (one for adding new
|
|
|
|
|
content, and one for truncation).
|
2016-12-02 06:55:02 +01:00
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
#### Filename argument
|
2012-03-04 01:23:31 +01:00
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
2017-05-19 04:07:52 +02:00
|
|
|
|
Providing `filename` argument in the callback is only supported on Linux,
|
2018-04-02 07:38:48 +02:00
|
|
|
|
macOS, Windows, and AIX. Even on supported platforms, `filename` is not always
|
2017-05-19 04:07:52 +02:00
|
|
|
|
guaranteed to be provided. Therefore, don't assume that `filename` argument is
|
2018-04-29 19:46:41 +02:00
|
|
|
|
always provided in the callback, and have some fallback logic if it is `null`.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2016-07-01 10:33:20 +02:00
|
|
|
|
fs.watch('somedir', (eventType, filename) => {
|
|
|
|
|
console.log(`event type is: ${eventType}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
if (filename) {
|
|
|
|
|
console.log(`filename provided: ${filename}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('filename not provided');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.watchFile(filename[, options], listener)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.31
|
2017-04-25 16:20:58 +02:00
|
|
|
|
changes:
|
2020-03-06 18:00:43 +01:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
|
|
|
|
description: The `bigint` option is now supported.
|
2017-04-25 16:20:58 +02:00
|
|
|
|
- version: v7.6.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10739
|
|
|
|
|
description: The `filename` parameter can be a WHATWG `URL` object using
|
|
|
|
|
`file:` protocol. Support is currently still *experimental*.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2014-12-15 16:44:46 +01:00
|
|
|
|
|
2017-04-25 16:20:58 +02:00
|
|
|
|
* `filename` {string|Buffer|URL}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `options` {Object}
|
2020-03-06 18:00:43 +01:00
|
|
|
|
* `bigint` {boolean} **Default:** `false`
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `persistent` {boolean} **Default:** `true`
|
|
|
|
|
* `interval` {integer} **Default:** `5007`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `listener` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `current` {fs.Stats}
|
|
|
|
|
* `previous` {fs.Stats}
|
2020-04-28 20:00:35 +02:00
|
|
|
|
* Returns: {fs.StatWatcher}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Watch for changes on `filename`. The callback `listener` will be called each
|
|
|
|
|
time the file is accessed.
|
2014-12-15 16:44:46 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
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
|
2018-04-02 03:44:32 +02:00
|
|
|
|
target should be polled in milliseconds.
|
2014-12-15 16:44:46 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The `listener` gets two arguments the current stat object and the previous
|
|
|
|
|
stat object:
|
2014-12-15 16:44:46 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.watchFile('message.text', (curr, prev) => {
|
|
|
|
|
console.log(`the current mtime is: ${curr.mtime}`);
|
|
|
|
|
console.log(`the previous mtime was: ${prev.mtime}`);
|
|
|
|
|
});
|
|
|
|
|
```
|
2014-12-15 16:44:46 +01:00
|
|
|
|
|
2020-03-06 18:00:43 +01:00
|
|
|
|
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.
|
2015-06-24 05:42:49 +02:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
To be notified when the file was modified, not just accessed, it is necessary
|
|
|
|
|
to compare `curr.mtime` and `prev.mtime`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
When an `fs.watchFile` operation results in an `ENOENT` error, it
|
2017-05-20 22:15:58 +02:00
|
|
|
|
will invoke the listener once, with all the fields zeroed (or, for dates, the
|
2019-02-12 16:39:10 +01:00
|
|
|
|
Unix Epoch). If the file is created later on, the listener will be called
|
2017-05-20 22:15:58 +02:00
|
|
|
|
again, with the latest stat objects. This is a change in functionality since
|
|
|
|
|
v0.10.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Using [`fs.watch()`][] is more efficient than `fs.watchFile` and
|
2016-05-02 19:27:12 +02:00
|
|
|
|
`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and
|
2017-05-02 21:05:11 +02:00
|
|
|
|
`fs.unwatchFile` when possible.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
When a file being watched by `fs.watchFile()` disappears and reappears,
|
2017-10-09 09:19:07 +02:00
|
|
|
|
then the `previousStat` reported in the second callback event (the file's
|
|
|
|
|
reappearance) will be the same as the `previousStat` of the first callback
|
|
|
|
|
event (its disappearance).
|
|
|
|
|
|
|
|
|
|
This happens when:
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
|
* the file is deleted, followed by a restore
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* the file is renamed and then renamed a second time back to its original name
|
2017-10-09 09:19:07 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.write(fd, buffer[, offset[, length[, position]]], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `buffer` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `buffer` parameter can now be any `TypedArray` or a
|
|
|
|
|
`DataView`
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10382
|
|
|
|
|
description: The `buffer` parameter can now be a `Uint8Array`.
|
|
|
|
|
- version: v7.2.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7856
|
|
|
|
|
description: The `offset` and `length` parameters are optional now.
|
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `bytesWritten` {integer}
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
Write `buffer` to the file specified by `fd`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-11-25 00:35:15 +01:00
|
|
|
|
`offset` determines the part of the buffer to be written, and `length` is
|
|
|
|
|
an integer specifying the number of bytes to write.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`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).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-04-16 21:19:36 +02:00
|
|
|
|
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
|
2018-04-29 19:46:41 +02:00
|
|
|
|
a `Promise` for an `Object` with `bytesWritten` and `buffer` properties.
|
2011-04-14 22:45:32 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
It is unsafe to use `fs.write()` multiple times on the same file without waiting
|
2018-09-26 15:15:54 +02:00
|
|
|
|
for the callback. For this scenario, [`fs.createWriteStream()`][] is
|
|
|
|
|
recommended.
|
2011-04-14 22:45:32 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
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.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.write(fd, string[, position[, encoding]], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.5
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `string` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.2.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7856
|
|
|
|
|
description: The `position` parameter is optional now.
|
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `string` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `position` {integer}
|
2018-08-19 17:24:56 +02:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2016-03-18 22:52:11 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `written` {integer}
|
|
|
|
|
* `string` {string}
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
|
Write `string` to the file specified by `fd`. If `string` is not a string, then
|
2019-12-26 19:48:53 +01:00
|
|
|
|
an exception will be thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`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).
|
2011-09-11 22:30:01 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
`encoding` is the expected string encoding.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
The callback will receive the arguments `(err, written, string)` where `written`
|
2018-07-04 01:51:28 +02:00
|
|
|
|
specifies how many _bytes_ the passed string required to be written. Bytes
|
|
|
|
|
written is not necessarily the same as string characters written. See
|
|
|
|
|
[`Buffer.byteLength`][].
|
2014-07-26 16:04:46 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
It is unsafe to use `fs.write()` multiple times on the same file without waiting
|
2018-09-26 15:15:54 +02:00
|
|
|
|
for the callback. For this scenario, [`fs.createWriteStream()`][] is
|
|
|
|
|
recommended.
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2015-11-04 18:07:07 +01:00
|
|
|
|
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.
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2018-11-22 17:36:17 +01:00
|
|
|
|
On Windows, if the file descriptor is connected to the console (e.g. `fd == 1`
|
|
|
|
|
or `stdout`) a string containing non-ASCII characters will not be rendered
|
|
|
|
|
properly by default, regardless of the encoding used.
|
|
|
|
|
It is possible to configure the console to render UTF-8 properly by changing the
|
|
|
|
|
active codepage with the `chcp 65001` command. See the [chcp][] docs for more
|
|
|
|
|
details.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writeFile(file, data[, options], callback)`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `data` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `data` parameter can now be any `TypedArray` or a
|
|
|
|
|
`DataView`.
|
2018-03-02 18:53:46 +01:00
|
|
|
|
- version: v10.0.0
|
2018-02-09 00:54:31 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/12562
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
|
|
|
|
it will throw a `TypeError` at runtime.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10382
|
|
|
|
|
description: The `data` parameter can now be a `Uint8Array`.
|
|
|
|
|
- version: v7.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7897
|
|
|
|
|
description: The `callback` parameter is no longer optional. Not passing
|
2018-02-09 00:54:31 +01:00
|
|
|
|
it will emit a deprecation warning with id DEP0013.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
|
|
|
|
description: The `file` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `file` {string|Buffer|URL|integer} filename or file descriptor
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `data` {string|Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
|
2015-11-04 18:07:07 +01:00
|
|
|
|
* `callback` {Function}
|
2017-06-02 23:14:30 +02:00
|
|
|
|
* `err` {Error}
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2019-04-17 19:04:30 +02:00
|
|
|
|
When `file` is a filename, asynchronously writes data to the file, replacing the
|
2019-12-26 19:48:53 +01:00
|
|
|
|
file if it already exists. `data` can be a string or a buffer.
|
2019-04-17 19:04:30 +02:00
|
|
|
|
|
|
|
|
|
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.
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
|
The `encoding` option is ignored if `data` is a buffer.
|
2012-02-27 20:09:33 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2018-08-06 11:55:59 +02:00
|
|
|
|
const data = new Uint8Array(Buffer.from('Hello Node.js'));
|
|
|
|
|
fs.writeFile('message.txt', data, (err) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
if (err) throw err;
|
2017-03-25 14:33:03 +01:00
|
|
|
|
console.log('The file has been saved!');
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
```
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
|
If `options` is a string, then it specifies the encoding:
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
|
|
|
|
|
```
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
It is unsafe to use `fs.writeFile()` multiple times on the same file without
|
2018-09-26 15:15:54 +02:00
|
|
|
|
waiting for the callback. For this scenario, [`fs.createWriteStream()`][] is
|
2018-07-04 01:51:28 +02:00
|
|
|
|
recommended.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### Using `fs.writeFile()` with file descriptors
|
2019-04-17 19:04:30 +02:00
|
|
|
|
|
|
|
|
|
When `file` is a file descriptor, the behavior is almost identical to directly
|
|
|
|
|
calling `fs.write()` like:
|
2019-08-29 15:28:03 +02:00
|
|
|
|
|
2020-05-24 01:34:40 +02:00
|
|
|
|
```js
|
2019-04-17 19:04:30 +02:00
|
|
|
|
fs.write(fd, Buffer.from(data, options.encoding), callback);
|
|
|
|
|
```
|
2018-10-17 09:26:13 +02:00
|
|
|
|
|
2019-04-17 19:04:30 +02:00
|
|
|
|
The difference from directly calling `fs.write()` is that under some unusual
|
|
|
|
|
conditions, `fs.write()` may write only part of the buffer and will need to be
|
|
|
|
|
retried to write the remaining data, whereas `fs.writeFile()` will retry until
|
|
|
|
|
the data is entirely written (or an error occurs).
|
|
|
|
|
|
2019-06-20 21:54:20 +02:00
|
|
|
|
The implications of this are a common source of confusion. In
|
|
|
|
|
the file descriptor case, the file is not replaced! The data is not necessarily
|
2019-04-17 19:04:30 +02:00
|
|
|
|
written to the beginning of the file, and the file's original data may remain
|
|
|
|
|
before and/or after the newly written data.
|
|
|
|
|
|
|
|
|
|
For example, if `fs.writeFile()` is called twice in a row, first to write the
|
|
|
|
|
string `'Hello'`, then to write the string `', World'`, the file would contain
|
|
|
|
|
`'Hello, World'`, and might contain some of the file's original data (depending
|
2020-06-21 01:46:33 +02:00
|
|
|
|
on the size of the original file, and the position of the file descriptor). If
|
2019-04-17 19:04:30 +02:00
|
|
|
|
a file name had been used instead of a descriptor, the file would be guaranteed
|
|
|
|
|
to contain only `', World'`.
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writeFileSync(file, data[, options])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `data` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `data` parameter can now be any `TypedArray` or a
|
|
|
|
|
`DataView`.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10382
|
|
|
|
|
description: The `data` parameter can now be a `Uint8Array`.
|
|
|
|
|
- version: v5.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/3163
|
|
|
|
|
description: The `file` parameter can be a file descriptor now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2018-01-23 03:12:43 +01:00
|
|
|
|
* `file` {string|Buffer|URL|integer} filename or file descriptor
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `data` {string|Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `options` {Object|string}
|
2017-06-18 20:28:07 +02:00
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2018-06-10 15:28:00 +02:00
|
|
|
|
Returns `undefined`.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.writeFile()`][].
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writeSync(fd, buffer[, offset[, length[, position]]])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.21
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `buffer` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-09-06, Version 10.10.0 (Current)
Notable changes:
* child_process:
* `TypedArray` and `DataView` values are now accepted as input by
`execFileSync` and `spawnSync`. https://github.com/nodejs/node/pull/22409
* coverage:
* Native V8 code coverage information can now be output to disk by setting the
environment variable `NODE_V8_COVERAGE` to a directory. https://github.com/nodejs/node/pull/22527
* deps:
* The bundled npm was upgraded to version 6.4.1. https://github.com/nodejs/node/pull/22591
* Changelogs:
[6.3.0-next.0](https://github.com/npm/cli/releases/tag/v6.3.0-next.0)
[6.3.0](https://github.com/npm/cli/releases/tag/v6.3.0)
[6.4.0](https://github.com/npm/cli/releases/tag/v6.4.0)
[6.4.1](https://github.com/npm/cli/releases/tag/v6.4.1)
* fs:
* The methods `fs.read`, `fs.readSync`, `fs.write`, `fs.writeSync`,
`fs.writeFile` and `fs.writeFileSync` now all accept `TypedArray` and
`DataView` objects. https://github.com/nodejs/node/pull/22150
* A new boolean option, `withFileTypes`, can be passed to to `fs.readdir` and
`fs.readdirSync`. If set to true, the methods return an array of directory
entries. These are objects that can be used to determine the type of each
entry and filter them based on that without calling `fs.stat`. https://github.com/nodejs/node/pull/22020
* http2:
* The `http2` module is no longer experimental. https://github.com/nodejs/node/pull/22466
* os:
* Added two new methods: `os.getPriority` and `os.setPriority`, allowing to
manipulate the scheduling priority of processes. https://github.com/nodejs/node/pull/22407
* process:
* Added `process.allowedNodeEnvironmentFlags`. This object can be used to
programmatically validate and list flags that are allowed in the
`NODE_OPTIONS` environment variable. https://github.com/nodejs/node/pull/19335
* src:
* Deprecated option variables in public C++ API. https://github.com/nodejs/node/pull/22515
* Refactored options parsing. https://github.com/nodejs/node/pull/22392
* vm:
* Added `vm.compileFunction`, a method to create new JavaScript functions from
a source body, with options similar to those of the other `vm` methods. https://github.com/nodejs/node/pull/21571
* Added new collaborators:
* [lundibundi](https://github.com/lundibundi) - Denys Otrishko
PR-URL: https://github.com/nodejs/node/pull/22716
2018-09-03 20:14:31 +02:00
|
|
|
|
- version: v10.10.0
|
2018-08-06 11:55:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22150
|
|
|
|
|
description: The `buffer` parameter can now be any `TypedArray` or a
|
|
|
|
|
`DataView`.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10382
|
|
|
|
|
description: The `buffer` parameter can now be a `Uint8Array`.
|
|
|
|
|
- version: v7.2.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7856
|
|
|
|
|
description: The `offset` and `length` parameters are optional now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2011-10-12 01:01:37 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2018-08-06 11:55:59 +02:00
|
|
|
|
* `buffer` {Buffer|TypedArray|DataView}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
2018-08-19 17:24:56 +02:00
|
|
|
|
* Returns: {number} The number of bytes written.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.write(fd, buffer...)`][].
|
2016-03-18 22:52:11 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writeSync(fd, string[, position[, encoding]])`
|
2016-05-12 20:52:39 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.5
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `string` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2017-02-21 23:38:45 +01:00
|
|
|
|
- version: v7.2.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7856
|
|
|
|
|
description: The `position` parameter is optional now.
|
2016-05-12 20:52:39 +02:00
|
|
|
|
-->
|
2015-08-20 00:37:52 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `fd` {integer}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `string` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* `position` {integer}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `encoding` {string}
|
2018-08-19 17:24:56 +02:00
|
|
|
|
* Returns: {number} The number of bytes written.
|
2018-06-10 15:28:00 +02:00
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
2018-08-19 17:24:56 +02:00
|
|
|
|
this API: [`fs.write(fd, string...)`][].
|
2015-11-28 00:30:32 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writev(fd, buffers[, position], callback)`
|
2019-02-04 17:18:39 +01:00
|
|
|
|
<!-- YAML
|
2019-08-19 21:14:22 +02:00
|
|
|
|
added: v12.9.0
|
2019-02-04 17:18:39 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* `err` {Error}
|
|
|
|
|
* `bytesWritten` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
|
|
|
|
|
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 [`fs.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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs.writevSync(fd, buffers[, position])`
|
2019-02-04 17:18:39 +01:00
|
|
|
|
<!-- YAML
|
2019-08-19 21:14:22 +02:00
|
|
|
|
added: v12.9.0
|
2019-02-04 17:18:39 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `fd` {integer}
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {number} The number of bytes written.
|
|
|
|
|
|
|
|
|
|
For detailed information, see the documentation of the asynchronous version of
|
|
|
|
|
this API: [`fs.writev()`][].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
## `fs` Promises API
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2018-05-03 20:40:48 +02:00
|
|
|
|
The `fs.promises` API provides an alternative set of asynchronous file system
|
2018-01-21 19:21:25 +01:00
|
|
|
|
methods that return `Promise` objects rather than using callbacks. The
|
2020-01-28 17:29:29 +01:00
|
|
|
|
API is accessible via `require('fs').promises` or `require('fs/promises')`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2020-06-19 19:56:28 +02:00
|
|
|
|
### Class: `FileHandle`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
A `FileHandle` object is a wrapper for a numeric file descriptor.
|
|
|
|
|
Instances of `FileHandle` are distinct from numeric file descriptors
|
2019-05-30 12:15:02 +02:00
|
|
|
|
in that they provide an object oriented API for working with files.
|
|
|
|
|
|
|
|
|
|
If a `FileHandle` is not closed using the
|
|
|
|
|
`filehandle.close()` method, it might automatically close the file descriptor
|
2018-01-21 19:21:25 +01:00
|
|
|
|
and will emit a process warning, thereby helping to prevent memory leaks.
|
2020-03-08 02:35:00 +01:00
|
|
|
|
Please do not rely on this behavior because it is unreliable and
|
|
|
|
|
the file may not be closed. Instead, always explicitly close `FileHandle`s.
|
2019-05-30 12:15:02 +02:00
|
|
|
|
Node.js may change this behavior in the future.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
Instances of the `FileHandle` object are created internally by the
|
2018-02-14 10:04:22 +01:00
|
|
|
|
`fsPromises.open()` method.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2018-05-01 06:56:46 +02:00
|
|
|
|
Unlike the callback-based API (`fs.fstat()`, `fs.fchown()`, `fs.fchmod()`, and
|
|
|
|
|
so on), a numeric file descriptor is not used by the promise-based API. Instead,
|
|
|
|
|
the promise-based API uses the `FileHandle` class in order to help avoid
|
|
|
|
|
accidental leaking of unclosed file descriptors after a `Promise` is resolved or
|
|
|
|
|
rejected.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.appendFile(data, options)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `data` {string|Buffer}
|
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
2020-01-07 04:21:54 +01:00
|
|
|
|
Alias of [`filehandle.writeFile()`][].
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2020-01-07 04:21:54 +01:00
|
|
|
|
When operating on file handles, the mode cannot be changed from what it was set
|
|
|
|
|
to with [`fsPromises.open()`][]. Therefore, this is equivalent to
|
|
|
|
|
[`filehandle.writeFile()`][].
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.chmod(mode)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `mode` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Modifies the permissions on the file. The `Promise` is resolved with no
|
|
|
|
|
arguments upon success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.chown(uid, gid)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the ownership of the file then resolves the `Promise` with no arguments
|
|
|
|
|
upon success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.close()`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {Promise} A `Promise` that will be resolved once the underlying
|
|
|
|
|
file descriptor is closed, or will be rejected if an error occurs while
|
|
|
|
|
closing.
|
|
|
|
|
|
|
|
|
|
Closes the file descriptor.
|
|
|
|
|
|
|
|
|
|
```js
|
2018-05-19 19:45:26 +02:00
|
|
|
|
const fsPromises = require('fs').promises;
|
2018-01-21 19:21:25 +01:00
|
|
|
|
async function openAndClose() {
|
|
|
|
|
let filehandle;
|
|
|
|
|
try {
|
2018-02-14 10:04:22 +01:00
|
|
|
|
filehandle = await fsPromises.open('thefile.txt', 'r');
|
2018-01-21 19:21:25 +01:00
|
|
|
|
} finally {
|
|
|
|
|
if (filehandle !== undefined)
|
|
|
|
|
await filehandle.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.datasync()`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous fdatasync(2). The `Promise` is resolved with no arguments upon
|
|
|
|
|
success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.fd`
|
2018-04-16 17:03:31 +02:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-04-16 17:03:31 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number} The numeric file descriptor managed by the `FileHandle` object.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.read(buffer, offset, length, position)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `buffer` {Buffer|Uint8Array}
|
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Read data from the file.
|
|
|
|
|
|
|
|
|
|
`buffer` is the buffer that the data will be written to.
|
|
|
|
|
|
|
|
|
|
`offset` is the offset in the buffer to start writing at.
|
|
|
|
|
|
|
|
|
|
`length` is an integer specifying the number of bytes to read.
|
|
|
|
|
|
|
|
|
|
`position` is an argument specifying where to begin reading from in the file.
|
|
|
|
|
If `position` is `null`, 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 remain unchanged.
|
|
|
|
|
|
|
|
|
|
Following successful read, the `Promise` is resolved with an object with a
|
2018-02-12 08:31:55 +01:00
|
|
|
|
`bytesRead` property specifying the number of bytes read, and a `buffer`
|
|
|
|
|
property that is a reference to the passed in `buffer` argument.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2020-01-16 15:46:28 +01:00
|
|
|
|
#### `filehandle.read(options)`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.11.0
|
|
|
|
|
- v12.17.0
|
2020-01-16 15:46:28 +01:00
|
|
|
|
-->
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `buffer` {Buffer|Uint8Array} **Default:** `Buffer.alloc(16384)`
|
|
|
|
|
* `offset` {integer} **Default:** `0`
|
|
|
|
|
* `length` {integer} **Default:** `buffer.length`
|
|
|
|
|
* `position` {integer} **Default:** `null`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.readFile(options)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `null`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously reads the entire contents of a file.
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with the contents of the file. If no encoding is
|
|
|
|
|
specified (using `options.encoding`), the data is returned as a `Buffer`
|
|
|
|
|
object. Otherwise, the data will be a string.
|
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
|
|
|
|
The `FileHandle` has to support reading.
|
|
|
|
|
|
2018-10-17 09:26:13 +02:00
|
|
|
|
If one or more `filehandle.read()` calls are made on a file handle and then a
|
|
|
|
|
`filehandle.readFile()` call is made, the data will be read from the current
|
|
|
|
|
position till the end of the file. It doesn't always read from the beginning
|
|
|
|
|
of the file.
|
|
|
|
|
|
2020-03-16 14:50:27 +01:00
|
|
|
|
#### `filehandle.readv(buffers[, position])`
|
|
|
|
|
<!-- YAML
|
2020-05-01 14:43:14 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.13.0
|
|
|
|
|
- v12.17.0
|
2020-03-16 14:50:27 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Read from a file and write to an array of `ArrayBufferView`s
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with an object containing a `bytesRead` property
|
|
|
|
|
identifying the number of bytes read, and a `buffers` property containing
|
|
|
|
|
a reference to the `buffers` input.
|
|
|
|
|
|
|
|
|
|
`position` is the offset from the beginning of the file where this data
|
|
|
|
|
should be read from. If `typeof position !== 'number'`, the data will be read
|
|
|
|
|
from the current position.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.stat([options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-04-23 11:14:56 +02:00
|
|
|
|
changes:
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Retrieves the [`fs.Stats`][] for the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.sync()`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous fsync(2). The `Promise` is resolved with no arguments upon
|
|
|
|
|
success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.truncate(len)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Truncates the file then resolves the `Promise` with no arguments upon success.
|
|
|
|
|
|
|
|
|
|
If the file 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
|
2018-05-19 19:45:26 +02:00
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const fsPromises = fs.promises;
|
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8'));
|
|
|
|
|
// Prints: Node.js
|
|
|
|
|
|
|
|
|
|
async function doTruncate() {
|
2018-05-23 15:48:47 +02:00
|
|
|
|
let filehandle = null;
|
|
|
|
|
try {
|
|
|
|
|
filehandle = await fsPromises.open('temp.txt', 'r+');
|
|
|
|
|
await filehandle.truncate(4);
|
|
|
|
|
} finally {
|
|
|
|
|
if (filehandle) {
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Close the file if it is opened.
|
2018-05-23 15:48:47 +02:00
|
|
|
|
await filehandle.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doTruncate().catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
If the file previously was shorter than `len` bytes, it is extended, and the
|
2018-08-26 18:02:27 +02:00
|
|
|
|
extended part is filled with null bytes (`'\0'`):
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
```js
|
2018-05-19 19:45:26 +02:00
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const fsPromises = fs.promises;
|
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8'));
|
|
|
|
|
// Prints: Node.js
|
|
|
|
|
|
|
|
|
|
async function doTruncate() {
|
2018-05-23 15:48:47 +02:00
|
|
|
|
let filehandle = null;
|
|
|
|
|
try {
|
|
|
|
|
filehandle = await fsPromises.open('temp.txt', 'r+');
|
|
|
|
|
await filehandle.truncate(10);
|
|
|
|
|
} finally {
|
|
|
|
|
if (filehandle) {
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Close the file if it is opened.
|
2018-05-23 15:48:47 +02:00
|
|
|
|
await filehandle.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doTruncate().catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
|
The last three bytes are null bytes (`'\0'`), to compensate the over-truncation.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.utimes(atime, mtime)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `atime` {number|string|Date}
|
2018-04-29 19:46:41 +02:00
|
|
|
|
* `mtime` {number|string|Date}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Change the file system timestamps of the object referenced by the `FileHandle`
|
|
|
|
|
then resolves the `Promise` with no arguments upon success.
|
|
|
|
|
|
|
|
|
|
This function does not work on AIX versions before 7.1, it will resolve the
|
|
|
|
|
`Promise` with an error using code `UV_ENOSYS`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.write(buffer[, offset[, length[, position]]])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `buffer` parameter won't coerce unsupported input to
|
|
|
|
|
buffers anymore.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `buffer` {Buffer|Uint8Array}
|
|
|
|
|
* `offset` {integer}
|
|
|
|
|
* `length` {integer}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Write `buffer` to the file.
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with an object containing a `bytesWritten` property
|
|
|
|
|
identifying the number of bytes written, and a `buffer` property containing
|
|
|
|
|
a reference to the `buffer` written.
|
|
|
|
|
|
|
|
|
|
`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).
|
|
|
|
|
|
|
|
|
|
It is unsafe to use `filehandle.write()` multiple times on the same file
|
|
|
|
|
without waiting for the `Promise` to be resolved (or rejected). For this
|
2019-06-02 17:12:10 +02:00
|
|
|
|
scenario, use [`fs.createWriteStream()`][].
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
On Linux, positional writes do not work when the file is opened in append mode.
|
|
|
|
|
The kernel ignores the position argument and always appends the data to
|
|
|
|
|
the end of the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.write(string[, position[, encoding]])`
|
2018-11-13 17:28:07 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v10.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `string` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-11-13 17:28:07 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `string` {string}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Write `string` to the file. If `string` is not a string, then
|
2019-12-19 19:00:45 +01:00
|
|
|
|
an exception will be thrown.
|
2018-11-13 17:28:07 +01:00
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with an object containing a `bytesWritten` property
|
|
|
|
|
identifying the number of bytes written, and a `buffer` property containing
|
|
|
|
|
a reference to the `string` written.
|
|
|
|
|
|
|
|
|
|
`position` refers to the offset from the beginning of the file where this data
|
|
|
|
|
should be written. If the type of `position` is not a `number` the data
|
|
|
|
|
will be written at the current position. See pwrite(2).
|
|
|
|
|
|
|
|
|
|
`encoding` is the expected string encoding.
|
|
|
|
|
|
|
|
|
|
It is unsafe to use `filehandle.write()` multiple times on the same file
|
|
|
|
|
without waiting for the `Promise` to be resolved (or rejected). For this
|
2019-06-02 17:12:10 +02:00
|
|
|
|
scenario, use [`fs.createWriteStream()`][].
|
2018-11-13 17:28:07 +01:00
|
|
|
|
|
|
|
|
|
On Linux, positional writes do not work when the file is opened in append mode.
|
|
|
|
|
The kernel ignores the position argument and always appends the data to
|
|
|
|
|
the end of the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.writeFile(data, options)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `data` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `data` {string|Buffer|Uint8Array}
|
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously writes data to a file, replacing the file if it already exists.
|
|
|
|
|
`data` can be a string or a buffer. The `Promise` will be resolved with no
|
|
|
|
|
arguments upon success.
|
|
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
|
The `encoding` option is ignored if `data` is a buffer.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
|
|
|
|
The `FileHandle` has to support writing.
|
|
|
|
|
|
|
|
|
|
It is unsafe to use `filehandle.writeFile()` multiple times on the same file
|
|
|
|
|
without waiting for the `Promise` to be resolved (or rejected).
|
|
|
|
|
|
2018-10-17 09:26:13 +02:00
|
|
|
|
If one or more `filehandle.write()` calls are made on a file handle and then a
|
|
|
|
|
`filehandle.writeFile()` call is made, the data will be written from the
|
|
|
|
|
current position till the end of the file. It doesn't always write from the
|
|
|
|
|
beginning of the file.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
#### `filehandle.writev(buffers[, position])`
|
2019-08-17 20:56:13 +02:00
|
|
|
|
<!-- YAML
|
2019-08-19 21:14:22 +02:00
|
|
|
|
added: v12.9.0
|
2019-08-17 20:56:13 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `buffers` {ArrayBufferView[]}
|
|
|
|
|
* `position` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Write an array of `ArrayBufferView`s to the file.
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with an object containing a `bytesWritten` property
|
|
|
|
|
identifying the number of bytes written, and a `buffers` property containing
|
|
|
|
|
a reference to the `buffers` input.
|
|
|
|
|
|
|
|
|
|
`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.
|
|
|
|
|
|
|
|
|
|
It is unsafe to call `writev()` multiple times on the same file without waiting
|
|
|
|
|
for the previous operation to complete.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.access(path[, mode])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `mode` {integer} **Default:** `fs.constants.F_OK`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Tests a user's permissions for the file or directory specified by `path`.
|
|
|
|
|
The `mode` argument is an optional integer that specifies the accessibility
|
2020-06-14 23:49:34 +02:00
|
|
|
|
checks to be performed. Check [File access constants][] for possible values
|
2018-05-06 09:35:21 +02:00
|
|
|
|
of `mode`. It is possible to create a mask consisting of the bitwise OR of
|
|
|
|
|
two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
If the accessibility check is successful, the `Promise` is resolved with no
|
|
|
|
|
value. If any of the accessibility checks fail, the `Promise` is rejected
|
|
|
|
|
with an `Error` object. The following example checks if the file
|
|
|
|
|
`/etc/passwd` can be read and written by the current process.
|
|
|
|
|
|
|
|
|
|
```js
|
2018-05-19 19:45:26 +02:00
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const fsPromises = fs.promises;
|
|
|
|
|
|
2018-02-14 10:04:22 +01:00
|
|
|
|
fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK)
|
2018-01-21 19:21:25 +01:00
|
|
|
|
.then(() => console.log('can access'))
|
|
|
|
|
.catch(() => console.error('cannot access'));
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-14 10:04:22 +01:00
|
|
|
|
Using `fsPromises.access()` to check for the accessibility of a file before
|
|
|
|
|
calling `fsPromises.open()` is not recommended. Doing so introduces a race
|
2018-01-21 19:21:25 +01:00
|
|
|
|
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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.appendFile(path, data[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
* `path` {string|Buffer|URL|FileHandle} filename or `FileHandle`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `data` {string|Buffer}
|
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'a'`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously append data to a file, creating the file if it does not yet
|
|
|
|
|
exist. `data` can be a string or a [`Buffer`][]. The `Promise` will be
|
|
|
|
|
resolved with no arguments upon success.
|
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
2018-05-03 07:20:45 +02:00
|
|
|
|
The `path` may be specified as a `FileHandle` that has been opened
|
2018-02-14 10:04:22 +01:00
|
|
|
|
for appending (using `fsPromises.open()`).
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.chmod(path, mode)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-12-27 16:09:35 +01:00
|
|
|
|
* `mode` {string|integer}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the permissions of a file then resolves the `Promise` with no
|
|
|
|
|
arguments upon succces.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.chown(path, uid, gid)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the ownership of a file then resolves the `Promise` with no arguments
|
|
|
|
|
upon success.
|
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
### `fsPromises.copyFile(src, dest[, mode])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `src` {string|Buffer|URL} source filename to copy
|
|
|
|
|
* `dest` {string|Buffer|URL} destination filename of the copy operation
|
2019-09-26 00:34:05 +02:00
|
|
|
|
* `mode` {integer} modifiers for copy operation. **Default:** `0`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it
|
|
|
|
|
already exists. The `Promise` will be resolved with no arguments upon success.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`mode` is an optional integer that specifies the behavior
|
2018-04-02 21:12:57 +02:00
|
|
|
|
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`).
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
|
2018-04-02 21:12:57 +02:00
|
|
|
|
exists.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
|
2018-04-02 21:12:57 +02:00
|
|
|
|
copy-on-write reflink. If the platform does not support copy-on-write, then a
|
|
|
|
|
fallback copy mechanism is used.
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
|
2018-04-02 21:12:57 +02:00
|
|
|
|
create a copy-on-write reflink. If the platform does not support copy-on-write,
|
|
|
|
|
then the operation will fail.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
```js
|
2019-09-26 14:13:49 +02:00
|
|
|
|
const {
|
|
|
|
|
promises: fsPromises,
|
|
|
|
|
constants: {
|
|
|
|
|
COPYFILE_EXCL
|
|
|
|
|
}
|
|
|
|
|
} = require('fs');
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
// destination.txt will be created or overwritten by default.
|
2018-02-14 10:04:22 +01:00
|
|
|
|
fsPromises.copyFile('source.txt', 'destination.txt')
|
2018-01-21 19:21:25 +01:00
|
|
|
|
.then(() => console.log('source.txt was copied to destination.txt'))
|
|
|
|
|
.catch(() => console.log('The file could not be copied'));
|
|
|
|
|
|
|
|
|
|
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
|
2018-02-14 10:04:22 +01:00
|
|
|
|
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
|
2018-01-21 19:21:25 +01:00
|
|
|
|
.then(() => console.log('source.txt was copied to destination.txt'))
|
|
|
|
|
.catch(() => console.log('The file could not be copied'));
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.lchmod(path, mode)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
deprecated: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-02-06 12:56:53 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `mode` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the permissions on a symbolic link then resolves the `Promise` with
|
|
|
|
|
no arguments upon success. This method is only implemented on macOS.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.lchown(path, uid, gid)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-06-23 22:26:29 +02:00
|
|
|
|
added: v10.0.0
|
|
|
|
|
changes:
|
2018-07-03 09:44:13 +02:00
|
|
|
|
- version: v10.6.0
|
2018-06-23 22:26:29 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21498
|
|
|
|
|
description: This API is no longer deprecated.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-02-06 12:56:53 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `uid` {integer}
|
|
|
|
|
* `gid` {integer}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the ownership on a symbolic link then resolves the `Promise` with
|
2018-06-23 22:26:29 +02:00
|
|
|
|
no arguments upon success.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2020-05-14 11:54:00 +02:00
|
|
|
|
### `fsPromises.lutimes(path, atime, mtime)`
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: REPLACEME
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Changes the access and modification times of a file in the same way as
|
|
|
|
|
[`fsPromises.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.
|
|
|
|
|
|
|
|
|
|
Upon success, the `Promise` is resolved without arguments.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.link(existingPath, newPath)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `existingPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous link(2). The `Promise` is resolved with no arguments upon success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.lstat(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-04-23 11:14:56 +02:00
|
|
|
|
changes:
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous lstat(2). The `Promise` is resolved with the [`fs.Stats`][] object
|
|
|
|
|
for the given symbolic link `path`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.mkdir(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-08-10 01:52:41 +02:00
|
|
|
|
* `options` {Object|integer}
|
|
|
|
|
* `recursive` {boolean} **Default:** `false`
|
2019-12-27 16:20:31 +01:00
|
|
|
|
* `mode` {string|integer} Not supported on Windows. **Default:** `0o777`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
2020-01-29 08:06:44 +01:00
|
|
|
|
Asynchronously creates a directory then resolves the `Promise` with either no
|
2020-04-18 22:02:54 +02:00
|
|
|
|
arguments, or the first directory path created if `recursive` is `true`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-09-26 00:34:05 +02:00
|
|
|
|
The optional `options` argument can be an integer specifying `mode` (permission
|
2018-08-10 01:52:41 +02:00
|
|
|
|
and sticky bits), or an object with a `mode` property and a `recursive`
|
2020-04-18 22:02:54 +02:00
|
|
|
|
property indicating whether parent directories should be created. Calling
|
2019-05-01 01:12:46 +02:00
|
|
|
|
`fsPromises.mkdir()` when `path` is a directory that exists results in a
|
|
|
|
|
rejection only when `recursive` is false.
|
2018-08-10 01:52:41 +02:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.mkdtemp(prefix[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `prefix` {string}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
2018-05-04 06:51:03 +02:00
|
|
|
|
Creates a unique temporary directory and resolves the `Promise` with the created
|
2020-04-18 22:02:54 +02:00
|
|
|
|
directory path. A unique directory name is generated by appending six random
|
2019-03-27 15:38:57 +01:00
|
|
|
|
characters to the end of the provided `prefix`. 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.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
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
|
2018-02-14 10:04:22 +01:00
|
|
|
|
fsPromises.mkdtemp(path.join(os.tmpdir(), 'foo-'))
|
2018-01-21 19:21:25 +01:00
|
|
|
|
.catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
2018-05-04 06:51:03 +02:00
|
|
|
|
The `fsPromises.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
|
2018-01-21 19:21:25 +01:00
|
|
|
|
(`require('path').sep`).
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.open(path, flags[, mode])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-11-08 01:45:33 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v11.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/23767
|
|
|
|
|
description: The `flags` argument is now optional and defaults to `'r'`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flags` {string|number} See [support of file system `flags`][].
|
2018-11-08 01:45:33 +01:00
|
|
|
|
**Default:** `'r'`.
|
2019-12-27 16:16:58 +01:00
|
|
|
|
* `mode` {string|integer} **Default:** `0o666` (readable and writable)
|
2018-04-02 07:38:48 +02:00
|
|
|
|
* Returns: {Promise}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
Asynchronous file open that returns a `Promise` that, when resolved, yields a
|
|
|
|
|
`FileHandle` object. See open(2).
|
|
|
|
|
|
|
|
|
|
`mode` sets the file mode (permission and sticky bits), but only if the file was
|
2018-04-02 03:44:32 +02:00
|
|
|
|
created.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
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][MSDN-Using-Streams].
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.opendir(path[, options])`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
<!-- YAML
|
2019-10-10 14:31:33 +02:00
|
|
|
|
added: v12.12.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.1.0
|
|
|
|
|
- v12.16.0
|
2019-10-25 16:17:07 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30114
|
|
|
|
|
description: The `bufferSize` option was introduced.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
2019-10-25 16:17:07 +02:00
|
|
|
|
* `bufferSize` {number} Number of directory entries that are buffered
|
|
|
|
|
internally when reading from the directory. Higher values lead to better
|
|
|
|
|
performance but higher memory usage. **Default:** `32`
|
2019-08-28 02:14:27 +02:00
|
|
|
|
* Returns: {Promise} containing {fs.Dir}
|
|
|
|
|
|
|
|
|
|
Asynchronously 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
|
2019-10-09 21:10:06 +02:00
|
|
|
|
directory and subsequent read operations.
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
2019-10-08 22:18:08 +02:00
|
|
|
|
Example using async iteration:
|
2019-08-28 02:14:27 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
async function print(path) {
|
|
|
|
|
const dir = await fs.promises.opendir(path);
|
|
|
|
|
for await (const dirent of dir) {
|
|
|
|
|
console.log(dirent.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print('./').catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.readdir(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-09-13 06:32:29 +02:00
|
|
|
|
changes:
|
2018-09-18 15:39:46 +02:00
|
|
|
|
- version: v10.11.0
|
2018-09-13 06:32:29 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/22020
|
|
|
|
|
description: New option `withFileTypes` was added.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2018-09-13 06:32:29 +02:00
|
|
|
|
* `withFileTypes` {boolean} **Default:** `false`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Reads the contents of a directory then resolves the `Promise` with an array
|
2018-06-28 19:48:59 +02:00
|
|
|
|
of the names of the files in the directory excluding `'.'` and `'..'`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
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. If the `encoding` is set to `'buffer'`, the filenames returned
|
|
|
|
|
will be passed as `Buffer` objects.
|
|
|
|
|
|
2018-09-13 06:32:29 +02:00
|
|
|
|
If `options.withFileTypes` is set to `true`, the resolved array will contain
|
|
|
|
|
[`fs.Dirent`][] objects.
|
|
|
|
|
|
2020-01-28 17:06:56 +01:00
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
async function print(path) {
|
|
|
|
|
const files = await fs.promises.readdir(path);
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
console.log(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print('./').catch(console.error);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.readFile(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-02-06 16:48:45 +01:00
|
|
|
|
* `path` {string|Buffer|URL|FileHandle} filename or `FileHandle`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `null`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'r'`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously reads the entire contents of a file.
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with the contents of the file. If no encoding is
|
|
|
|
|
specified (using `options.encoding`), the data is returned as a `Buffer`
|
|
|
|
|
object. Otherwise, the data will be a string.
|
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
2018-02-14 10:04:22 +01:00
|
|
|
|
When the `path` is a directory, the behavior of `fsPromises.readFile()` is
|
2018-01-21 19:21:25 +01:00
|
|
|
|
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
|
|
|
|
|
with an error. On FreeBSD, a representation of the directory's contents will be
|
|
|
|
|
returned.
|
|
|
|
|
|
|
|
|
|
Any specified `FileHandle` has to support reading.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.readlink(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous readlink(2). The `Promise` is resolved with the `linkString` upon
|
|
|
|
|
success.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.realpath(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `options` {string|Object}
|
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Determines the actual location of `path` using the same semantics as the
|
|
|
|
|
`fs.realpath.native()` function then resolves the `Promise` with the resolved
|
|
|
|
|
path.
|
|
|
|
|
|
|
|
|
|
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. If the `encoding` is set to `'buffer'`, the path returned will be
|
|
|
|
|
passed as a `Buffer` object.
|
|
|
|
|
|
|
|
|
|
On Linux, when Node.js is linked against musl libc, the procfs file system must
|
2018-04-02 07:38:48 +02:00
|
|
|
|
be mounted on `/proc` in order for this function to work. Glibc does not have
|
2018-01-21 19:21:25 +01:00
|
|
|
|
this restriction.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.rename(oldPath, newPath)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `oldPath` {string|Buffer|URL}
|
|
|
|
|
* `newPath` {string|Buffer|URL}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Renames `oldPath` to `newPath` and resolves the `Promise` with no arguments
|
|
|
|
|
upon success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.rmdir(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2019-08-16 19:17:21 +02:00
|
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.3.0
|
|
|
|
|
- v12.16.0
|
2019-11-25 21:04:18 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30644
|
2019-11-25 21:31:44 +01:00
|
|
|
|
description: The `maxBusyTries` option is renamed to `maxRetries`, and its
|
2019-11-25 22:13:27 +01:00
|
|
|
|
default is 0. The `emfileWait` option has been removed, and
|
2019-11-25 22:43:59 +01:00
|
|
|
|
`EMFILE` errors use the same retry logic as other errors. The
|
2019-11-27 16:16:36 +01:00
|
|
|
|
`retryDelay` option is now supported. `ENFILE` errors are now
|
|
|
|
|
retried.
|
2019-09-04 00:10:04 +02:00
|
|
|
|
- version: v12.10.0
|
2019-08-16 19:17:21 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/29168
|
|
|
|
|
description: The `recursive`, `maxBusyTries`, and `emfileWait` options are
|
|
|
|
|
now supported.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2019-08-16 19:17:21 +02:00
|
|
|
|
> Stability: 1 - Recursive removal is experimental.
|
|
|
|
|
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `options` {Object}
|
2019-11-27 16:16:36 +01:00
|
|
|
|
* `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
2019-09-26 00:34:05 +02:00
|
|
|
|
`EPERM` error is encountered, Node.js will retry the operation with a linear
|
|
|
|
|
backoff wait of `retryDelay` ms longer on each try. This option represents
|
|
|
|
|
the number of retries. This option is ignored if the `recursive` option is
|
|
|
|
|
not `true`. **Default:** `0`.
|
2019-08-16 19:17:21 +02:00
|
|
|
|
* `recursive` {boolean} If `true`, perform a recursive directory removal. In
|
2019-09-26 00:34:05 +02:00
|
|
|
|
recursive mode, errors are not reported if `path` does not exist, and
|
|
|
|
|
operations are retried on failure. **Default:** `false`.
|
2019-11-25 22:43:59 +01:00
|
|
|
|
* `retryDelay` {integer} The amount of time in milliseconds to wait between
|
2019-09-26 00:34:05 +02:00
|
|
|
|
retries. This option is ignored if the `recursive` option is not `true`.
|
|
|
|
|
**Default:** `100`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Removes the directory identified by `path` then resolves the `Promise` with
|
|
|
|
|
no arguments upon success.
|
|
|
|
|
|
2018-02-14 10:04:22 +01:00
|
|
|
|
Using `fsPromises.rmdir()` on a file (not a directory) results in the
|
2018-01-21 19:21:25 +01:00
|
|
|
|
`Promise` being rejected with an `ENOENT` error on Windows and an `ENOTDIR`
|
|
|
|
|
error on POSIX.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.stat(path[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-04-23 11:14:56 +02:00
|
|
|
|
changes:
|
2018-06-19 09:35:50 +02:00
|
|
|
|
- version: v10.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20220
|
2018-04-23 11:14:56 +02:00
|
|
|
|
description: Accepts an additional `options` object to specify whether
|
|
|
|
|
the numeric values returned should be bigint.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-04-23 11:14:56 +02:00
|
|
|
|
* `options` {Object}
|
|
|
|
|
* `bigint` {boolean} Whether the numeric values in the returned
|
|
|
|
|
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
The `Promise` is resolved with the [`fs.Stats`][] object for the given `path`.
|
|
|
|
|
|
2020-01-01 17:44:16 +01:00
|
|
|
|
### `fsPromises.symlink(target, path[, type])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `target` {string|Buffer|URL}
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `type` {string} **Default:** `'file'`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Creates a symbolic link then resolves the `Promise` with no arguments upon
|
|
|
|
|
success.
|
|
|
|
|
|
|
|
|
|
The `type` argument is only used on Windows platforms and can be one of `'dir'`,
|
2018-07-04 01:51:28 +02:00
|
|
|
|
`'file'`, or `'junction'`. Windows junction points require the destination path
|
|
|
|
|
to be absolute. When using `'junction'`, the `target` argument will
|
|
|
|
|
automatically be normalized to absolute path.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.truncate(path[, len])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-02-06 12:56:53 +01:00
|
|
|
|
* `path` {string|Buffer|URL}
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `len` {integer} **Default:** `0`
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Truncates the `path` then resolves the `Promise` with no arguments upon
|
|
|
|
|
success. The `path` *must* be a string or `Buffer`.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.unlink(path)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronous unlink(2). The `Promise` is resolved with no arguments upon
|
|
|
|
|
success.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.utimes(path, atime, mtime)`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `path` {string|Buffer|URL}
|
|
|
|
|
* `atime` {number|string|Date}
|
|
|
|
|
* `mtime` {number|string|Date}
|
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Change the file system timestamps of the object referenced by `path` then
|
|
|
|
|
resolves the `Promise` with no arguments upon success.
|
|
|
|
|
|
|
|
|
|
The `atime` and `mtime` arguments follow these rules:
|
2019-09-06 07:42:22 +02:00
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
|
* Values can be either numbers representing Unix epoch time, `Date`s, or a
|
2018-01-21 19:21:25 +01:00
|
|
|
|
numeric string like `'123456789.0'`.
|
2019-09-13 06:22:29 +02:00
|
|
|
|
* If the value can not be converted to a number, or is `NaN`, `Infinity` or
|
2018-01-21 19:21:25 +01:00
|
|
|
|
`-Infinity`, an `Error` will be thrown.
|
|
|
|
|
|
2019-12-24 07:01:33 +01:00
|
|
|
|
### `fsPromises.writeFile(file, data[, options])`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
|
added: v10.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
changes:
|
2020-03-10 18:16:08 +01:00
|
|
|
|
- version: v14.0.0
|
2019-12-19 19:00:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31030
|
|
|
|
|
description: The `data` parameter won't coerce unsupported input to
|
|
|
|
|
strings anymore.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2018-02-06 16:48:45 +01:00
|
|
|
|
* `file` {string|Buffer|URL|FileHandle} filename or `FileHandle`
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* `data` {string|Buffer|Uint8Array}
|
|
|
|
|
* `options` {Object|string}
|
|
|
|
|
* `encoding` {string|null} **Default:** `'utf8'`
|
|
|
|
|
* `mode` {integer} **Default:** `0o666`
|
2018-04-15 03:50:48 +02:00
|
|
|
|
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
* Returns: {Promise}
|
|
|
|
|
|
|
|
|
|
Asynchronously writes data to a file, replacing the file if it already exists.
|
|
|
|
|
`data` can be a string or a buffer. The `Promise` will be resolved with no
|
|
|
|
|
arguments upon success.
|
|
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
|
The `encoding` option is ignored if `data` is a buffer.
|
2018-01-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
If `options` is a string, then it specifies the encoding.
|
|
|
|
|
|
|
|
|
|
Any specified `FileHandle` has to support writing.
|
|
|
|
|
|
2018-02-14 10:04:22 +01:00
|
|
|
|
It is unsafe to use `fsPromises.writeFile()` multiple times on the same file
|
2018-01-21 19:21:25 +01:00
|
|
|
|
without waiting for the `Promise` to be resolved (or rejected).
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
## FS constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
2017-05-20 22:15:58 +02:00
|
|
|
|
The following constants are exported by `fs.constants`.
|
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Not every constant will be available on every operating system.
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
2020-05-07 17:18:20 +02:00
|
|
|
|
To use more than one constant, use the bitwise OR `|` operator.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
O_RDWR,
|
|
|
|
|
O_CREAT,
|
|
|
|
|
O_EXCL
|
|
|
|
|
} = fs.constants;
|
|
|
|
|
|
|
|
|
|
fs.open('/path/to/my/file', O_RDWR | O_CREAT | O_EXCL, (err, fd) => {
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File access constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
|
|
The following constants are meant for use with [`fs.access()`][].
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Constant</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>F_OK</code></td>
|
2018-05-06 09:35:21 +02:00
|
|
|
|
<td>Flag indicating that the file is visible to the calling process.
|
|
|
|
|
This is useful for determining if a file exists, but says nothing
|
|
|
|
|
about <code>rwx</code> permissions. Default if no mode is specified.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>R_OK</code></td>
|
|
|
|
|
<td>Flag indicating that the file can be read by the calling process.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>W_OK</code></td>
|
|
|
|
|
<td>Flag indicating that the file can be written by the calling
|
|
|
|
|
process.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>X_OK</code></td>
|
|
|
|
|
<td>Flag indicating that the file can be executed by the calling
|
2018-05-06 09:35:21 +02:00
|
|
|
|
process. This has no effect on Windows
|
|
|
|
|
(will behave like <code>fs.constants.F_OK</code>).</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File copy constants
|
2018-04-02 21:12:57 +02:00
|
|
|
|
|
|
|
|
|
The following constants are meant for use with [`fs.copyFile()`][].
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Constant</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>COPYFILE_EXCL</code></td>
|
|
|
|
|
<td>If present, the copy operation will fail with an error if the
|
|
|
|
|
destination path already exists.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>COPYFILE_FICLONE</code></td>
|
|
|
|
|
<td>If present, the copy operation will attempt to create a
|
|
|
|
|
copy-on-write reflink. If the underlying platform does not support
|
|
|
|
|
copy-on-write, then a fallback copy mechanism is used.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>COPYFILE_FICLONE_FORCE</code></td>
|
|
|
|
|
<td>If present, the copy operation will attempt to create a
|
|
|
|
|
copy-on-write reflink. If the underlying platform does not support
|
|
|
|
|
copy-on-write, then the operation will fail with an error.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File open constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
|
|
The following constants are meant for use with `fs.open()`.
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Constant</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_RDONLY</code></td>
|
|
|
|
|
<td>Flag indicating to open a file for read-only access.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_WRONLY</code></td>
|
|
|
|
|
<td>Flag indicating to open a file for write-only access.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_RDWR</code></td>
|
|
|
|
|
<td>Flag indicating to open a file for read-write access.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_CREAT</code></td>
|
|
|
|
|
<td>Flag indicating to create the file if it does not already exist.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_EXCL</code></td>
|
|
|
|
|
<td>Flag indicating that opening a file should fail if the
|
|
|
|
|
<code>O_CREAT</code> flag is set and the file already exists.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_NOCTTY</code></td>
|
|
|
|
|
<td>Flag indicating that if path identifies a terminal device, opening the
|
|
|
|
|
path shall not cause that terminal to become the controlling terminal for
|
|
|
|
|
the process (if the process does not already have one).</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_TRUNC</code></td>
|
|
|
|
|
<td>Flag indicating that if the file exists and is a regular file, and the
|
|
|
|
|
file is opened successfully for write access, its length shall be truncated
|
|
|
|
|
to zero.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_APPEND</code></td>
|
|
|
|
|
<td>Flag indicating that data will be appended to the end of the file.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_DIRECTORY</code></td>
|
|
|
|
|
<td>Flag indicating that the open should fail if the path is not a
|
|
|
|
|
directory.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_NOATIME</code></td>
|
|
|
|
|
<td>Flag indicating reading accesses to the file system will no longer
|
2018-07-12 19:28:42 +02:00
|
|
|
|
result in an update to the <code>atime</code> information associated with
|
2018-11-24 09:42:09 +01:00
|
|
|
|
the file. This flag is available on Linux operating systems only.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_NOFOLLOW</code></td>
|
|
|
|
|
<td>Flag indicating that the open should fail if the path is a symbolic
|
|
|
|
|
link.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_SYNC</code></td>
|
2017-09-22 03:10:04 +02:00
|
|
|
|
<td>Flag indicating that the file is opened for synchronized I/O with write
|
|
|
|
|
operations waiting for file integrity.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
2017-09-15 23:08:11 +02:00
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_DSYNC</code></td>
|
2017-09-22 03:10:04 +02:00
|
|
|
|
<td>Flag indicating that the file is opened for synchronized I/O with write
|
|
|
|
|
operations waiting for data integrity.</td>
|
2017-09-15 23:08:11 +02:00
|
|
|
|
</tr>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_SYMLINK</code></td>
|
2016-06-02 19:39:39 +02:00
|
|
|
|
<td>Flag indicating to open the symbolic link itself rather than the
|
2016-05-02 19:27:12 +02:00
|
|
|
|
resource it is pointing to.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_DIRECT</code></td>
|
|
|
|
|
<td>When set, an attempt will be made to minimize caching effects of file
|
|
|
|
|
I/O.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>O_NONBLOCK</code></td>
|
|
|
|
|
<td>Flag indicating to open the file in nonblocking mode when possible.</td>
|
|
|
|
|
</tr>
|
2019-08-22 01:40:47 +02:00
|
|
|
|
<tr>
|
|
|
|
|
<td><code>UV_FS_O_FILEMAP</code></td>
|
|
|
|
|
<td>When set, a memory file mapping is used to access the file. This flag
|
|
|
|
|
is available on Windows operating systems only. On other operating systems,
|
|
|
|
|
this flag is ignored.</td>
|
|
|
|
|
</tr>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</table>
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File type constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
|
|
The following constants are meant for use with the [`fs.Stats`][] object's
|
|
|
|
|
`mode` property for determining a file's type.
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Constant</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFMT</code></td>
|
|
|
|
|
<td>Bit mask used to extract the file type code.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFREG</code></td>
|
|
|
|
|
<td>File type constant for a regular file.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFDIR</code></td>
|
|
|
|
|
<td>File type constant for a directory.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFCHR</code></td>
|
|
|
|
|
<td>File type constant for a character-oriented device file.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFBLK</code></td>
|
|
|
|
|
<td>File type constant for a block-oriented device file.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFIFO</code></td>
|
|
|
|
|
<td>File type constant for a FIFO/pipe.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFLNK</code></td>
|
|
|
|
|
<td>File type constant for a symbolic link.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IFSOCK</code></td>
|
|
|
|
|
<td>File type constant for a socket.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
### File mode constants
|
2016-05-02 19:27:12 +02:00
|
|
|
|
|
|
|
|
|
The following constants are meant for use with the [`fs.Stats`][] object's
|
|
|
|
|
`mode` property for determining the access permissions for a file.
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Constant</th>
|
|
|
|
|
<th>Description</th>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IRWXU</code></td>
|
2017-11-29 03:53:24 +01:00
|
|
|
|
<td>File mode indicating readable, writable, and executable by owner.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IRUSR</code></td>
|
|
|
|
|
<td>File mode indicating readable by owner.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IWUSR</code></td>
|
|
|
|
|
<td>File mode indicating writable by owner.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IXUSR</code></td>
|
|
|
|
|
<td>File mode indicating executable by owner.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IRWXG</code></td>
|
2017-11-29 03:53:24 +01:00
|
|
|
|
<td>File mode indicating readable, writable, and executable by group.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IRGRP</code></td>
|
|
|
|
|
<td>File mode indicating readable by group.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IWGRP</code></td>
|
|
|
|
|
<td>File mode indicating writable by group.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IXGRP</code></td>
|
|
|
|
|
<td>File mode indicating executable by group.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IRWXO</code></td>
|
2017-11-29 03:53:24 +01:00
|
|
|
|
<td>File mode indicating readable, writable, and executable by others.</td>
|
2016-05-02 19:27:12 +02:00
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IROTH</code></td>
|
|
|
|
|
<td>File mode indicating readable by others.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IWOTH</code></td>
|
|
|
|
|
<td>File mode indicating writable by others.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>S_IXOTH</code></td>
|
|
|
|
|
<td>File mode indicating executable by others.</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
|
## File system flags
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
|
|
|
|
The following flags are available wherever the `flag` option takes a
|
2019-10-24 06:28:42 +02:00
|
|
|
|
string.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'a'`: Open file for appending.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created if it does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'ax'`: Like `'a'` but fails if the path exists.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'a+'`: Open file for reading and appending.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created if it does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'ax+'`: Like `'a+'` but fails if the path exists.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'as'`: Open file for appending in synchronous mode.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created if it does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'as+'`: Open file for reading and appending in synchronous mode.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created if it does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'r'`: Open file for reading.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
An exception occurs if the file does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'r+'`: Open file for reading and writing.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
An exception occurs if the file does not exist.
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'rs+'`: Open file for reading and writing in synchronous mode. Instructs
|
2018-04-15 03:50:48 +02:00
|
|
|
|
the operating system to bypass the local file system cache.
|
|
|
|
|
|
|
|
|
|
This is primarily useful for opening files on NFS mounts as it allows
|
|
|
|
|
skipping the potentially stale local cache. It has a very real impact on
|
|
|
|
|
I/O performance so using this flag is not recommended unless it is needed.
|
|
|
|
|
|
2018-07-04 01:51:28 +02:00
|
|
|
|
This doesn't turn `fs.open()` or `fsPromises.open()` into a synchronous
|
|
|
|
|
blocking call. If synchronous operation is desired, something like
|
|
|
|
|
`fs.openSync()` should be used.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'w'`: Open file for writing.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created (if it does not exist) or truncated (if it exists).
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'wx'`: Like `'w'` but fails if the path exists.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'w+'`: Open file for reading and writing.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
The file is created (if it does not exist) or truncated (if it exists).
|
|
|
|
|
|
2019-10-24 06:28:42 +02:00
|
|
|
|
* `'wx+'`: Like `'w+'` but fails if the path exists.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
|
|
|
|
`flag` can also be a number as documented by open(2); commonly used constants
|
|
|
|
|
are available from `fs.constants`. On Windows, flags are translated to
|
|
|
|
|
their equivalent ones where applicable, e.g. `O_WRONLY` to `FILE_GENERIC_WRITE`,
|
2018-04-29 19:46:41 +02:00
|
|
|
|
or `O_EXCL|O_CREAT` to `CREATE_NEW`, as accepted by `CreateFileW`.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
|
|
|
|
The exclusive flag `'x'` (`O_EXCL` flag in open(2)) ensures that path is newly
|
|
|
|
|
created. On POSIX systems, path is considered to exist even if it is a symlink
|
|
|
|
|
to a non-existent file. The exclusive flag may or may not work with network
|
|
|
|
|
file systems.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2019-04-02 01:21:36 +02:00
|
|
|
|
Modifying a file rather than replacing it may require the `flag` option to be
|
|
|
|
|
set to `'r+'` rather than the default `'w'`.
|
2018-04-15 03:50:48 +02:00
|
|
|
|
|
|
|
|
|
The behavior of some flags are platform-specific. As such, opening a directory
|
2019-10-24 06:28:42 +02:00
|
|
|
|
on macOS and Linux with the `'a+'` flag, as in the example below, will return an
|
2018-04-15 03:50:48 +02:00
|
|
|
|
error. In contrast, on Windows and FreeBSD, a file descriptor or a `FileHandle`
|
|
|
|
|
will be returned.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// macOS and Linux
|
|
|
|
|
fs.open('<directory>', 'a+', (err, fd) => {
|
|
|
|
|
// => [Error: EISDIR: illegal operation on a directory, open <directory>]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Windows and FreeBSD
|
|
|
|
|
fs.open('<directory>', 'a+', (err, fd) => {
|
|
|
|
|
// => null, <fd>
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
On Windows, opening an existing hidden file using the `'w'` flag (either
|
|
|
|
|
through `fs.open()` or `fs.writeFile()` or `fsPromises.open()`) will fail with
|
|
|
|
|
`EPERM`. Existing hidden files can be opened for writing with the `'r+'` flag.
|
|
|
|
|
|
2018-09-01 00:19:09 +02:00
|
|
|
|
A call to `fs.ftruncate()` or `filehandle.truncate()` can be used to reset
|
2018-04-15 03:50:48 +02:00
|
|
|
|
the file contents.
|
2017-05-08 18:30:13 +02:00
|
|
|
|
|
|
|
|
|
[`AHAFS`]: https://www.ibm.com/developerworks/aix/library/au-aix_event_infrastructure/
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`Buffer.byteLength`]: buffer.html#buffer_class_method_buffer_bytelength_string_encoding
|
|
|
|
|
[`Buffer`]: buffer.html#buffer_buffer
|
2018-05-24 23:05:16 +02:00
|
|
|
|
[`FSEvents`]: https://developer.apple.com/documentation/coreservices/file_system_events
|
2019-10-09 15:10:19 +02:00
|
|
|
|
[`Number.MAX_SAFE_INTEGER`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
|
2018-07-14 14:10:10 +02:00
|
|
|
|
[`ReadDirectoryChangesW`]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`ReadStream`]: #fs_class_fs_readstream
|
2019-08-11 15:29:30 +02:00
|
|
|
|
[Readable Stream]: #stream_class_stream_readable
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`URL`]: url.html#url_the_whatwg_url_api
|
2017-08-23 23:59:06 +02:00
|
|
|
|
[`UV_THREADPOOL_SIZE`]: cli.html#cli_uv_threadpool_size_size
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`WriteStream`]: #fs_class_fs_writestream
|
2019-09-04 08:39:05 +02:00
|
|
|
|
[`event ports`]: https://illumos.org/man/port_create
|
2020-01-07 04:21:54 +01:00
|
|
|
|
[`filehandle.writeFile()`]: #fs_filehandle_writefile_data_options
|
2019-08-28 02:14:27 +02:00
|
|
|
|
[`fs.Dir`]: #fs_class_fs_dir
|
2018-07-28 04:29:32 +02:00
|
|
|
|
[`fs.Dirent`]: #fs_class_fs_dirent
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`fs.FSWatcher`]: #fs_class_fs_fswatcher
|
|
|
|
|
[`fs.Stats`]: #fs_class_fs_stats
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.access()`]: #fs_fs_access_path_mode_callback
|
2017-12-14 12:56:38 +01:00
|
|
|
|
[`fs.chmod()`]: #fs_fs_chmod_path_mode_callback
|
|
|
|
|
[`fs.chown()`]: #fs_fs_chown_path_uid_gid_callback
|
2019-09-26 00:34:05 +02:00
|
|
|
|
[`fs.copyFile()`]: #fs_fs_copyfile_src_dest_mode_callback
|
2018-09-26 15:15:54 +02:00
|
|
|
|
[`fs.createWriteStream()`]: #fs_fs_createwritestream_path_options
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.exists()`]: fs.html#fs_fs_exists_path_callback
|
2018-04-23 11:14:56 +02:00
|
|
|
|
[`fs.fstat()`]: #fs_fs_fstat_fd_options_callback
|
2018-06-10 15:28:00 +02:00
|
|
|
|
[`fs.ftruncate()`]: #fs_fs_ftruncate_fd_len_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.futimes()`]: #fs_fs_futimes_fd_atime_mtime_callback
|
2018-04-23 11:14:56 +02:00
|
|
|
|
[`fs.lstat()`]: #fs_fs_lstat_path_options_callback
|
2020-05-14 11:54:00 +02:00
|
|
|
|
[`fs.lutimes()`]: #fs_fs_lutimes_path_atime_mtime_callback
|
2018-08-10 01:52:41 +02:00
|
|
|
|
[`fs.mkdir()`]: #fs_fs_mkdir_path_options_callback
|
2016-10-31 14:59:08 +01:00
|
|
|
|
[`fs.mkdtemp()`]: #fs_fs_mkdtemp_prefix_options_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.open()`]: #fs_fs_open_path_flags_mode_callback
|
2019-08-28 02:14:27 +02:00
|
|
|
|
[`fs.opendir()`]: #fs_fs_opendir_path_options_callback
|
|
|
|
|
[`fs.opendirSync()`]: #fs_fs_opendirsync_path_options
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.read()`]: #fs_fs_read_fd_buffer_offset_length_position_callback
|
2017-06-09 14:14:51 +02:00
|
|
|
|
[`fs.readFile()`]: #fs_fs_readfile_path_options_callback
|
|
|
|
|
[`fs.readFileSync()`]: #fs_fs_readfilesync_path_options
|
2018-11-27 20:49:21 +01:00
|
|
|
|
[`fs.readdir()`]: #fs_fs_readdir_path_options_callback
|
|
|
|
|
[`fs.readdirSync()`]: #fs_fs_readdirsync_path_options
|
2020-03-16 14:50:27 +01:00
|
|
|
|
[`fs.readv()`]: #fs_fs_readv_fd_buffers_position_callback
|
2018-05-25 01:25:18 +02:00
|
|
|
|
[`fs.realpath()`]: #fs_fs_realpath_path_options_callback
|
2019-08-16 19:17:21 +02:00
|
|
|
|
[`fs.rmdir()`]: #fs_fs_rmdir_path_options_callback
|
2018-04-23 11:14:56 +02:00
|
|
|
|
[`fs.stat()`]: #fs_fs_stat_path_options_callback
|
2018-06-10 15:28:00 +02:00
|
|
|
|
[`fs.symlink()`]: #fs_fs_symlink_target_path_type_callback
|
2017-06-11 08:00:52 +02:00
|
|
|
|
[`fs.utimes()`]: #fs_fs_utimes_path_atime_mtime_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.watch()`]: #fs_fs_watch_filename_options_listener
|
2018-08-19 17:24:56 +02:00
|
|
|
|
[`fs.write(fd, buffer...)`]: #fs_fs_write_fd_buffer_offset_length_position_callback
|
|
|
|
|
[`fs.write(fd, string...)`]: #fs_fs_write_fd_string_position_encoding_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`fs.writeFile()`]: #fs_fs_writefile_file_data_options_callback
|
2019-02-04 17:18:39 +01:00
|
|
|
|
[`fs.writev()`]: #fs_fs_writev_fd_buffers_position_callback
|
2020-01-07 04:21:54 +01:00
|
|
|
|
[`fsPromises.open()`]: #fs_fspromises_open_path_flags_mode
|
2019-08-28 02:14:27 +02:00
|
|
|
|
[`fsPromises.opendir()`]: #fs_fspromises_opendir_path_options
|
2020-05-14 11:54:00 +02:00
|
|
|
|
[`fsPromises.utimes()`]: #fs_fspromises_utimes_path_atime_mtime
|
2018-05-20 19:16:19 +02:00
|
|
|
|
[`inotify(7)`]: http://man7.org/linux/man-pages/man7/inotify.7.html
|
|
|
|
|
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`net.Socket`]: net.html#net_class_net_socket
|
2018-07-01 23:38:05 +02:00
|
|
|
|
[`stat()`]: fs.html#fs_fs_stat_path_options_callback
|
2017-04-16 21:19:36 +02:00
|
|
|
|
[`util.promisify()`]: util.html#util_util_promisify_original
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[Caveats]: #fs_caveats
|
|
|
|
|
[Common System Errors]: errors.html#errors_common_system_errors
|
2020-06-14 23:49:34 +02:00
|
|
|
|
[FS constants]: #fs_fs_constants_1
|
|
|
|
|
[File access constants]: #fs_file_access_constants
|
2018-07-14 14:10:10 +02:00
|
|
|
|
[MDN-Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
2017-05-23 18:15:56 +02:00
|
|
|
|
[MDN-Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
|
2018-07-01 23:38:05 +02:00
|
|
|
|
[MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths
|
2018-11-27 20:49:21 +01:00
|
|
|
|
[MSDN-Using-Streams]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams
|
|
|
|
|
[Naming Files, Paths, and Namespaces]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
|
2019-10-09 15:10:19 +02:00
|
|
|
|
[bigints]: https://tc39.github.io/proposal-bigint
|
2018-11-27 20:49:21 +01:00
|
|
|
|
[chcp]: https://ss64.com/nt/chcp.html
|
2016-10-05 15:43:55 +02:00
|
|
|
|
[inode]: https://en.wikipedia.org/wiki/Inode
|
2018-04-15 03:50:48 +02:00
|
|
|
|
[support of file system `flags`]: #fs_file_system_flags
|
2020-01-15 18:47:43 +01:00
|
|
|
|
[Writable Stream]: stream.html#stream_class_stream_writable
|