Skip to content

Commit 3b1d911

Browse files
thelostone-mcMylesBorins
authored andcommitted
doc: update readFileSync in fs.md
* Updated fs.md stating fs.readFileAsync is platform specific * Fix formatting of `note`s PR-URL: #12800 Refs: #10962 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent fc9e7a9 commit 3b1d911

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

doc/api/fs.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ fs.appendFile('message.txt', 'data to append', 'utf8', callback);
479479

480480
Any specified file descriptor has to have been opened for appending.
481481

482-
_Note: If a file descriptor is specified as the `file`, it will not be closed
483-
automatically._
482+
*Note*: If a file descriptor is specified as the `file`, it will not be closed
483+
automatically.
484484

485485
## fs.appendFileSync(file, data[, options])
486486
<!-- YAML
@@ -1248,10 +1248,10 @@ On Linux, positional writes don't work when the file is opened in append mode.
12481248
The kernel ignores the position argument and always appends the data to
12491249
the end of the file.
12501250

1251-
_Note: The behavior of `fs.open()` is platform specific for some flags. As such,
1251+
*Note*: The behavior of `fs.open()` is platform-specific for some flags. As such,
12521252
opening a directory on macOS and Linux with the `'a+'` flag - see example
12531253
below - will return an error. In contrast, on Windows and FreeBSD, a file
1254-
descriptor will be returned._
1254+
descriptor will be returned.
12551255

12561256
```js
12571257
// macOS and Linux
@@ -1368,11 +1368,27 @@ If `options` is a string, then it specifies the encoding. Example:
13681368
```js
13691369
fs.readFile('/etc/passwd', 'utf8', callback);
13701370
```
1371+
*Note*: When the path is a directory, the behavior of
1372+
`fs.readFile()` and [`fs.readFileSync()`][] is platform-specific. On macOS,
1373+
Linux, and Windows, an error will be returned. On FreeBSD, a representation
1374+
of the directory's contents will be returned.
1375+
1376+
```js
1377+
// macOS, Linux and Windows
1378+
fs.readFile('<directory>', (err, data) => {
1379+
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
1380+
});
1381+
1382+
// FreeBSD
1383+
fs.readFile('<directory>', (err, data) => {
1384+
// => null, <data>
1385+
});
1386+
```
13711387

13721388
Any specified file descriptor has to support reading.
13731389

1374-
_Note: If a file descriptor is specified as the `file`, it will not be closed
1375-
automatically._
1390+
*Note*: If a file descriptor is specified as the `path`, it will not be closed
1391+
automatically.
13761392

13771393
## fs.readFileSync(file[, options])
13781394
<!-- YAML
@@ -1389,6 +1405,18 @@ Synchronous version of [`fs.readFile`][]. Returns the contents of the `file`.
13891405
If the `encoding` option is specified then this function returns a
13901406
string. Otherwise it returns a buffer.
13911407

1408+
*Note*: Similar to [`fs.readFile()`][], when the path is a directory, the
1409+
behavior of `fs.readFileSync()` is platform-specific.
1410+
1411+
```js
1412+
// macOS, Linux and Windows
1413+
fs.readFileSync('<directory>');
1414+
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
1415+
1416+
// FreeBSD
1417+
fs.readFileSync('<directory>'); // => null, <data>
1418+
```
1419+
13921420
## fs.readlink(path[, options], callback)
13931421
<!-- YAML
13941422
added: v0.1.31
@@ -1641,9 +1669,9 @@ have effectively stopped watching `filename`.
16411669
Calling `fs.unwatchFile()` with a filename that is not being watched is a
16421670
no-op, not an error.
16431671

1644-
_Note: [`fs.watch()`][] is more efficient than `fs.watchFile()` and `fs.unwatchFile()`.
1672+
*Note*: [`fs.watch()`][] is more efficient than `fs.watchFile()` and `fs.unwatchFile()`.
16451673
`fs.watch()` should be used instead of `fs.watchFile()` and `fs.unwatchFile()`
1646-
when possible._
1674+
when possible.
16471675

16481676
## fs.utimes(path, atime, mtime, callback)
16491677
<!-- YAML
@@ -1817,15 +1845,15 @@ These stat objects are instances of `fs.Stat`.
18171845
If you want to be notified when the file was modified, not just accessed,
18181846
you need to compare `curr.mtime` and `prev.mtime`.
18191847

1820-
_Note: when an `fs.watchFile` operation results in an `ENOENT` error, it will
1848+
*Note*: when an `fs.watchFile` operation results in an `ENOENT` error, it will
18211849
invoke the listener once, with all the fields zeroed (or, for dates, the Unix
18221850
Epoch). In Windows, `blksize` and `blocks` fields will be `undefined`, instead
18231851
of zero. If the file is created later on, the listener will be called again,
1824-
with the latest stat objects. This is a change in functionality since v0.10._
1852+
with the latest stat objects. This is a change in functionality since v0.10.
18251853

1826-
_Note: [`fs.watch()`][] is more efficient than `fs.watchFile` and
1854+
*Note*: [`fs.watch()`][] is more efficient than `fs.watchFile` and
18271855
`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and
1828-
`fs.unwatchFile` when possible._
1856+
`fs.unwatchFile` when possible.
18291857

18301858
## fs.write(fd, buffer, offset, length[, position], callback)
18311859
<!-- YAML
@@ -1935,8 +1963,8 @@ Note that it is unsafe to use `fs.writeFile` multiple times on the same file
19351963
without waiting for the callback. For this scenario,
19361964
`fs.createWriteStream` is strongly recommended.
19371965

1938-
_Note: If a file descriptor is specified as the `file`, it will not be closed
1939-
automatically._
1966+
*Note*: If a file descriptor is specified as the `file`, it will not be closed
1967+
automatically.
19401968

19411969
## fs.writeFileSync(file, data[, options])
19421970
<!-- YAML

0 commit comments

Comments
 (0)