Skip to content

Latest commit

 

History

History
2855 lines (2324 loc) · 86 KB

File metadata and controls

2855 lines (2324 loc) · 86 KB
 
Feb 27, 2012
Feb 27, 2012
1
# File System
Oct 28, 2010
Oct 28, 2010
2
Aug 4, 2016
Aug 4, 2016
3
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
4
Mar 4, 2012
Mar 4, 2012
5
<!--name=fs-->
6
Oct 28, 2010
Oct 28, 2010
7
File I/O is provided by simple wrappers around standard POSIX functions. To
8
use this module do `require('fs')`. All the methods have asynchronous and
Nov 21, 2010
Nov 21, 2010
9
synchronous forms.
Oct 28, 2010
Oct 28, 2010
10
Aug 26, 2015
Aug 26, 2015
11
The asynchronous form always takes a completion callback as its last argument.
Oct 28, 2010
Oct 28, 2010
12
The arguments passed to the completion callback depend on the method, but the
13
first argument is always reserved for an exception. If the operation was
14
completed successfully, then the first argument will be `null` or `undefined`.
15
Sep 6, 2011
Sep 6, 2011
16
When using the synchronous form any exceptions are immediately thrown.
Apr 28, 2017
Apr 28, 2017
17
Exceptions may be handled using `try`/`catch`, or they may be allowed to
18
bubble up.
Sep 6, 2011
Sep 6, 2011
19
Oct 28, 2010
Oct 28, 2010
20
Here is an example of the asynchronous version:
21
Jan 21, 2016
Jan 21, 2016
22
```js
23
const fs = require('fs');
Oct 28, 2010
Oct 28, 2010
24
Jan 21, 2016
Jan 21, 2016
25
fs.unlink('/tmp/hello', (err) => {
26
if (err) throw err;
27
console.log('successfully deleted /tmp/hello');
28
});
29
```
Oct 28, 2010
Oct 28, 2010
30
31
Here is the synchronous version:
32
Jan 21, 2016
Jan 21, 2016
33
```js
34
const fs = require('fs');
Oct 28, 2010
Oct 28, 2010
35
Jan 21, 2016
Jan 21, 2016
36
fs.unlinkSync('/tmp/hello');
37
console.log('successfully deleted /tmp/hello');
38
```
Oct 28, 2010
Oct 28, 2010
39
40
With the asynchronous methods there is no guaranteed ordering. So the
41
following is prone to error:
42
Jan 21, 2016
Jan 21, 2016
43
```js
44
fs.rename('/tmp/hello', '/tmp/world', (err) => {
45
if (err) throw err;
46
console.log('renamed complete');
47
});
48
fs.stat('/tmp/world', (err, stats) => {
49
if (err) throw err;
50
console.log(`stats: ${JSON.stringify(stats)}`);
51
});
52
```
Oct 28, 2010
Oct 28, 2010
53
54
It could be that `fs.stat` is executed before `fs.rename`.
55
The correct way to do this is to chain the callbacks.
56
Jan 21, 2016
Jan 21, 2016
57
```js
58
fs.rename('/tmp/hello', '/tmp/world', (err) => {
59
if (err) throw err;
60
fs.stat('/tmp/world', (err, stats) => {
61
if (err) throw err;
62
console.log(`stats: ${JSON.stringify(stats)}`);
63
});
64
});
65
```
Oct 28, 2010
Oct 28, 2010
66
67
In busy processes, the programmer is _strongly encouraged_ to use the
68
asynchronous versions of these calls. The synchronous versions will block
69
the entire process until they complete--halting all connections.
70
Aug 26, 2015
Aug 26, 2015
71
The relative path to a filename can be used. Remember, however, that this path
72
will be relative to `process.cwd()`.
Dec 4, 2012
Dec 4, 2012
73
Apr 28, 2017
Apr 28, 2017
74
While it is not recommended, most fs functions allow the callback argument to
75
be omitted, in which case a default callback is used that rethrows errors. To
76
get a trace to the original call site, set the `NODE_DEBUG` environment
77
variable:
78
79
*Note*: Omitting the callback function on asynchronous fs functions is
80
deprecated and may result in an error being thrown in the future.
Mar 13, 2013
Mar 13, 2013
81
Jul 14, 2016
Jul 14, 2016
82
```txt
Jan 21, 2016
Jan 21, 2016
83
$ cat script.js
84
function bad() {
85
require('fs').readFile('/');
86
}
87
bad();
88
89
$ env NODE_DEBUG=fs node script.js
May 15, 2016
May 15, 2016
90
fs.js:88
91
throw backtrace;
92
^
93
Error: EISDIR: illegal operation on a directory, read
94
<stack trace.>
Jan 21, 2016
Jan 21, 2016
95
```
Dec 4, 2012
Dec 4, 2012
96
Jun 6, 2017
Jun 6, 2017
97
*Note:* On Windows Node.js follows the concept of per-drive working directory.
98
This behavior can be observed when using a drive path without a backslash. For
99
example `fs.readdirSync('c:\\')` can potentially return a different result than
100
`fs.readdirSync('c:')`. For more information, see
101
[this MSDN page][MSDN-Rel-Path].
102
May 4, 2017
May 4, 2017
103
## WHATWG URL object support
104
<!-- YAML
105
added: v7.6.0
106
-->
107
108
> Stability: 1 - Experimental
109
110
For most `fs` module functions, the `path` or `filename` argument may be passed
111
as a WHATWG [`URL`][] object. Only [`URL`][] objects using the `file:` protocol
112
are supported.
113
114
```js
115
const fs = require('fs');
116
const { URL } = require('url');
117
const fileUrl = new URL('file:///tmp/hello');
118
119
fs.readFileSync(fileUrl);
120
```
121
122
*Note*: `file:` URLs are always absolute paths.
123
124
Using WHATWG [`URL`][] objects might introduce platform-specific behaviors.
125
126
On Windows, `file:` URLs with a hostname convert to UNC paths, while `file:`
127
URLs with drive letters convert to local absolute paths. `file:` URLs without a
128
hostname nor a drive letter will result in a throw :
129
130
```js
131
// On Windows :
132
133
// - WHATWG file URLs with hostname convert to UNC path
134
// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file
135
fs.readFileSync(new URL('file://hostname/p/a/t/h/file'));
136
137
// - WHATWG file URLs with drive letters convert to absolute path
138
// file:///C:/tmp/hello => C:\tmp\hello
139
fs.readFileSync(new URL('file:///C:/tmp/hello'));
140
141
// - WHATWG file URLs without hostname must have a drive letters
142
fs.readFileSync(new URL('file:///notdriveletter/p/a/t/h/file'));
143
fs.readFileSync(new URL('file:///c/p/a/t/h/file'));
144
// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute
145
```
146
147
*Note*: `file:` URLs with drive letters must use `:` as a separator just after
148
the drive letter. Using another separator will result in a throw.
149
150
On all other platforms, `file:` URLs with a hostname are unsupported and will
151
result in a throw:
152
153
```js
154
// On other platforms:
155
156
// - WHATWG file URLs with hostname are unsupported
157
// file://hostname/p/a/t/h/file => throw!
158
fs.readFileSync(new URL('file://hostname/p/a/t/h/file'));
159
// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute
160
161
// - WHATWG file URLs convert to absolute path
162
// file:///tmp/hello => /tmp/hello
163
fs.readFileSync(new URL('file:///tmp/hello'));
164
```
165
166
A `file:` URL having encoded slash characters will result in a throw on all
167
platforms:
168
169
```js
170
// On Windows
171
fs.readFileSync(new URL('file:///C:/p/a/t/h/%2F'));
172
fs.readFileSync(new URL('file:///C:/p/a/t/h/%2f'));
173
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
174
\ or / characters */
175
176
// On POSIX
177
fs.readFileSync(new URL('file:///p/a/t/h/%2F'));
178
fs.readFileSync(new URL('file:///p/a/t/h/%2f'));
179
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
180
/ characters */
181
```
182
On Windows, `file:` URLs having encoded backslash will result in a throw:
183
184
```js
185
// On Windows
186
fs.readFileSync(new URL('file:///C:/path/%5C'));
187
fs.readFileSync(new URL('file:///C:/path/%5c'));
188
/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
189
\ or / characters */
190
```
191
Apr 18, 2016
Apr 18, 2016
192
## Buffer API
May 17, 2016
May 17, 2016
193
<!-- YAML
194
added: v6.0.0
195
-->
Apr 18, 2016
Apr 18, 2016
196
197
`fs` functions support passing and receiving paths as both strings
198
and Buffers. The latter is intended to make it possible to work with
199
filesystems that allow for non-UTF-8 filenames. For most typical
200
uses, working with paths as Buffers will be unnecessary, as the string
201
API converts to and from UTF-8 automatically.
202
May 25, 2017
May 25, 2017
203
*Note*: On certain file systems (such as NTFS and HFS+) filenames
Apr 20, 2016
Apr 20, 2016
204
will always be encoded as UTF-8. On such file systems, passing
Apr 18, 2016
Apr 18, 2016
205
non-UTF-8 encoded Buffers to `fs` functions will not work as expected.
206
Nov 13, 2015
Nov 13, 2015
207
## Class: fs.FSWatcher
May 17, 2016
May 17, 2016
208
<!-- YAML
209
added: v0.5.8
210
-->
Jul 15, 2011
Jul 15, 2011
211
Jul 14, 2016
Jul 14, 2016
212
Objects returned from [`fs.watch()`][] are of this type.
213
214
The `listener` callback provided to `fs.watch()` receives the returned FSWatcher's
215
`change` events.
216
217
The object itself emits these events:
Oct 28, 2010
Oct 28, 2010
218
Nov 13, 2015
Nov 13, 2015
219
### Event: 'change'
May 17, 2016
May 17, 2016
220
<!-- YAML
221
added: v0.5.8
222
-->
Oct 28, 2010
Oct 28, 2010
223
Mar 2, 2017
Mar 2, 2017
224
* `eventType` {string} The type of fs change
Mar 8, 2017
Mar 8, 2017
225
* `filename` {string|Buffer} The filename that changed (if relevant/available)
Oct 28, 2010
Oct 28, 2010
226
Nov 13, 2015
Nov 13, 2015
227
Emitted when something changes in a watched directory or file.
Dec 3, 2015
Dec 3, 2015
228
See more details in [`fs.watch()`][].
Oct 28, 2010
Oct 28, 2010
229
Mar 25, 2016
Mar 25, 2016
230
The `filename` argument may not be provided depending on operating system
231
support. If `filename` is provided, it will be provided as a `Buffer` if
Oct 13, 2016
Oct 13, 2016
232
`fs.watch()` is called with its `encoding` option set to `'buffer'`, otherwise
Mar 25, 2016
Mar 25, 2016
233
`filename` will be a string.
234
235
```js
Jul 14, 2016
Jul 14, 2016
236
// Example when handled through fs.watch listener
Jun 2, 2017
Jun 2, 2017
237
fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
Mar 25, 2016
Mar 25, 2016
238
if (filename)
239
console.log(filename);
240
// Prints: <Buffer ...>
241
});
242
```
243
Nov 13, 2015
Nov 13, 2015
244
### Event: 'error'
May 17, 2016
May 17, 2016
245
<!-- YAML
246
added: v0.5.8
247
-->
Oct 28, 2010
Oct 28, 2010
248
Feb 9, 2016
Feb 9, 2016
249
* `error` {Error}
Oct 28, 2010
Oct 28, 2010
250
Nov 13, 2015
Nov 13, 2015
251
Emitted when an error occurs.
Oct 28, 2010
Oct 28, 2010
252
Nov 13, 2015
Nov 13, 2015
253
### watcher.close()
May 17, 2016
May 17, 2016
254
<!-- YAML
255
added: v0.5.8
256
-->
Oct 28, 2010
Oct 28, 2010
257
Nov 13, 2015
Nov 13, 2015
258
Stop watching for changes on the given `fs.FSWatcher`.
Aug 6, 2012
Aug 6, 2012
259
Nov 13, 2015
Nov 13, 2015
260
## Class: fs.ReadStream
May 17, 2016
May 17, 2016
261
<!-- YAML
262
added: v0.1.93
263
-->
Aug 6, 2012
Aug 6, 2012
264
Nov 16, 2015
Nov 16, 2015
265
`ReadStream` is a [Readable Stream][].
Aug 6, 2012
Aug 6, 2012
266
Feb 20, 2017
Feb 20, 2017
267
### Event: 'close'
May 17, 2016
May 17, 2016
268
<!-- YAML
269
added: v0.1.93
270
-->
Aug 6, 2012
Aug 6, 2012
271
Feb 20, 2017
Feb 20, 2017
272
Emitted when the `ReadStream`'s underlying file descriptor has been closed
273
using the `fs.close()` method.
Apr 20, 2011
Apr 20, 2011
274
Feb 20, 2017
Feb 20, 2017
275
### Event: 'open'
May 17, 2016
May 17, 2016
276
<!-- YAML
277
added: v0.1.93
278
-->
May 3, 2016
May 3, 2016
279
Mar 8, 2017
Mar 8, 2017
280
* `fd` {integer} Integer file descriptor used by the ReadStream.
Feb 20, 2017
Feb 20, 2017
281
282
Emitted when the ReadStream's file is opened.
May 3, 2016
May 3, 2016
283
Aug 8, 2016
Aug 8, 2016
284
### readStream.bytesRead
285
<!-- YAML
Aug 16, 2016
Aug 16, 2016
286
added: 6.4.0
Aug 8, 2016
Aug 8, 2016
287
-->
288
289
The number of bytes read so far.
290
Jan 15, 2016
Jan 15, 2016
291
### readStream.path
May 17, 2016
May 17, 2016
292
<!-- YAML
293
added: v0.1.93
294
-->
Jan 15, 2016
Jan 15, 2016
295
Mar 25, 2016
Mar 25, 2016
296
The path to the file the stream is reading from as specified in the first
297
argument to `fs.createReadStream()`. If `path` is passed as a string, then
298
`readStream.path` will be a string. If `path` is passed as a `Buffer`, then
299
`readStream.path` will be a `Buffer`.
Jan 15, 2016
Jan 15, 2016
300
Nov 13, 2015
Nov 13, 2015
301
## Class: fs.Stats
May 17, 2016
May 17, 2016
302
<!-- YAML
303
added: v0.1.21
Jun 7, 2017
Jun 7, 2017
304
changes:
305
- version: REPLACEME
306
pr-url: https://github.com/nodejs/node/pull/13173
307
description: Added times as numbers.
May 17, 2016
May 17, 2016
308
-->
Apr 20, 2011
Apr 20, 2011
309
Jun 7, 2017
Jun 7, 2017
310
Objects returned from [`fs.stat()`][], [`fs.lstat()`][] and [`fs.fstat()`][] and
311
their synchronous counterparts are of this type.
Apr 20, 2011
Apr 20, 2011
312
Nov 13, 2015
Nov 13, 2015
313
- `stats.isFile()`
314
- `stats.isDirectory()`
315
- `stats.isBlockDevice()`
316
- `stats.isCharacterDevice()`
Dec 3, 2015
Dec 3, 2015
317
- `stats.isSymbolicLink()` (only valid with [`fs.lstat()`][])
Nov 13, 2015
Nov 13, 2015
318
- `stats.isFIFO()`
319
- `stats.isSocket()`
Apr 20, 2011
Apr 20, 2011
320
Dec 3, 2015
Dec 3, 2015
321
For a regular file [`util.inspect(stats)`][] would return a string very
Nov 13, 2015
Nov 13, 2015
322
similar to this:
Apr 20, 2011
Apr 20, 2011
323
May 28, 2017
May 28, 2017
324
```console
Mar 27, 2017
Mar 27, 2017
325
Stats {
Jan 21, 2016
Jan 21, 2016
326
dev: 2114,
327
ino: 48064969,
328
mode: 33188,
329
nlink: 1,
330
uid: 85,
331
gid: 100,
332
rdev: 0,
333
size: 527,
334
blksize: 4096,
335
blocks: 8,
Jun 7, 2017
Jun 7, 2017
336
atimeMs: 1318289051000.1,
337
mtimeMs: 1318289051000.1,
338
ctimeMs: 1318289051000.1,
339
birthtimeMs: 1318289051000.1,
Jan 21, 2016
Jan 21, 2016
340
atime: Mon, 10 Oct 2011 23:24:11 GMT,
341
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
342
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
Mar 27, 2017
Mar 27, 2017
343
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
Jan 21, 2016
Jan 21, 2016
344
```
Apr 20, 2011
Apr 20, 2011
345
Jun 7, 2017
Jun 7, 2017
346
*Note*: `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs` are [numbers][MDN-Number]
347
that hold the corresponding times in milliseconds. Their precision is platform
348
specific. `atime`, `mtime`, `ctime`, and `birthtime` are [`Date`][MDN-Date]
349
object alternate representations of the various times. The `Date` and number
350
values are not connected. Assigning a new number value, or mutating the `Date`
351
value, will not be reflected in the corresponding alternate representation.
352
Apr 20, 2011
Apr 20, 2011
353
Nov 13, 2015
Nov 13, 2015
354
### Stat Time Values
Apr 20, 2011
Apr 20, 2011
355
Nov 13, 2015
Nov 13, 2015
356
The times in the stat object have the following semantics:
Apr 20, 2011
Apr 20, 2011
357
Nov 13, 2015
Nov 13, 2015
358
* `atime` "Access Time" - Time when file data last accessed. Changed
Nov 19, 2016
Nov 19, 2016
359
by the mknod(2), utimes(2), and read(2) system calls.
Nov 13, 2015
Nov 13, 2015
360
* `mtime` "Modified Time" - Time when file data last modified.
Nov 19, 2016
Nov 19, 2016
361
Changed by the mknod(2), utimes(2), and write(2) system calls.
Nov 13, 2015
Nov 13, 2015
362
* `ctime` "Change Time" - Time when file status was last changed
Nov 19, 2016
Nov 19, 2016
363
(inode data modification). Changed by the chmod(2), chown(2),
364
link(2), mknod(2), rename(2), unlink(2), utimes(2),
365
read(2), and write(2) system calls.
Nov 13, 2015
Nov 13, 2015
366
* `birthtime` "Birth Time" - Time of file creation. Set once when the
367
file is created. On filesystems where birthtime is not available,
368
this field may instead hold either the `ctime` or
Mar 14, 2016
Mar 14, 2016
369
`1970-01-01T00:00Z` (ie, unix epoch timestamp `0`). Note that this
370
value may be greater than `atime` or `mtime` in this case. On Darwin
371
and other FreeBSD variants, also set if the `atime` is explicitly
372
set to an earlier value than the current `birthtime` using the
Nov 19, 2016
Nov 19, 2016
373
utimes(2) system call.
Apr 20, 2011
Apr 20, 2011
374
Nov 13, 2015
Nov 13, 2015
375
Prior to Node v0.12, the `ctime` held the `birthtime` on Windows
376
systems. Note that as of v0.12, `ctime` is not "creation time", and
377
on Unix systems, it never was.
Oct 28, 2010
Oct 28, 2010
378
Nov 13, 2015
Nov 13, 2015
379
## Class: fs.WriteStream
May 17, 2016
May 17, 2016
380
<!-- YAML
381
added: v0.1.93
382
-->
Oct 28, 2010
Oct 28, 2010
383
Nov 16, 2015
Nov 16, 2015
384
`WriteStream` is a [Writable Stream][].
Oct 28, 2010
Oct 28, 2010
385
Feb 20, 2017
Feb 20, 2017
386
### Event: 'close'
May 17, 2016
May 17, 2016
387
<!-- YAML
388
added: v0.1.93
389
-->
Nov 21, 2010
Nov 21, 2010
390
Feb 20, 2017
Feb 20, 2017
391
Emitted when the `WriteStream`'s underlying file descriptor has been closed
392
using the `fs.close()` method.
Apr 20, 2011
Apr 20, 2011
393
Feb 20, 2017
Feb 20, 2017
394
### Event: 'open'
May 17, 2016
May 17, 2016
395
<!-- YAML
396
added: v0.1.93
397
-->
May 3, 2016
May 3, 2016
398
Mar 8, 2017
Mar 8, 2017
399
* `fd` {integer} Integer file descriptor used by the WriteStream.
Feb 20, 2017
Feb 20, 2017
400
401
Emitted when the WriteStream's file is opened.
May 3, 2016
May 3, 2016
402
Nov 13, 2015
Nov 13, 2015
403
### writeStream.bytesWritten
May 17, 2016
May 17, 2016
404
<!-- YAML
405
added: v0.4.7
406
-->
Apr 20, 2011
Apr 20, 2011
407
Nov 13, 2015
Nov 13, 2015
408
The number of bytes written so far. Does not include data that is still queued
409
for writing.
Apr 20, 2011
Apr 20, 2011
410
Jan 15, 2016
Jan 15, 2016
411
### writeStream.path
May 17, 2016
May 17, 2016
412
<!-- YAML
413
added: v0.1.93
414
-->
Jan 15, 2016
Jan 15, 2016
415
Mar 25, 2016
Mar 25, 2016
416
The path to the file the stream is writing to as specified in the first
417
argument to `fs.createWriteStream()`. If `path` is passed as a string, then
418
`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then
419
`writeStream.path` will be a `Buffer`.
Jan 15, 2016
Jan 15, 2016
420
Nov 13, 2015
Nov 13, 2015
421
## fs.access(path[, mode], callback)
May 17, 2016
May 17, 2016
422
<!-- YAML
Jun 18, 2016
Jun 18, 2016
423
added: v0.11.15
May 4, 2017
May 4, 2017
424
changes:
425
- version: v7.6.0
426
pr-url: https://github.com/nodejs/node/pull/10739
427
description: The `path` parameter can be a WHATWG `URL` object using `file:`
428
protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
429
-->
Apr 20, 2011
Apr 20, 2011
430
May 4, 2017
May 4, 2017
431
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
432
* `mode` {integer}
Mar 25, 2016
Mar 25, 2016
433
* `callback` {Function}
434
Jun 6, 2016
Jun 6, 2016
435
Tests a user's permissions for the file or directory specified by `path`.
436
The `mode` argument is an optional integer that specifies the accessibility
437
checks to be performed. The following constants define the possible values of
438
`mode`. It is possible to create a mask consisting of the bitwise OR of two or
439
more values.
Apr 20, 2011
Apr 20, 2011
440
Jun 6, 2016
Jun 6, 2016
441
- `fs.constants.F_OK` - `path` is visible to the calling process. This is useful
May 17, 2016
May 17, 2016
442
for determining if a file exists, but says nothing about `rwx` permissions.
Nov 13, 2015
Nov 13, 2015
443
Default if no `mode` is specified.
Jun 6, 2016
Jun 6, 2016
444
- `fs.constants.R_OK` - `path` can be read by the calling process.
445
- `fs.constants.W_OK` - `path` can be written by the calling process.
446
- `fs.constants.X_OK` - `path` can be executed by the calling process. This has
447
no effect on Windows (will behave like `fs.constants.F_OK`).
Jun 30, 2012
Jun 30, 2012
448
Nov 13, 2015
Nov 13, 2015
449
The final argument, `callback`, is a callback function that is invoked with
450
a possible error argument. If any of the accessibility checks fail, the error
451
argument will be populated. The following example checks if the file
452
`/etc/passwd` can be read and written by the current process.
Apr 20, 2011
Apr 20, 2011
453
Jan 21, 2016
Jan 21, 2016
454
```js
May 17, 2016
May 17, 2016
455
fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
Jan 21, 2016
Jan 21, 2016
456
console.log(err ? 'no access!' : 'can read/write');
457
});
458
```
Apr 20, 2011
Apr 20, 2011
459
Sep 1, 2016
Sep 1, 2016
460
Using `fs.access()` to check for the accessibility of a file before calling
461
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
462
so introduces a race condition, since other processes may change the file's
463
state between the two calls. Instead, user code should open/read/write the
464
file directly and handle the error raised if the file is not accessible.
465
466
For example:
467
468
469
**write (NOT RECOMMENDED)**
470
471
```js
472
fs.access('myfile', (err) => {
473
if (!err) {
474
console.error('myfile already exists');
475
return;
476
}
477
478
fs.open('myfile', 'wx', (err, fd) => {
479
if (err) throw err;
480
writeMyData(fd);
481
});
482
});
483
```
484
485
**write (RECOMMENDED)**
486
487
```js
488
fs.open('myfile', 'wx', (err, fd) => {
489
if (err) {
Mar 27, 2017
Mar 27, 2017
490
if (err.code === 'EEXIST') {
Sep 1, 2016
Sep 1, 2016
491
console.error('myfile already exists');
492
return;
493
}
Mar 27, 2017
Mar 27, 2017
494
495
throw err;
Sep 1, 2016
Sep 1, 2016
496
}
497
498
writeMyData(fd);
499
});
500
```
501
502
**read (NOT RECOMMENDED)**
503
504
```js
505
fs.access('myfile', (err) => {
506
if (err) {
Mar 27, 2017
Mar 27, 2017
507
if (err.code === 'ENOENT') {
Sep 1, 2016
Sep 1, 2016
508
console.error('myfile does not exist');
509
return;
510
}
Mar 27, 2017
Mar 27, 2017
511
512
throw err;
Sep 1, 2016
Sep 1, 2016
513
}
514
515
fs.open('myfile', 'r', (err, fd) => {
516
if (err) throw err;
517
readMyData(fd);
518
});
519
});
520
```
521
522
**read (RECOMMENDED)**
523
524
```js
525
fs.open('myfile', 'r', (err, fd) => {
526
if (err) {
Mar 27, 2017
Mar 27, 2017
527
if (err.code === 'ENOENT') {
Sep 1, 2016
Sep 1, 2016
528
console.error('myfile does not exist');
529
return;
530
}
Mar 27, 2017
Mar 27, 2017
531
532
throw err;
Sep 1, 2016
Sep 1, 2016
533
}
534
535
readMyData(fd);
536
});
537
```
538
539
The "not recommended" examples above check for accessibility and then use the
540
file; the "recommended" examples are better because they use the file directly
541
and handle the error, if any.
542
543
In general, check for the accessibility of a file only if the file won’t be
544
used directly, for example when its accessibility is a signal from another
545
process.
546
Nov 13, 2015
Nov 13, 2015
547
## fs.accessSync(path[, mode])
May 17, 2016
May 17, 2016
548
<!-- YAML
Jun 18, 2016
Jun 18, 2016
549
added: v0.11.15
May 4, 2017
May 4, 2017
550
changes:
551
- version: v7.6.0
552
pr-url: https://github.com/nodejs/node/pull/10739
553
description: The `path` parameter can be a WHATWG `URL` object using `file:`
554
protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
555
-->
Oct 28, 2010
Oct 28, 2010
556
May 4, 2017
May 4, 2017
557
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
558
* `mode` {integer}
Mar 25, 2016
Mar 25, 2016
559
Jun 6, 2016
Jun 6, 2016
560
Synchronous version of [`fs.access()`][]. This throws if any accessibility
May 17, 2016
May 17, 2016
561
checks fail, and does nothing otherwise.
Oct 28, 2010
Oct 28, 2010
562
Nov 13, 2015
Nov 13, 2015
563
## fs.appendFile(file, data[, options], callback)
May 17, 2016
May 17, 2016
564
<!-- YAML
565
added: v0.6.7
Feb 24, 2017
Feb 24, 2017
566
changes:
567
- version: v7.0.0
568
pr-url: https://github.com/nodejs/node/pull/7897
569
description: The `callback` parameter is no longer optional. Not passing
May 20, 2017
May 20, 2017
570
it will emit a deprecation warning.
Feb 24, 2017
Feb 24, 2017
571
- version: v7.0.0
572
pr-url: https://github.com/nodejs/node/pull/7831
573
description: The passed `options` object will never be modified.
574
- version: v5.0.0
575
pr-url: https://github.com/nodejs/node/pull/3163
576
description: The `file` parameter can be a file descriptor now.
May 17, 2016
May 17, 2016
577
-->
Oct 28, 2010
Oct 28, 2010
578
Mar 8, 2017
Mar 8, 2017
579
* `file` {string|Buffer|number} filename or file descriptor
580
* `data` {string|Buffer}
581
* `options` {Object|string}
582
* `encoding` {string|null} default = `'utf8'`
583
* `mode` {integer} default = `0o666`
Mar 2, 2017
Mar 2, 2017
584
* `flag` {string} default = `'a'`
Nov 13, 2015
Nov 13, 2015
585
* `callback` {Function}
Oct 28, 2010
Oct 28, 2010
586
Nov 13, 2015
Nov 13, 2015
587
Asynchronously append data to a file, creating the file if it does not yet exist.
588
`data` can be a string or a buffer.
Oct 28, 2010
Oct 28, 2010
589
Nov 13, 2015
Nov 13, 2015
590
Example:
Oct 28, 2010
Oct 28, 2010
591
Jan 21, 2016
Jan 21, 2016
592
```js
593
fs.appendFile('message.txt', 'data to append', (err) => {
594
if (err) throw err;
595
console.log('The "data to append" was appended to file!');
596
});
597
```
Oct 28, 2010
Oct 28, 2010
598
Nov 13, 2015
Nov 13, 2015
599
If `options` is a string, then it specifies the encoding. Example:
Oct 28, 2010
Oct 28, 2010
600
Jan 21, 2016
Jan 21, 2016
601
```js
602
fs.appendFile('message.txt', 'data to append', 'utf8', callback);
603
```
Oct 28, 2010
Oct 28, 2010
604
Nov 13, 2015
Nov 13, 2015
605
Any specified file descriptor has to have been opened for appending.
Oct 28, 2010
Oct 28, 2010
606
May 5, 2017
May 5, 2017
607
*Note*: If a file descriptor is specified as the `file`, it will not be closed
608
automatically.
Oct 28, 2010
Oct 28, 2010
609
Nov 13, 2015
Nov 13, 2015
610
## fs.appendFileSync(file, data[, options])
May 17, 2016
May 17, 2016
611
<!-- YAML
612
added: v0.6.7
Feb 24, 2017
Feb 24, 2017
613
changes:
614
- version: v7.0.0
615
pr-url: https://github.com/nodejs/node/pull/7831
616
description: The passed `options` object will never be modified.
617
- version: v5.0.0
618
pr-url: https://github.com/nodejs/node/pull/3163
619
description: The `file` parameter can be a file descriptor now.
May 17, 2016
May 17, 2016
620
-->
Oct 28, 2010
Oct 28, 2010
621
Mar 8, 2017
Mar 8, 2017
622
* `file` {string|Buffer|number} filename or file descriptor
623
* `data` {string|Buffer}
624
* `options` {Object|string}
625
* `encoding` {string|null} default = `'utf8'`
626
* `mode` {integer} default = `0o666`
Mar 2, 2017
Mar 2, 2017
627
* `flag` {string} default = `'a'`
Mar 25, 2016
Mar 25, 2016
628
Dec 3, 2015
Dec 3, 2015
629
The synchronous version of [`fs.appendFile()`][]. Returns `undefined`.
Oct 28, 2010
Oct 28, 2010
630
Nov 13, 2015
Nov 13, 2015
631
## fs.chmod(path, mode, callback)
May 17, 2016
May 17, 2016
632
<!-- YAML
633
added: v0.1.30
Feb 24, 2017
Feb 24, 2017
634
changes:
May 4, 2017
May 4, 2017
635
- version: v7.6.0
636
pr-url: https://github.com/nodejs/node/pull/10739
637
description: The `path` parameter can be a WHATWG `URL` object using `file:`
638
protocol. Support is currently still *experimental*.
Feb 24, 2017
Feb 24, 2017
639
- version: v7.0.0
640
pr-url: https://github.com/nodejs/node/pull/7897
641
description: The `callback` parameter is no longer optional. Not passing
May 20, 2017
May 20, 2017
642
it will emit a deprecation warning.
May 17, 2016
May 17, 2016
643
-->
Oct 28, 2010
Oct 28, 2010
644
May 4, 2017
May 4, 2017
645
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
646
* `mode` {integer}
Mar 25, 2016
Mar 25, 2016
647
* `callback` {Function}
648
Nov 13, 2015
Nov 13, 2015
649
Asynchronous chmod(2). No arguments other than a possible exception are given
650
to the completion callback.
Oct 28, 2010
Oct 28, 2010
651
Nov 13, 2015
Nov 13, 2015
652
## fs.chmodSync(path, mode)
May 17, 2016
May 17, 2016
653
<!-- YAML
654
added: v0.6.7
May 4, 2017
May 4, 2017
655
changes:
656
- version: v7.6.0
657
pr-url: https://github.com/nodejs/node/pull/10739
658
description: The `path` parameter can be a WHATWG `URL` object using `file:`
659
protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
660
-->
Oct 28, 2010
Oct 28, 2010
661
May 4, 2017
May 4, 2017
662
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
663
* `mode` {integer}
Mar 25, 2016
Mar 25, 2016
664
Nov 13, 2015
Nov 13, 2015
665
Synchronous chmod(2). Returns `undefined`.
Oct 28, 2010
Oct 28, 2010
666
Nov 13, 2015
Nov 13, 2015
667
## fs.chown(path, uid, gid, callback)
May 17, 2016
May 17, 2016
668
<!-- YAML
669
added: v0.1.97
Feb 24, 2017
Feb 24, 2017
670
changes:
May 4, 2017
May 4, 2017
671
- version: v7.6.0
672
pr-url: https://github.com/nodejs/node/pull/10739
673
description: The `path` parameter can be a WHATWG `URL` object using `file:`
674
protocol. Support is currently still *experimental*.
Feb 24, 2017
Feb 24, 2017
675
- version: v7.0.0
676
pr-url: https://github.com/nodejs/node/pull/7897
677
description: The `callback` parameter is no longer optional. Not passing
May 20, 2017
May 20, 2017
678
it will emit a deprecation warning.
May 17, 2016
May 17, 2016
679
-->
Nov 13, 2015
Nov 13, 2015
680
May 4, 2017
May 4, 2017
681
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
682
* `uid` {integer}
683
* `gid` {integer}
Mar 25, 2016
Mar 25, 2016
684
* `callback` {Function}
685
Nov 13, 2015
Nov 13, 2015
686
Asynchronous chown(2). No arguments other than a possible exception are given
Nov 18, 2010
Nov 18, 2010
687
to the completion callback.
Oct 28, 2010
Oct 28, 2010
688
Nov 13, 2015
Nov 13, 2015
689
## fs.chownSync(path, uid, gid)
May 17, 2016
May 17, 2016
690
<!-- YAML
691
added: v0.1.97
May 4, 2017
May 4, 2017
692
changes:
693
- version: v7.6.0
694
pr-url: https://github.com/nodejs/node/pull/10739
695
description: The `path` parameter can be a WHATWG `URL` object using `file:`
696
protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
697
-->
Oct 28, 2010
Oct 28, 2010
698
May 4, 2017
May 4, 2017
699
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
700
* `uid` {integer}
701
* `gid` {integer}
Mar 25, 2016
Mar 25, 2016
702
Nov 13, 2015
Nov 13, 2015
703
Synchronous chown(2). Returns `undefined`.
Oct 28, 2010
Oct 28, 2010
704
Nov 13, 2015
Nov 13, 2015
705
## fs.close(fd, callback)
May 17, 2016
May 17, 2016
706
<!-- YAML
707
added: v0.0.2
Feb 24, 2017
Feb 24, 2017
708
changes:
709
- version: v7.0.0
710
pr-url: https://github.com/nodejs/node/pull/7897
711
description: The `callback` parameter is no longer optional. Not passing
May 20, 2017
May 20, 2017
712
it will emit a deprecation warning.
May 17, 2016
May 17, 2016
713
-->
Oct 28, 2010
Oct 28, 2010
714
Mar 8, 2017
Mar 8, 2017
715
* `fd` {integer}
Mar 25, 2016
Mar 25, 2016
716
* `callback` {Function}
717
Nov 13, 2015
Nov 13, 2015
718
Asynchronous close(2). No arguments other than a possible exception are given
719
to the completion callback.
Oct 28, 2010
Oct 28, 2010
720
Nov 13, 2015
Nov 13, 2015
721
## fs.closeSync(fd)
May 17, 2016
May 17, 2016
722
<!-- YAML
723
added: v0.1.21
724
-->
Oct 28, 2010
Oct 28, 2010
725
Mar 8, 2017
Mar 8, 2017
726
* `fd` {integer}
Mar 25, 2016
Mar 25, 2016
727
Nov 13, 2015
Nov 13, 2015
728
Synchronous close(2). Returns `undefined`.
Oct 28, 2010
Oct 28, 2010
729
May 17, 2016
May 17, 2016
730
## fs.constants
731
732
Returns an object containing commonly used constants for file system
733
operations. The specific constants currently defined are described in
734
[FS Constants][].
735
Nov 13, 2015
Nov 13, 2015
736
## fs.createReadStream(path[, options])
May 17, 2016
May 17, 2016
737
<!-- YAML
738
added: v0.1.31
Feb 24, 2017
Feb 24, 2017
739
changes:
May 4, 2017
May 4, 2017
740
- version: v7.6.0
741
pr-url: https://github.com/nodejs/node/pull/10739
742
description: The `path` parameter can be a WHATWG `URL` object using
743
`file:` protocol. Support is currently still *experimental*.
Feb 24, 2017
Feb 24, 2017
744
- version: v7.0.0
745
pr-url: https://github.com/nodejs/node/pull/7831
746
description: The passed `options` object will never be modified.
747
- version: v2.3.0
748
pr-url: https://github.com/nodejs/node/pull/1845
749
description: The passed `options` object can be a string now.
May 17, 2016
May 17, 2016
750
-->
Oct 28, 2010
Oct 28, 2010
751
May 4, 2017
May 4, 2017
752
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
753
* `options` {string|Object}
Mar 2, 2017
Mar 2, 2017
754
* `flags` {string}
755
* `encoding` {string}
Mar 8, 2017
Mar 8, 2017
756
* `fd` {integer}
757
* `mode` {integer}
Mar 2, 2017
Mar 2, 2017
758
* `autoClose` {boolean}
Mar 8, 2017
Mar 8, 2017
759
* `start` {integer}
760
* `end` {integer}
Mar 25, 2016
Mar 25, 2016
761
Dec 3, 2015
Dec 3, 2015
762
Returns a new [`ReadStream`][] object. (See [Readable Stream][]).
Oct 28, 2010
Oct 28, 2010
763
Nov 13, 2015
Nov 13, 2015
764
Be aware that, unlike the default value set for `highWaterMark` on a
765
readable stream (16 kb), the stream returned by this method has a
766
default value of 64 kb for the same parameter.
Apr 17, 2012
Apr 17, 2012
767
Nov 13, 2015
Nov 13, 2015
768
`options` is an object or string with the following defaults:
Apr 17, 2012
Apr 17, 2012
769
Jan 21, 2016
Jan 21, 2016
770
```js
Apr 24, 2017
Apr 24, 2017
771
const defaults = {
Jan 21, 2016
Jan 21, 2016
772
flags: 'r',
773
encoding: null,
774
fd: null,
775
mode: 0o666,
776
autoClose: true
Apr 24, 2017
Apr 24, 2017
777
};
Jan 21, 2016
Jan 21, 2016
778
```
Oct 28, 2010
Oct 28, 2010
779
Nov 13, 2015
Nov 13, 2015
780
`options` can include `start` and `end` values to read a range of bytes from
781
the file instead of the entire file. Both `start` and `end` are inclusive and
Dec 3, 2016
Dec 3, 2016
782
start counting at 0. If `fd` is specified and `start` is omitted or `undefined`,
783
`fs.createReadStream()` reads sequentially from the current file position.
784
The `encoding` can be any one of those accepted by [`Buffer`][].
Oct 28, 2010
Oct 28, 2010
785
Nov 13, 2015
Nov 13, 2015
786
If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
Jun 6, 2016
Jun 6, 2016
787
the specified file descriptor. This means that no `'open'` event will be
788
emitted. Note that `fd` should be blocking; non-blocking `fd`s should be passed
May 17, 2016
May 17, 2016
789
to [`net.Socket`][].
Oct 28, 2010
Oct 28, 2010
790
Nov 13, 2015
Nov 13, 2015
791
If `autoClose` is false, then the file descriptor won't be closed, even if
Apr 28, 2017
Apr 28, 2017
792
there's an error. It is the application's responsibility to close it and make
793
sure there's no file descriptor leak. If `autoClose` is set to true (default
Nov 13, 2015
Nov 13, 2015
794
behavior), on `error` or `end` the file descriptor will be closed
795
automatically.
Oct 28, 2010
Oct 28, 2010
796
Nov 13, 2015
Nov 13, 2015
797
`mode` sets the file mode (permission and sticky bits), but only if the
798
file was created.
Oct 28, 2010
Oct 28, 2010
799
Nov 13, 2015
Nov 13, 2015
800
An example to read the last 10 bytes of a file which is 100 bytes long:
Oct 28, 2010
Oct 28, 2010
801
Jan 21, 2016
Jan 21, 2016
802
```js
Jun 2, 2017
Jun 2, 2017
803
fs.createReadStream('sample.txt', { start: 90, end: 99 });
Jan 21, 2016
Jan 21, 2016
804
```
Oct 28, 2010
Oct 28, 2010
805
Nov 13, 2015
Nov 13, 2015
806
If `options` is a string, then it specifies the encoding.
807
808
## fs.createWriteStream(path[, options])
May 17, 2016
May 17, 2016
809
<!-- YAML
810
added: v0.1.31
Feb 24, 2017
Feb 24, 2017
811
changes:
May 4, 2017
May 4, 2017
812
- version: v7.6.0
813
pr-url: https://github.com/nodejs/node/pull/10739
814
description: The `path` parameter can be a WHATWG `URL` object using
815
`file:` protocol. Support is currently still *experimental*.
Feb 24, 2017
Feb 24, 2017
816
- version: v7.0.0
817
pr-url: https://github.com/nodejs/node/pull/7831
818
description: The passed `options` object will never be modified.
819
- version: v5.5.0
820
pr-url: https://github.com/nodejs/node/pull/3679
821
description: The `autoClose` option is supported now.
822
- version: v2.3.0
823
pr-url: https://github.com/nodejs/node/pull/1845
824
description: The passed `options` object can be a string now.
May 17, 2016
May 17, 2016
825
-->
Nov 13, 2015
Nov 13, 2015
826
May 4, 2017
May 4, 2017
827
* `path` {string|Buffer|URL}
Mar 8, 2017
Mar 8, 2017
828
* `options` {string|Object}
Mar 2, 2017
Mar 2, 2017
829
* `flags` {string}
830
* `defaultEncoding` {string}
Mar 8, 2017
Mar 8, 2017
831
* `fd` {integer}
832
* `mode` {integer}
Mar 2, 2017
Mar 2, 2017
833
* `autoClose` {boolean}
Mar 8, 2017
Mar 8, 2017
834
* `start` {integer}
Mar 25, 2016
Mar 25, 2016
835
Dec 3, 2015
Dec 3, 2015
836
Returns a new [`WriteStream`][] object. (See [Writable Stream][]).
Nov 13, 2015
Nov 13, 2015
837
838
`options` is an object or string with the following defaults:
839
Jan 21, 2016
Jan 21, 2016
840
```js
Apr 24, 2017
Apr 24, 2017
841
const defaults = {
Jan 21, 2016
Jan 21, 2016
842
flags: 'w',
843
defaultEncoding: 'utf8',
844
fd: null,
845
mode: 0o666,
846
autoClose: true
Apr 24, 2017
Apr 24, 2017
847
};
Jan 21, 2016
Jan 21, 2016
848
```
Nov 13, 2015
Nov 13, 2015
849
850
`options` may also include a `start` option to allow writing data at
851
some position past the beginning of the file. Modifying a file rather
852
than replacing it may require a `flags` mode of `r+` rather than the
May 17, 2016
May 17, 2016
853
default mode `w`. The `defaultEncoding` can be any one of those accepted by
854
[`Buffer`][].
Nov 13, 2015
Nov 13, 2015
855
Jan 11, 2016
Jan 11, 2016
856
If `autoClose` is set to true (default behavior) on `error` or `end`
857
the file descriptor will be closed automatically. If `autoClose` is false,
858
then the file descriptor won't be closed, even if there's an error.
Apr 28, 2017
Apr 28, 2017
859
It is the application's responsibility to close it and make sure there's no
860
file descriptor leak.
Jan 11, 2016
Jan 11, 2016
861
Dec 3, 2015
Dec 3, 2015
862
Like [`ReadStream`][], if `fd` is specified, `WriteStream` will ignore the
Nov 13, 2015
Nov 13, 2015
863
`path` argument and will use the specified file descriptor. This means that no
Dec 3, 2015
Dec 3, 2015
864
`'open'` event will be emitted. Note that `fd` should be blocking; non-blocking
865
`fd`s should be passed to [`net.Socket`][].
Nov 13, 2015
Nov 13, 2015
866
867
If `options` is a string, then it specifies the encoding.
868
869
## fs.exists(path, callback)
May 17, 2016
May 17, 2016
870
<!-- YAML
871
added: v0.0.2
May 4, 2017
May 4, 2017
872
changes:
873
- version: v7.6.0
874
pr-url: https://github.com/nodejs/node/pull/10739
875
description: The `path` parameter can be a WHATWG `URL` object using
876
`file:` protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
877
deprecated: v1.0.0
878
-->
Nov 13, 2015
Nov 13, 2015
879
Aug 4, 2016
Aug 4, 2016
880
> Stability: 0 - Deprecated: Use [`fs.stat()`][] or [`fs.access()`][] instead.
Nov 13, 2015
Nov 13, 2015
881
May 4, 2017
May 4, 2017
882
* `path` {string|Buffer|URL}
Mar 25, 2016
Mar 25, 2016
883
* `callback` {Function}
884
Nov 13, 2015
Nov 13, 2015
885
Test whether or not the given path exists by checking with the file system.
886
Then call the `callback` argument with either true or false. Example:
887
Jan 21, 2016
Jan 21, 2016
888
```js
889
fs.exists('/etc/passwd', (exists) => {
890
console.log(exists ? 'it\'s there' : 'no passwd!');
891
});
892
```
Nov 13, 2015
Nov 13, 2015
893
Oct 5, 2016
Oct 5, 2016
894
**Note that the parameter to this callback is not consistent with other
895
Node.js callbacks.** Normally, the first parameter to a Node.js callback is
896
an `err` parameter, optionally followed by other parameters. The
897
`fs.exists()` callback has only one boolean parameter. This is one reason
898
`fs.access()` is recommended instead of `fs.exists()`.
899
Sep 1, 2016
Sep 1, 2016
900
Using `fs.exists()` to check for the existence of a file before calling
901
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
902
so introduces a race condition, since other processes may change the file's
903
state between the two calls. Instead, user code should open/read/write the
904
file directly and handle the error raised if the file does not exist.
905
906
For example:
907
908
**write (NOT RECOMMENDED)**
909
910
```js
911
fs.exists('myfile', (exists) => {
912
if (exists) {
913
console.error('myfile already exists');
914
} else {
915
fs.open('myfile', 'wx', (err, fd) => {
916
if (err) throw err;
917
writeMyData(fd);
918
});
919
}
920
});
921
```
922
923
**write (RECOMMENDED)**
924
925
```js
926
fs.open('myfile', 'wx', (err, fd) => {
927
if (err) {
Mar 27, 2017
Mar 27, 2017
928
if (err.code === 'EEXIST') {
Sep 1, 2016
Sep 1, 2016
929
console.error('myfile already exists');
930
return;
931
}
Mar 27, 2017
Mar 27, 2017
932
933
throw err;
Sep 1, 2016
Sep 1, 2016
934
}
Mar 27, 2017
Mar 27, 2017
935
Sep 1, 2016
Sep 1, 2016
936
writeMyData(fd);
937
});
938
```
939
940
**read (NOT RECOMMENDED)**
941
942
```js
943
fs.exists('myfile', (exists) => {
944
if (exists) {
945
fs.open('myfile', 'r', (err, fd) => {
946
readMyData(fd);
947
});
948
} else {
949
console.error('myfile does not exist');
950
}
951
});
952
```
953
954
**read (RECOMMENDED)**
955
956
```js
957
fs.open('myfile', 'r', (err, fd) => {
958
if (err) {
Mar 27, 2017
Mar 27, 2017
959
if (err.code === 'ENOENT') {
Sep 1, 2016
Sep 1, 2016
960
console.error('myfile does not exist');
961
return;
962
}
Mar 27, 2017
Mar 27, 2017
963
964
throw err;
Sep 1, 2016
Sep 1, 2016
965
}
Mar 27, 2017
Mar 27, 2017
966
967
readMyData(fd);
Sep 1, 2016
Sep 1, 2016
968
});
969
```
970
971
The "not recommended" examples above check for existence and then use the
972
file; the "recommended" examples are better because they use the file directly
973
and handle the error, if any.
974
975
In general, check for the existence of a file only if the file won’t be
976
used directly, for example when its existence is a signal from another
977
process.
Nov 13, 2015
Nov 13, 2015
978
979
## fs.existsSync(path)
May 17, 2016
May 17, 2016
980
<!-- YAML
981
added: v0.1.21
May 4, 2017
May 4, 2017
982
changes:
983
- version: v7.6.0
984
pr-url: https://github.com/nodejs/node/pull/10739
985
description: The `path` parameter can be a WHATWG `URL` object using
986
`file:` protocol. Support is currently still *experimental*.
May 17, 2016
May 17, 2016
987
-->
Nov 13, 2015
Nov 13, 2015
988
May 4, 2017
May 4, 2017
989
* `path` {string|Buffer|URL}
Mar 25, 2016
Mar 25, 2016
990
Dec 3, 2015
Dec 3, 2015
991
Synchronous version of [`fs.exists()`][].
Nov 13, 2015
Nov 13, 2015
992
Returns `true` if the file exists, `false` otherwise.
993
Oct 5, 2016
Oct 5, 2016
994
Note that `fs.exists()` is deprecated, but `fs.existsSync()` is not.
995
(The `callback` parameter to `fs.exists()` accepts parameters that are
996
inconsistent with other Node.js callbacks. `fs.existsSync()` does not use
997
a callback.)
998
Nov 13, 2015
Nov 13, 2015
999
## fs.fchmod(fd, mode, callback)
May 17, 2016
May 17, 2016
1000
<!-- YAML